简体   繁体   中英

Multiple sum in SQL query

SELECT sum( plot_status = 'OPEN' ) AS OPEN 
     , sum( plot_status = 'SOLD' ) AS SOLD
FROM `tbl_plot`
GROUP BY `plot_status

This is giving

OPEN   SOLD
7       0
0       8

How to make it

OPEN  SOLD
7      8

Or is it possible?

just remove the GROUP BY clause and it will work.

SELECT sum( plot_status = 'OPEN' ) AS `OPEN` ,
       sum( plot_status = 'SOLD' ) AS SOLD
FROM  `tbl_plot`

If there is present plot_name or id then group by that not by plot_status:

SELECT sum( plot_status = 'OPEN' ) AS
OPEN , sum( plot_status = 'SOLD' ) AS SOLD
FROM `tbl_plot`
GROUP BY //`plot_name or plot_id

This will work for you for individual plot. And if you don't want that then remove the group by clause.

select * from(select sum(plot_status =' tbl_plot )AS OPEN select sum(plot_status =' tbl_plot )如售)tbl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM