读书人

难的SQL语句

发布时间: 2012-02-13 17:20:26 作者: rapoo

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

读书人网 >SQL Server

热点推荐