sql 整理(二)

本文提供了多个SQL查询示例,包括按产品统计不同类型的门店数量、计算用户活动指标等。通过这些案例,展示了如何使用CASE语句进行条件判断及聚合函数进行数据汇总。
sql 代码

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  usercount,uploaduser,storenum,callstore,plancallstore,callstore/(case when plancallstore = 0 then 1 else plancallstore endas plancallrate,trainNum,storeCheckNum,priceTrack,priceTrack2 from  
  2. (   
  3. select count(*) as usercount from dim_mobi_user u    
  4. inner join dim_employee e on u.employee_id = e.id   
  5. inner join dim_org s on e.org_id = s.id   
  6. where u.usage_flag='1' and e.usage_flag='1' and s.usage_flag='1'    
  7. $P!{queryStringOrg}   
  8. ) aa,   
  9. -----------------   
  10. (   
  11. select count(*) as uploaduser from  
  12. (   
  13. select upload_by as upuser from biz_placement p    
  14. inner join dim_store s on p.store_id = s.id   
  15. where p.usage_flag='1' and s.usage_flag='1'   
  16. $P!{queryStringUpload}   
  17. $P!{queryStringStore}   
  18. union  
  19. select p.upload_by as upuser from biz_storecheck_price p    
  20. inner join dim_store s on p.store_id = s.id    
  21. $P!{queryStringUpload}   
  22. $P!{queryStringStore}   
  23. union  
  24. select p.upload_by as upuser from biz_prod_distribution p    
  25. inner join dim_store s on p.store_id = s.id    
  26. where p.usage_flag='1' and s.usage_flag='1'   
  27. $P!{queryStringUpload}   
  28. $P!{queryStringStore}   
  29. union  
  30. select p.upload_by as upuser from biz_prod_price p    
  31. inner join dim_store s on p.store_id = s.id   
  32. where p.usage_flag='1' and s.usage_flag='1'    
  33. $P!{queryStringUpload}   
  34. $P!{queryStringStore}   
  35. union  
  36. select p.upload_by as upuser from biz_storecheck_instore p    
  37. inner join dim_store s on p.store_id = s.id   
  38. where p.usage_flag='1' and s.usage_flag='1'    
  39. $P!{queryStringUpload}   
  40. $P!{queryStringStore}   
  41. union  
  42. select p.upload_by as upuser from biz_store_stop p    
  43. inner join dim_store s on p.store_id = s.id   
  44. where p.usage_flag='1' and s.usage_flag='1'    
  45. $P!{queryStringUpload}   
  46. $P!{queryStringStore}   
  47. union  
  48. select p.upload_by as upuser from biz_train_data p    
  49. inner join dim_employee e on p.TRAINER_ID = e.id   
  50. inner join dim_org  s on e.org_id = s.id   
  51. where p.usage_flag='1' and e.usage_flag='1'  and s.usage_flag='1'    
  52. $P!{queryStringUpload}   
  53. $P!{queryStringOrg}   
  54. )b   
  55. )bb,   
  56. (   
  57. select count(*) as storenum from dim_store u where u.usage_flag='1'    
  58. --$P!{queryStringUpdate}   
  59. $P!{queryStore}   
  60. )cc,   
  61. (   
  62. select cast(count(*) as floatas callstore from  
  63. (   
  64. select p.store_id as callstore from biz_placement p    
  65. inner join dim_store s  on p.store_id = s.id    
  66. where p.usage_flag='1' and s.usage_flag='1'    
  67. $P!{queryStringUpload}   
  68. $P!{queryStringStore}   
  69. union  
  70. select p.store_id as callstore from biz_storecheck_price p    
  71. inner join dim_store s on p.store_id = s.id   
  72. where p.usage_flag='1' and s.usage_flag='1'    
  73. $P!{queryStringUpload}   
  74. $P!{queryStringStore}   
  75. union  
  76. select p.store_id as callstore from biz_prod_distribution p    
  77. inner join dim_store s on p.store_id = s.id   
  78. where p.usage_flag='1' and s.usage_flag='1'    
  79. $P!{queryStringUpload}   
  80. $P!{queryStringStore}   
  81. union  
  82. select p.store_id as callstore from biz_prod_price p    
  83. inner join dim_store s on p.store_id = s.id   
  84. where p.usage_flag='1' and s.usage_flag='1'    
  85. $P!{queryStringUpload}   
  86. $P!{queryStringStore}   
  87. union  
  88. select p.store_id as callstore from biz_storecheck_instore p    
  89. inner join dim_store s on p.store_id = s.id   
  90. where p.usage_flag='1' and s.usage_flag='1'    
  91. $P!{queryStringUpload}   
  92. $P!{queryStringStore}   
  93. )c   
  94. )dd,   
  95. (   
  96. select cast(count(up.store_id) as floatas plancallstore from call_plan_item up    
  97. inner join biz_call_plan u on up.plan_id = u.id    
  98. inner join dim_store s on up.store_id = s.id   
  99. and u.usage_flag='1' and u.usage_flag='1'  and s.usage_flag='1'    
  100. $P!{queryStringCallplan}    
  101. $P!{queryStringStore}   
  102. )ee   
  103. ,   
  104. (   
  105. select count(*) as trainNum  from biz_train_data p    
  106. inner join dim_employee e on p.TRAINER_ID = e.id   
  107. inner join dim_org  s on e.org_id = s.id   
  108. where p.usage_flag='1'  and e.usage_flag='1'  and s.usage_flag='1'    
  109. $P!{queryStringUpload}    
  110. $P!{queryStringOrg}   
  111. )ff,   
  112. (   
  113. select count(p.store_id) as storeCheckNum from BIZ_STORECHECK_INSTORE p    
  114. inner join dim_store s on p.store_id = s.id   
  115. where p.usage_flag='1'  and s.usage_flag='1'    
  116. $P!{queryStringUpload}    
  117. $P!{queryStringStore}   
  118. )jj,   
  119. (   
  120. select count(*) as priceTrack from DIM_PRICE_TRACK k where k.usage_flag='1'    
  121. and k.CHNL_TYPE = '11'   
  122. )hh,   
  123. (   
  124. select count(*) as priceTrack2 from DIM_PRICE_TRACK k where k.usage_flag='1'    
  125. and k.CHNL_TYPE = '12'   
  126. )ii  

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

sql 代码
  1. SELECT prod.prod_name,chnl.org_name as chnl,rd.org_name as rd,   
  2. bm.org_name as bm,cm.org_name as cm,g1.geography_name as province,   
  3. g.geography_name as city,s.store_name_cn,s.store_name_en,s.store_code,   
  4. case s.store_type when '1' then  '15+' WHEN '2' THEN '4-14'   
  5.  WHEN '3' THEN '1-3' WHEN '4' THEN 'CVS' WHEN '5' THEN 'W/S'  END AS storeType   
  6. FROM REF_PROD_STORE_TYPE_5P  p inner join  
  7. REF_PROD_STORE_TYPE_5P_ITEM  pi on pi.ref_id=p.id  and p.usage_flag='1'   
  8. inner join dim_product prod on pi.prod_id=prod.id    
  9. inner join dim_store s on s.store_type=p.store_type and s.usage_flag='1'   
  10. left join biz_prod_distribution d on s.id=d.id and d.usage_flag='1'    
  11. left join biz_prod_distribution_item di on di.main_id=d.id and di.prod_id=prod.id    
  12. inner join dim_org chnl on s.chnl_id=chnl.id   
  13. inner join dim_org rd on s.rd_id=rd.id   
  14. inner join dim_org bm on s.BM_id=bm.id   
  15. inner join dim_org cm on s.cm_id=cm.id   
  16. inner join dim_geography g on g.id=s.geo_id    
  17. inner join dim_geography g1 ON g.parent_geography_id = g1.id    
  18. where (d.store_id is null or di.prod_id is nulland $P!{queryString}   
  19. order by prod.prod_name  

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

sql 代码
  1. SELECT prod.prod_name,case s.store_type when '1' then  '15+' WHEN '2' THEN '4-14'   
  2.  WHEN '3' THEN '1-3' WHEN '4' THEN 'CVS' WHEN '5' THEN 'W/S'  END AS storeType,count(s.id) as num   
  3. FROM REF_PROD_STORE_TYPE_5P  p inner join  
  4. REF_PROD_STORE_TYPE_5P_ITEM  pi on pi.ref_id=p.id  and p.usage_flag='1'   
  5. inner join dim_product prod on pi.prod_id=prod.id    
  6. inner join dim_store s on s.store_type=p.store_type and s.usage_flag='1'   
  7. inner join dim_geography g on g.id=s.geo_id    
  8. inner join dim_geography g1 ON g.parent_geography_id = g1.id    
  9. left join biz_prod_distribution d on s.id=d.id and d.usage_flag='1'    
  10. left join biz_prod_distribution_item di on di.main_id=d.id and di.prod_id=prod.id    
  11. where (d.store_id is null or di.prod_id is nulland $P!{queryString}   
  12. group by prod.prod_name,s.store_type   
  13. order by prod.prod_name  

 

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

sql 代码
  1. select sum(aa) as 'a15+',sum(bb) as 'a1-14',sum(cc) as 'a1-3',sum(dd) as 'aCVS',    
  2.        sum(xx) as 'b15+',sum(yy) as 'b1-14',sum(zz) as 'b1-3',sum(uu) as 'bCVS'   
  3. from(   
  4. select     
  5. case c.storeType when 1 then c.num else 0 end as aa,   
  6. case c.storeType when 2 then c.num else 0 end as bb,   
  7. case c.storeType when 3 then c.num else 0 end as cc,   
  8. case c.storeType when 4 then c.num else 0 end as dd,   
  9. case c.storeType when 5 then c.num else 0 end as ee,   
  10.   
  11. case c.storeType when 1 then c.numb else 0 end as xx,   
  12. case c.storeType when 2 then c.numb else 0 end as yy,   
  13. case c.storeType when 3 then c.numb else 0 end as zz,   
  14. case c.storeType when 4 then c.numb else 0 end as uu,   
  15. case c.storeType when 5 then c.numb else 0 end as vv    
  16. from(   
  17. select a.store_type as storeType,a.num as num,b.num as numb from (   
  18. select store_type,count(1) as num   
  19. from DIM_STORE    
  20. where USAGE_FLAG='1' and IS_CLOSED='1'    
  21. group by store_type)   
  22. inner join    
  23. (   
  24. select s.store_type as storeType,count(distinct p.store_id) as num from    
  25. dim_store s inner join call_plan_item p on s.id=p.store_id   
  26. group by s.store_type   
  27. ) b on a.store_type=b.storeType    
  28. ) c   
  29. ) f  

 

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

 

sql 代码
  1. select  t.id as id,dc.id as prodid, dc.prod_name,i.prod_price,CONVERT(varchar(10) ,t.upload_datetime, 120 ) as upload_datetime   
  2. from BIZ_PROD_PRICE  t   
  3.   
  4. inner join BIZ_PROD_PRICE_ITEM i on t.id = i.main_id    
  5.   
  6. inner join dim_product dc on i.prod_id = dc.id   
  7.   
  8. where  i.main_id in  
  9.   
  10. (   
  11. select top 8 id  from BIZ_PROD_PRICE $P!{queryString} order by upload_datetime desc  
  12. )   
  13.   
  14. order by t.upload_datetime desc  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值