PostgreSQL中的列总数

时间:2016-09-29 06:07:08

标签: sql postgresql

现有查询

SELECT nmindar,
         count(indx) FILTER(WHERE indx='Allotted') Allotted,
         count(indx) FILTER(WHERE indx='Vacant') Vacant,
         count(indx) FILTER(WHERE indx='Amenities') Amenities
  FROM plotboundary  where indzone='Belagavi Zone' group by nmindar order by nmindar

Expected Result

我希望获得分配,空置和设施的总数。

1 个答案:

答案 0 :(得分:1)

SELECT nmindar,
     count(indx) FILTER(WHERE indx='Allotted') Allotted,
     count(indx) FILTER(WHERE indx='Vacant') Vacant,
     count(indx) FILTER(WHERE indx='Amenities') Amenities
FROM plotboundary  
where indzone='Belagavi Zone' 
group by nmindar 

UNION ALL 

SELECT 'ZZZZZZZZZZZ' as nmindar, 
      SUM(Allotted) as Allotted,
      SUM(Vacant) as Vacant,
      SUM(Amenities) as Amenities
FROM (the original query) sub

order by nmindar