sql 整理(一)

本文提供多个SQL查询案例,展示如何通过嵌套查询和case语句处理不同条件下的计数问题,涉及多表联接和聚合函数,适用于产品分布统计场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、***************************************************************************************

sql 代码
  1. select b.id,b.prod_name,sum(aa) as w1,sum(bb) as w2,sum(cc) as w3,sum(dd) as w4,sum(ee) as w5   
  2. from (   
  3. select a.id,a.prod_name,   
  4. case a.iid when 1 then storeCount1 else 0 end as aa,   
  5. case a.iid when 2 then storeCount1 else 0 end as bb,   
  6. case a.iid when 3 then storeCount1 else 0 end as cc,   
  7. case a.iid when 4 then storeCount1 else 0 end as dd,   
  8. case a.iid when 5 then storeCount1 else 0 end as ee   
  9. from (   
  10. SELECT p.id,i.id as iid,p.prod_name,count(s.id) as storeCount1   
  11. FROM BIZ_PROD_DISTRIBUTION d inner join  
  12.  BIZ_PROD_DISTRIBUTION_ITEM di on d.id=di.main_id inner join  
  13. dim_store s on s.id=d.store_id inner join    
  14. dim_dict_item i on i.id=s.store_type inner join  
  15. dim_product p on p.id=di.prod_id   
  16. where d.cate_type='1' and s.is_closed='1'   
  17. group by p.prod_name,i.id,i.item_name,p.id) a   
  18. ) b   
  19. group by b.id,b.prod_name  

 

2、***************************************************************************************

sql 代码
  1. select sum(g1.a1) as w1,sum(g1.a2) as w2,sum(g1.a3) as w3,sum(g1.a4) as w4,sum(g1.a5) as w5,   
  2. sum(g1.b1) as s1,sum(g1.b2) as s2,sum(g1.b3) as s3,sum(g1.b4) as s4,sum(g1.b5) as s5    
  3. from    
  4.   
  5. (select    
  6.   
  7. case h1.id when '1' then h1.storeTotal ELSE 0 end as a1,   
  8. case h1.id when '2' then h1.storeTotal ELSE 0 end as a2,   
  9. case h1.id when '3' then h1.storeTotal ELSE 0 end as a3,   
  10. case h1.id when '4' then h1.storeTotal ELSE 0 end as a4,   
  11. case h1.id when '5' then h1.storeTotal ELSE 0 end as a5,   
  12.   
  13. case h1.id when '1' then h1.callTotal ELSE 0 end as b1,   
  14. case h1.id when '2' then h1.callTotal ELSE 0 end as b2,   
  15. case h1.id when '3' then h1.callTotal ELSE 0 end as b3,   
  16. case h1.id when '4' then h1.callTotal ELSE 0 end as b4,   
  17. case h1.id when '5' then h1.callTotal ELSE 0 end as b5   
  18.   
  19. from    
  20. (select i.id,x2.storeTotal,x3.callTotal   
  21. from  dim_dict_item i inner join    
  22.   
  23. (select s.store_type,count(s.id) as storeTotal    
  24. from dim_store s    
  25. where s.usage_flag='1' and s.is_closed='1'   
  26. group by store_type)x2   
  27.   
  28. on i.id=x2.store_type   
  29. inner join  
  30.   
  31. (select s.store_type,count(distinct c.STORE_ID) as callTotal    
  32. from dim_store s   
  33. inner join  
  34. CALL_PLAN_ITEM c ON s.ID = c.STORE_ID   
  35. where s.usage_flag='1' and s.is_closed='1'   
  36. group by store_type)x3   
  37.   
  38. on x2.store_type=x3.store_type)h1)g1  

 

3、*******************************************************************************************************************************

sql 代码
  1. select b.id,b.prod_name_en,sum(aa) as w1,sum(bb) as w2,sum(cc) as w3,sum(dd) as w4,sum(ee) as w5   
  2. from (   
  3. select a.id,a.prod_name_en,   
  4. case a.iid when 1 then storeCount1 else 0 end as aa,   
  5. case a.iid when 2 then storeCount1 else 0 end as bb,   
  6. case a.iid when 3 then storeCount1 else 0 end as cc,   
  7. case a.iid when 4 then storeCount1 else 0 end as dd,   
  8. case a.iid when 5 then storeCount1 else 0 end as ee   
  9. from (   
  10. SELECT p.id,i.id as iid,p.prod_name_en,count(s.id) as storeCount1   
  11. FROM BIZ_PROD_DISTRIBUTION d inner join  
  12.  BIZ_PROD_DISTRIBUTION_ITEM di on d.id=di.main_id inner join  
  13. dim_store s on s.id=d.store_id inner join    
  14. dim_dict_item i on i.id=s.store_type inner join  
  15. dim_product p on p.id=di.prod_id   
  16. inner join dim_geography g ON s.geo_id = g.id    
  17. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  18. where d.cate_type='2' and s.is_closed='1' and d.usage_flag='1' and s.usage_flag='1'   
  19.   
  20. group by p.prod_name_en,i.id,i.item_name,p.id) a   
  21. ) b   
  22. group by b.id,b.prod_name_en  

 

