特殊SQL示例

ProductsData
pd_num          pd_type  pd_status
SCS-1-00       SCS-1     0
SCS-1-002     SCS-1     0
SCS-2-001     SCS-2     0
SCS-2-001     SCS-2     1

结果(num1 是pd_status=0的个数,num1 是pd_status=1的个数)

pd_type num1   num2
scs-1     2           0

scs_2   1            1


select distinct t3.up_type,   t3.up_num,  iif(isnull(t4.num), 0, t4.num) as non_num   from   

(select distinct t1.pd_type as up_type, iif(ISNULL(t2.num), 0,t2.num) as up_num from ProductsData as t1  
left join 
(select pd_type, count(pd_num) as num from ProductsData where pd_status = 0 group by pd_type) as t2  on t1.pd_type = t2.pd_type) as t3 
left join 
(select pd_type, count(pd_num) as num from ProductsData where pd_status = 1 group by pd_type) as t4 on t4.pd_type = t3.up_type


你可能感兴趣的:(JOIN,sql,UP)