user3291491
user3291491

Reputation: 1

SQL restrict one column in select query

I would like to restrict the results for column count(rm_ca) as office to be rm_ca ='1'
I can't put this into the where clause as I don't want the other fields restricted. only that one column. How can this be done?

SELECT count(distinct bl) as bld, 
       count(rm) as rooms, 
       sum(area) as total_area, 
       count(rm_ca) as office 
FROM table 
WHERE dp= '345';

Upvotes: 0

Views: 751

Answers (2)

nitesh.kodle123
nitesh.kodle123

Reputation: 1033

Use:

   SELECT count(distinct bl) as bld, 
   count(rm) as rooms, 
   sum(area) as total_area, 
   count(decode (rm_ca,1,0)) as office 
   FROM table 
   WHERE dp= '345';

To learn more about Decode read this

Upvotes: 0

Barmar
Barmar

Reputation: 781068

Use:

SUM(CASE WHEN rm_ca = 1 THEN 1 ELSE 0 END) AS office

Upvotes: 1

Related Questions