首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求大牛赐教

2012-12-16 
求大牛指教select TB_USERINFO.userid as 用户ID,username as 用户名,giftprice as 单价,sum(quantity) as

求大牛指教

select TB_USERINFO.userid as 用户ID,username as 用户名,giftprice as 单价
,sum(quantity) as 数量,总价=case
when giftprice>0 then giftprice*sum(quantity) 
else 0
end
  from tb_vipbuygift join tb_vip on vip_id=vipid join tb_userinfo on tb_userinfo.userid=tb_vip.userid join tb_gift on tb_gift.giftid=TB_VipBuyGift.giftid group by username,giftprice,TB_USERINFO.userid
返回了
用户ID  用户名    礼物价格  礼物数量  总价
20vvvvvv5030015000
20vvvvvv5002010000
26pppppp500126000 结果集,但是不是我想要的,我想在通过一条SQL语句查出不同用户购买所有礼物的总价 

就像ID 20用户 购买礼物总价为25000
[最优解释]
select TB_USERINFO.userid as 用户ID,username as 用户名,总价=sum(case when giftprice>0 then giftprice*quantity else 0 end)
from ...
group by TB_USERINFO.userid,username
[其他解释]
你把giftprice as 单价 去掉,group by的giftprice 也去掉看看
[其他解释]
引用:
你把giftprice as 单价 去掉,group by的giftprice 也去掉看看

选择列表中的列 'tb_gift.GiftPrice' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。

[其他解释]
select 总价=case
when giftprice>0 then giftprice*sum(quantity) 
else 0
end
  from tb_vipbuygift join tb_vip on vip_id=vipid join tb_userinfo on tb_userinfo.userid=tb_vip.userid join tb_gift on tb_gift.giftid=TB_VipBuyGift.giftid 

[其他解释]
引用:
SQL code12345select 总价=casewhen giftprice>0 then giftprice*sum(quantity) else 0end  from tb_vipbuygift join tb_vip on vip_id=vipid join tb_userinfo on tb_userinfo.userid=tb_vip.userid joi……

消息 8120,级别 16,状态 1,第 1 行
选择列表中的列 'tb_gift.GiftPrice' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。

[其他解释]
select 总价=sum(casewhen giftprice>0 then giftprice*quantity else 0 end)
[其他解释]

引用:
select 总价=sum(casewhen giftprice>0 then giftprice*quantity else 0 end)

谢谢了,不过不是我所表达的那样,我是想把不同用户的购买礼物总价给分别求出来!
[其他解释]
引用:
select TB_USERINFO.userid as 用户ID,username as 用户名,总价=sum(case when giftprice>0 then giftprice*quantity else 0 end)
from ...
group by TB_USERINFO.userid,username

非常感谢,只有那么点分了。原来sum函数是不允许嵌套的。

热点排行