当前位置: 代码迷 >> Sql Server >> 难的SQL语句
  详细解决方案

难的SQL语句

热度:55   发布时间:2016-04-27 15:28:21.0
求一个难的SQL语句
SQL语句:

Select Product_id,Name,(sum(ppvalue +curvalue)) as totalvalue From productpraise_cp_view group by product_id,name order by totalvalue desc

现在要求其中求和的ppvalue和curvalue 是来自表A中的isadauit列的值为1的才相加。视图中的PPVALUE和CURVALUE就是来自表A。





------解决方案--------------------
Select Product_id,Name,(sum(case when isadauit=1 then ppvalue +curvalue else 0 end)) as totalvalue From productpraise_cp_view group by product_id,name order by totalvalue desc
------解决方案--------------------

Select Product_id,
Name,
(sum( case when isadauit = 1 then ppvalue +curvalue else 0 end )) as totalvalue 
From productpraise_cp_view
group by product_id,name 
order by totalvalue desc
------解决方案--------------------
SQL code
Select Select     Product_id,Name,    (select sum(ppvalue +curvalue) from productpraise_cp_view where product_id=a.product_id and Name=a.Name and  isadauit=1) as totalvalue From     productpraise_cp_view  agroup by     product_id,name order by totalvalue desc     select    Product_id,Name,    sum(case when isadauit=1 then ppvalue +curvalue else 0 end) as totalvaluefrom     productpraise_cp_viewgroup by     product_id,name order by totalvalue desc
  相关解决方案