Thursday, February 18, 2010

SQL Case Statements for Sum Operations

SELECT DISTINCT TOP (100) PERCENT
room_type, COUNT(room_id) AS rooms, SUM(room_area) AS sqft,
CASE
WHEN room_type = 1 THEN 'Office'
WHEN room_type = 2 THEN 'Storage'
WHEN room_type = 3 THEN 'Equipment'
WHEN room_type = 4 THEN 'Computer'
WHEN room_type = 5 THEN 'Medical'
WHEN room_type = 6 THEN 'Public'
ELSE 'Unknown'
END AS TypeName
FROM dbo.tbl_rooms
GROUP BY room_type
ORDER BY room_type


Returns something like the following:



room_type rooms     sqft
Office 210 6,540
Storage 54 983
Equipment 86 1,002
Computer 3 852
Medical 640 39,553
Public 6 10,401
Unknown 0 0

No comments:

Post a Comment