作者nd5287 (Mike)
看板Database
标题[SQL ] 将SQL查询结果 再做群组汇总
时间Thu Sep 7 00:58:39 2017
资料库名称:
资料库版本:sql 2014
内容/问题描述:
我下了 以下的语法查询出的结果如下
select DISTINCT B.CU_order_amount "金额",D.CU_name as "地区",
B.BillDate as "日期" from CU_order B
join CU_cust_info C on B.CU_cust_id=C.CU_cust_id
join CU_city D on C.CU_city_id = D.CU_Id
where B.CU_cust_id in
(select CU_cust_id from CU_cust_info C where CU_city_id in (select CU_city_id
from CU_city ))
and B.BillDate Between '20160101' and '20171231'
order by B.CU_order_amount desc
金额 地区 日期
--------------------------------------------------
700 台北市 20170105
300 新北市 20160501
200 台北市 20170509
200 新北市 20170901
500 台北市 20160405
请问一下如果我要将这段查询的结果 再针对地区分组 且把金额加总该怎麽做?
因为我已经跨表取值了,不知道怎麽针对已经查询到的结果再作group by 和 sum。
再麻烦大家帮我看一下,谢谢。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 113.196.182.169
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1504717122.A.9F2.html
1F:推 criky: 把查询结果当子查询表格 09/07 07:34
2F:→ nd5287: 所以是再用一个Select最後 from 把第一次的查询结果()起 09/07 07:52
3F:→ nd5287: 吗? 09/07 07:52
4F:→ Duncan101: select xxzxx from ( subtable) where xxxx 02/11 06:25