sql 整理(三)

sql 代碼
  1. select * from ( select * from(   
  2. select sum(isnull(aa,0)) as a,sum(bb)as b,sum(cc)as c,sum(dd)as d,sum(ee)as e   
  3. from ( select    
  4. case c1.store_type when 1 then a1StoreCount else 0 end as aa,   
  5. case c1.store_type when 2 then a1StoreCount else 0 end as bb,   
  6. case c1.store_type when 3 then a1StoreCount else 0 end as cc,   
  7. case c1.store_type when 4 then a1StoreCount else 0 end as dd,   
  8. case c1.store_type when 5 then a1StoreCount else 0 end as ee   
  9. from ( SELECT a1.store_type,a1.item_name, count(a1.store_id) as a1StoreCount   
  10. FROM BIZ_PROD_PRICE p inner join  
  11. (SELECT s.store_type,p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  12.         FROM BIZ_PROD_PRICE p INNER JOIN  
  13.               BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  14.         dim_store s on s.id=p.store_id inner join    
  15.         dim_dict_item i on i.id=s.store_type inner join  
  16.               dim_product pd ON pd.id = pi.prod_id   
  17. inner join dim_geography g ON s.geo_id = g.id    
  18. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  19.         WHERE pd.id = 284 and $P!{queryString}) a1 on p.id=a1.id INNER JOIN  
  20.           (SELECT p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  21.          FROM BIZ_PROD_PRICE p INNER JOIN  
  22.                BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  23.         dim_store s on s.id=p.store_id inner join    
  24.         dim_dict_item i on i.id=s.store_type inner join  
  25.                dim_compt_prod pd ON pd.id = pi.prod_id   
  26. inner join dim_geography g ON s.geo_id = g.id    
  27. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  28.          WHERE pd.id = 17 and $P!{queryString}) a2 ON a1.store_id = a2.store_id AND    
  29.       a1.prod_price > a2.prod_price  and a1.iid=a2.iid     
  30. group by a1.item_name,a1.store_type   
  31. )c1    
  32. )e1)d,   
  33. (select sum(aa) as a1,sum(bb)as b1,sum(cc)as c1,sum(dd)as d1,sum(ee)as e1   
  34. from ( select    
  35. case c1.store_type when 1 then a1StoreCount else 0 end as aa,   
  36. case c1.store_type when 2 then a1StoreCount else 0 end as bb,   
  37. case c1.store_type when 3 then a1StoreCount else 0 end as cc,   
  38. case c1.store_type when 4 then a1StoreCount else 0 end as dd,   
  39. case c1.store_type when 5 then a1StoreCount else 0 end as ee   
  40. from (SELECT a1.store_type,a1.item_name, count(a1.store_id) as a1StoreCount FROM    
  41. BIZ_PROD_PRICE p inner join  
  42. (SELECT s.store_type,p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  43.         FROM BIZ_PROD_PRICE p INNER JOIN  
  44.               BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  45.         dim_store s on s.id=p.store_id inner join    
  46.         dim_dict_item i on i.id=s.store_type inner join  
  47.               dim_product pd ON pd.id = pi.prod_id   
  48. inner join dim_geography g ON s.geo_id = g.id    
  49. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  50.         WHERE pd.id = 284 and $P!{queryString}) a1 on p.id=a1.id INNER JOIN  
  51.           (SELECT p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  52.          FROM BIZ_PROD_PRICE p INNER JOIN  
  53.                BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  54.         dim_store s on s.id=p.store_id inner join    
  55.         dim_dict_item i on i.id=s.store_type inner join  
  56.                dim_compt_prod pd ON pd.id = pi.prod_id   
  57. inner join dim_geography g ON s.geo_id = g.id    
  58. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  59.          WHERE pd.id = 50 and $P!{queryString}) a2 ON a1.store_id = a2.store_id AND    
  60.       a1.prod_price > a2.prod_price and a1.iid=a2.iid and p.id=a2.id   
  61. group by a1.item_name,a1.store_type   
  62. )c1    
  63. )e2)d1,   
  64. (   
  65. select sum(aa) as a2,sum(bb)as b2,sum(cc)as c2,sum(dd)as d2,sum(ee)as e2   
  66. from ( select    
  67. case c1.store_type when 1 then a1StoreCount else 0 end as aa,   
  68. case c1.store_type when 2 then a1StoreCount else 0 end as bb,   
  69. case c1.store_type when 3 then a1StoreCount else 0 end as cc,   
  70. case c1.store_type when 4 then a1StoreCount else 0 end as dd,   
  71. case c1.store_type when 5 then a1StoreCount else 0 end as ee   
  72. from (SELECT a1.store_type,a1.item_name,count(a1.store_id) as a1StoreCount FROM    
  73. BIZ_PROD_PRICE p inner join  
  74. (SELECT s.store_type,p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  75.         FROM BIZ_PROD_PRICE p INNER JOIN  
  76.               BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  77.         dim_store s on s.id=p.store_id inner join    
  78.         dim_dict_item i on i.id=s.store_type inner join  
  79.               dim_product pd ON pd.id = pi.prod_id   
  80. inner join dim_geography g ON s.geo_id = g.id    
  81. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  82.         WHERE pd.id = 343 and $P!{queryString}) a1 on p.id=a1.id INNER JOIN  
  83.           (SELECT p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  84.          FROM BIZ_PROD_PRICE p INNER JOIN  
  85.                BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  86.         dim_store s on s.id=p.store_id inner join    
  87.         dim_dict_item i on i.id=s.store_type inner join  
  88.                dim_compt_prod pd ON pd.id = pi.prod_id   
  89. inner join dim_geography g ON s.geo_id = g.id    
  90. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  91.          WHERE pd.id = 22 and $P!{queryString}) a2 ON a1.store_id = a2.store_id AND    
  92.       a1.prod_price > a2.prod_price  and a1.iid=a2.iid    
  93. group by a1.item_name,a1.store_type   
  94. )c1    
  95. )e3)d2,   
  96. (   
  97. select sum(aa) as a3,sum(bb)as b3,sum(cc)as c3,sum(dd)as d3,sum(ee)as e3   
  98. from ( select     
  99. case c1.store_type when 1 then a1StoreCount else 0 end as aa,   
  100. case c1.store_type when 2 then a1StoreCount else 0 end as bb,   
  101. case c1.store_type when 3 then a1StoreCount else 0 end as cc,   
  102. case c1.store_type when 4 then a1StoreCount else 0 end as dd,   
  103. case c1.store_type when 5 then a1StoreCount else 0 end as ee   
  104. from (SELECT a1.store_type,a1.item_name,count(a1.store_id) as a1StoreCount  FROM    
  105. BIZ_PROD_PRICE p inner join  
  106. (SELECT s.store_type,p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  107.         FROM BIZ_PROD_PRICE p INNER JOIN  
  108.               BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  109.         dim_store s on s.id=p.store_id inner join    
  110.         dim_dict_item i on i.id=s.store_type inner join  
  111.               dim_product pd ON pd.id = pi.prod_id   
  112. inner join dim_geography g ON s.geo_id = g.id    
  113. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  114.         WHERE pd.id = 341 and $P!{queryString}) a1 on p.id=a1.id INNER JOIN  
  115.           (SELECT p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  116.          FROM BIZ_PROD_PRICE p INNER JOIN  
  117.                BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  118.         dim_store s on s.id=p.store_id inner join    
  119.         dim_dict_item i on i.id=s.store_type inner join  
  120.                dim_compt_prod pd ON pd.id = pi.prod_id   
  121. inner join dim_geography g ON s.geo_id = g.id    
  122. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  123.          WHERE pd.id = 22 and $P!{queryString}) a2 ON a1.store_id = a2.store_id AND    
  124.       a1.prod_price > a2.prod_price  and a1.iid=a2.iid    
  125. group by a1.item_name,a1.store_type   
  126. )c1    
  127. )e4)d3,   
  128. (   
  129. select sum(aa) as a4,sum(bb)as b4,sum(cc)as c4,sum(dd)as d4,sum(ee)as e4   
  130. from (   
  131. select    
  132. case c1.store_type when 1 then a1StoreCount else 0 end as aa,   
  133. case c1.store_type when 2 then a1StoreCount else 0 end as bb,   
  134. case c1.store_type when 3 then a1StoreCount else 0 end as cc,   
  135. case c1.store_type when 4 then a1StoreCount else 0 end as dd,   
  136. case c1.store_type when 5 then a1StoreCount else 0 end as ee   
  137. from  
  138. (SELECT a1.store_type,a1.item_name,count(a1.store_id) as a1StoreCount   
  139. FROM    
  140. BIZ_PROD_PRICE p inner join  
  141. (SELECT s.store_type,p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  142.         FROM BIZ_PROD_PRICE p INNER JOIN  
  143.               BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  144.         dim_store s on s.id=p.store_id inner join    
  145.         dim_dict_item i on i.id=s.store_type inner join  
  146.               dim_product pd ON pd.id = pi.prod_id   
  147. inner join dim_geography g ON s.geo_id = g.id    
  148. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  149.         WHERE pd.id = 345 and $P!{queryString}) a1 on p.id=a1.id INNER JOIN  
  150.           (SELECT p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  151.          FROM BIZ_PROD_PRICE p INNER JOIN  
  152.                BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  153.         dim_store s on s.id=p.store_id inner join    
  154.         dim_dict_item i on i.id=s.store_type inner join  
  155.                dim_compt_prod pd ON pd.id = pi.prod_id   
  156. inner join dim_geography g ON s.geo_id = g.id    
  157. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  158.          WHERE pd.id = 22 and $P!{queryString}) a2 ON a1.store_id = a2.store_id AND    
  159.       a1.prod_price > a2.prod_price and a1.iid=a2.iid    
  160. group by a1.item_name,a1.store_type   
  161. )c1    
  162. )e5)d4   
  163. )w1,   
  164. ---lower-----------------------------------------------------------------   
  165. select * from(   
  166. select sum(isnull(aa,0)) as sa,sum(bb)as sb,sum(cc)as sc,sum(dd)as sd,sum(ee)as se from (   
  167. select     
  168. case c1.store_type when 1 then a1StoreCount else 0 end as aa,   
  169. case c1.store_type when 2 then a1StoreCount else 0 end as bb,   
  170. case c1.store_type when 3 then a1StoreCount else 0 end as cc,   
  171. case c1.store_type when 4 then a1StoreCount else 0 end as dd,   
  172. case c1.store_type when 5 then a1StoreCount else 0 end as ee   
  173. from ( SELECT a1.store_type,a1.item_name, count(a1.store_id) as a1StoreCount FROM    
  174. BIZ_PROD_PRICE p inner join  
  175. (SELECT s.store_type,p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  176.         FROM BIZ_PROD_PRICE p INNER JOIN  
  177.               BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  178.         dim_store s on s.id=p.store_id inner join    
  179.         dim_dict_item i on i.id=s.store_type inner join  
  180.               dim_product pd ON pd.id = pi.prod_id   
  181. inner join dim_geography g ON s.geo_id = g.id    
  182. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  183.         WHERE pd.id = 284 and $P!{queryString}) a1 on p.id=a1.id INNER JOIN  
  184.           (SELECT p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  185.          FROM BIZ_PROD_PRICE p INNER JOIN  
  186.                BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  187.         dim_store s on s.id=p.store_id inner join    
  188.         dim_dict_item i on i.id=s.store_type inner join  
  189.                dim_compt_prod pd ON pd.id = pi.prod_id   
  190. inner join dim_geography g ON s.geo_id = g.id    
  191. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  192.          WHERE pd.id = 17 and $P!{queryString}) a2 ON a1.store_id = a2.store_id AND    
  193.       a1.prod_price <= a2.prod_price  and a1.iid=a2.iid     
  194.   
  195. group by a1.item_name,a1.store_type   
  196. )c1 )e1)d,   
  197. (select sum(aa) as sa1,sum(bb)as sb1,sum(cc)as sc1,sum(dd)as sd1,sum(ee)as se1   
  198. from ( select    
  199. case c1.store_type when 1 then a1StoreCount else 0 end as aa,   
  200. case c1.store_type when 2 then a1StoreCount else 0 end as bb,   
  201. case c1.store_type when 3 then a1StoreCount else 0 end as cc,   
  202. case c1.store_type when 4 then a1StoreCount else 0 end as dd,   
  203. case c1.store_type when 5 then a1StoreCount else 0 end as ee   
  204. from  
  205. (SELECT a1.store_type,a1.item_name, count(a1.store_id) as a1StoreCount   
  206. FROM    
  207. BIZ_PROD_PRICE p inner join  
  208. (SELECT s.store_type,p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  209.         FROM BIZ_PROD_PRICE p INNER JOIN  
  210.               BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  211.         dim_store s on s.id=p.store_id inner join    
  212.         dim_dict_item i on i.id=s.store_type inner join  
  213.               dim_product pd ON pd.id = pi.prod_id   
  214. inner join dim_geography g ON s.geo_id = g.id    
  215. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  216.         WHERE pd.id = 284 and $P!{queryString}) a1 on p.id=a1.id INNER JOIN  
  217.           (SELECT p.id,p.store_id,i.id as iid,i.item_name, pd.prod_name, pi.prod_price   
  218.          FROM BIZ_PROD_PRICE p INNER JOIN  
  219.                BIZ_PROD_PRICE_ITEM pi ON p.id = pi.main_id INNER JOIN  
  220.         dim_store s on s.id=p.store_id inner join    
  221.         dim_dict_item i on i.id=s.store_type inner join  
  222.                dim_compt_prod pd ON pd.id = pi.prod_id   
  223. inner join dim_geography g ON s.geo_id = g.id    
  224. inner join dim_geography g1 ON g.parent_geography_id = g1.id   
  225.          WHERE pd.id = 50 and $P!{queryString}) a2 ON a1.store_id = a2.store_id AND    
  226.       a1.prod_price <= a2.prod_price and a1.iid=a2.iid and p.id=a2.id   
  227. group by a1.item
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章