作者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://www.ptt.cc/bbs/Database/M.1504717122.A.9F2.html
推 criky: 把查詢結果當子查詢表格 09/07 07:34
→ nd5287: 所以是再用一個Select最後 from 把第一次的查詢結果()起 09/07 07:52
→ nd5287: 嗎? 09/07 07:52
→ Duncan101: select xxzxx from ( subtable) where xxxx 02/11 06:25