Search
Search

Reputation: 13

Total of the columns in PostgreSQL

Existing Query

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

I want to get the Total of Allotted, Vacant and Amenities.

Upvotes: 0

Views: 50

Answers (1)

StanislavL
StanislavL

Reputation: 57381

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

Upvotes: 1

Related Questions