4、***********************************************************************************************************************

sql 代码
  1. select b.id,b.prod_name_en,sum(aa) as w1,sum(bb) as w2,sum(cc) as w3,sum(dd) as w4,sum(ee) as w5   
  2. from (   
  3. select a.id,a.prod_name_en,   
  4. case a.iid when 1 then storeCount1 else 0 end as aa,   
  5. case a.iid when 2 then storeCount1 else 0 end as bb,   
  6. case a.iid when 3 then storeCount1 else 0 end as cc,   
  7. case a.iid when 4 then storeCount1 else 0 end as dd,   
  8. case a.iid when 5 then storeCount1 else 0 end as ee   
  9. from (   
  10. SELECT p.id,i.id as iid,p.prod_name_en,count(s.id) as storeCount1   
  11. FROM BIZ_PROD_DISTRIBUTION d inner join  
  12.  BIZ_PROD_DISTRIBUTION_ITEM di on d.id=di.main_id inner join  
  13. dim_store s on s.id=d.store_id inner join    
  14. dim_dict_item i on i.id=s.store_type inner join  
  15. dim_product p on p.id=di.prod_id   
  16. inner join dim_geography g ON s.geo_id = g.id    
  17. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  18. where d.cate_type='2' and s.is_closed='1' and d.usage_flag='1' and s.usage_flag='1'   
  19. and $P!{queryString}   
  20. group by p.prod_name_en,i.id,i.item_name,p.id) a   
  21. ) b   
  22. group by b.id,b.prod_name_en  

 

5、**********************************************************************************************************************

sql 代码
  1. select b.id,b.prod_name_en,sum(aa) as w1,sum(bb) as w2,sum(cc) as w3,sum(dd) as w4,sum(ee) as w5   
  2. from (   
  3. select a.id,a.prod_name_en,   
  4. case a.iid when 1 then storeCount1 else 0 end as aa,   
  5. case a.iid when 2 then storeCount1 else 0 end as bb,   
  6. case a.iid when 3 then storeCount1 else 0 end as cc,   
  7. case a.iid when 4 then storeCount1 else 0 end as dd,   
  8. case a.iid when 5 then storeCount1 else 0 end as ee   
  9. from (   
  10. SELECT p.id,i.id as iid,p.prod_name_en,count(s.id) as storeCount1   
  11. FROM BIZ_PROD_DISTRIBUTION d inner join  
  12.  BIZ_PROD_DISTRIBUTION_ITEM di on d.id=di.main_id inner join  
  13. dim_store s on s.id=d.store_id inner join    
  14. dim_dict_item i on i.id=s.store_type inner join  
  15. dim_product p on p.id=di.prod_id   
  16. inner join dim_geography g ON s.geo_id = g.id    
  17. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  18. where d.cate_type='2' and s.is_closed='1' and d.usage_flag='1' and s.usage_flag='1'   
  19. and $P!{queryString}   
  20. group by p.prod_name_en,i.id,i.item_name,p.id) a   
  21. ) b   
  22. group by b.id,b.prod_name_en  

 

6、****************************************************************************************************************

sql 代码
  1. select b.id,b.prod_name_en,sum(aa) as w1,sum(bb) as w2,sum(cc) as w3,sum(dd) as w4,sum(ee) as w5   
  2. from (   
  3. select a.id,a.prod_name_en,   
  4. case a.iid when 1 then storeCount1 else 0 end as aa,   
  5. case a.iid when 2 then storeCount1 else 0 end as bb,   
  6. case a.iid when 3 then storeCount1 else 0 end as cc,   
  7. case a.iid when 4 then storeCount1 else 0 end as dd,   
  8. case a.iid when 5 then storeCount1 else 0 end as ee   
  9. from (   
  10. SELECT p.id,i.id as iid,p.prod_name_en,count(s.id) as storeCount1   
  11. FROM BIZ_PROD_DISTRIBUTION d inner join  
  12.  BIZ_PROD_DISTRIBUTION_ITEM di on d.id=di.main_id inner join  
  13. dim_store s on s.id=d.store_id inner join    
  14. dim_dict_item i on i.id=s.store_type inner join  
  15. dim_product p on p.id=di.prod_id   
  16. inner join dim_geography g ON s.geo_id = g.id    
  17. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  18. where d.cate_type='1' and s.is_closed='1' and d.usage_flag='1' and s.usage_flag='1'   
  19. and $P!{queryString}   
  20. group by p.prod_name_en,i.id,i.item_name,p.id) a   
  21. ) b   
  22. group by b.id,b.prod_name_en  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值