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 用户名 礼物价格 礼物数量 总价
20 vvvvvv 50 300 15000
20 vvvvvv 500 20 10000
26 pppppp 500 12 6000 结果集,但是不是我想要的,我想在通过一条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函数是不允许嵌套的。