请教一个求和的sql语句,马上给分

slashh 2003-10-09 03:00:54

我写的一个sql句
select t_trade.price,t_trade.tradename,sum(t_orderdish.amount) as total_amount,sum(t_trade.price) as total_price from t_orderdish,t_trade where t_orderdish.tableindex='"&tableindex&"'
and t_orderdish.tradeid=t_trade.tradeid
GROUP BY t_trade.price,t_trade.tradename


任取一个tableindex值
得到一下结果

price tradename amount totalprice
1200.0000 xo 1 1200.0000
1200.0000 白兰地 2 2400.0000
20.0000 花生 1 20.0000
20.0000 卡布其洛 1 20.0000
15.0000 烈焰红唇 2 30.0000
1200.0000 人头马 13 15600.0000
20.0000 喜力 1 20.0000



能否在同一个sql句中求出sum(totalprice)呢
就是求出查询结果中所有totalprice 的和呢??
感激中~~~



...全文
37 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
slashh 2003-10-09
  • 打赏
  • 举报
回复
懂了 :)
sdhdy 2003-10-09
  • 打赏
  • 举报
回复
在你那个SQL语句中不可能既求出sum(t_trade.price) as total_price,又求出sum(total_price)因为他是按t_trade.price,t_trade.tradename分组的
用union 是把两个结果集合并为一个
zhbname 2003-10-09
  • 打赏
  • 举报
回复
select t_trade.price,t_trade.tradename,sum(t_orderdish.amount) as total_amount,
sum(t_trade.price) as total_price,
@totalprice=@totalprice+sum(t_trade.price)
from t_orderdish,t_trade where t_orderdish.tableindex='"&tableindex&"'
and t_orderdish.tradeid=t_trade.tradeid
GROUP BY t_trade.price,t_trade.tradename
sdhdy 2003-10-09
  • 打赏
  • 举报
回复
select t_trade.price,t_trade.tradename,sum(t_orderdish.amount) as total_amount,sum(t_trade.price) as total_price
from t_orderdish,t_trade where t_orderdish.tableindex='"&tableindex&"'
and t_orderdish.tradeid=t_trade.tradeid
GROUP BY t_trade.price,t_trade.tradename
union
select 0,'',0,sum(t_trade.price) as total_price
from t_orderdish,t_trade where t_orderdish.tableindex='"&tableindex&"'
and t_orderdish.tradeid=t_trade.tradeid
slashh 2003-10-09
  • 打赏
  • 举报
回复
to txlicenhe(马可):

小弟是初学者,实在不明白您的这句
Union all
Select 9999,'合计',999,sum(t_trade.price) from t_trade


我的意思是在我的这个sql句中既要求出sum(t_trade.price) as total_price
又要求出sum(total_price)
郁闷之极...
slashh 2003-10-09
  • 打赏
  • 举报
回复
简单的说就是要把返回的totalprice全部加起来~
伍子V5 2003-10-09
  • 打赏
  • 举报
回复
关键是不知道你怎么排
txlicenhe 2003-10-09
  • 打赏
  • 举报
回复
select t_trade.price,t_trade.tradename,sum(t_orderdish.amount) as total_amount,sum(t_trade.price) as total_price from t_orderdish,t_trade where t_orderdish.tableindex='"&tableindex&"'
and t_orderdish.tradeid=t_trade.tradeid
GROUP BY t_trade.price,t_trade.tradename

Union all
Select 9999,'合计',999,sum(t_trade.price) from t_trade

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