日期:2014-05-18  浏览次数:20457 次

求一个难的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  a
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