SQL语句不起作用

时间:2013-09-12 09:20:49

标签: sql postgresql

如何做一个真正有用的sql语句:

select distinct store_id as myid, 
       (select count(*) from products where store_id = myid and delete_flag = true)     
from products 
where delete_flag = true 

我想要一个包含每个商店ID的列和具有此商店ID的已删除商品的数量。

2 个答案:

答案 0 :(得分:3)

select store_id, 
       count(*) as all_product_count,
       sum(case when delete_flag = 1 then 1 else 0 end) as deleted_product_count
from products     
group by store_id

答案 1 :(得分:0)

希望它有所帮助!

select store_id as myid , count(*) as noofdeleteditems from products where delete_flag = true group by store_id;