求大牛指教
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 也去掉看看
[其他解释]
选择列表中的列 '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
[其他解释]
消息 8120,级别 16,状态 1,第 1 行
选择列表中的列 'tb_gift.GiftPrice' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
[其他解释]
select 总价=sum(casewhen giftprice>0 then giftprice*quantity else 0 end)
[其他解释]
谢谢了,不过不是我所表达的那样,我是想把不同用户的购买礼物总价给分别求出来!
[其他解释]
非常感谢,只有那么点分了。原来sum函数是不允许嵌套的。