我有2张桌子
表1:发票
我有一行如下
这里有两张发票,其中invoice_id = 1,2
当发票状态处于查询状态时,则会计入“pending_reason”。
这里我需要结果集如下:
答案 0 :(得分:2)
你可以使用不同的invoce_id计数,如果是发布和查询
select count(distinct invoce_id) no_of_invoices,
sum(if(s_status = 'posted', 1,0)) invoice_status_posted,
sum(if(s_status = 'query', 1,0)) invoice_status_query
from my_table
并为您更新..问题....
select count(distinct invoce_id) no_of_invoices,
sum(if(s_status = 'posted', 1,0)) invoice_status_posted,
sum(if(s_status = 'query' AND pending_reason is null, 1,0)) invoice_status_query,
sum(if() s_status = 'query' and pending_reason ='Pending Internal Clariication', 1,0)) Pending_Internal_Clariication
from my_table