本文介紹了在不使用 CTE 的情況下在結果集中按列獲取分組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
這是以下查詢
select CAST(DepositeDate AS DATE) as DepositeDate,SUM(TotalAmount) as Amount
from Payment
group by DepositeDate
我得到以下結果.
DepositeDate | Amount |
2021-04-30 | 50 |
2021-04-30 | 50 |
2021-04-26 | 75 |
2021-04-11 | 30 |
2021-04-11 | 30 |
但我需要以下結果
DepositeDate | Amount |
2021-04-30 | 100 |
2021-04-26 | 75 |
2021-04-11 | 60 |
這可以不使用 CTE 嗎?
Is this possible without using CTE?
推薦答案
顯然,您的 DepositeDate
列有一個時間組件.重復GROUP BY
中的表達式:
Apparently, your DepositeDate
column has a time component. Repeat the expression in the GROUP BY
:
select CAST(DepositeDate AS DATE) as DepositeDate,
SUM(TotalAmount) as Amount
from Payment
group by CAST(DepositeDate AS DATE)
order by CAST(DepositeDate AS DATE);
SQL Server 無法識別 GROUP BY
中的別名.所以 DepositeDate
總是會引用在 FROM
子句中定義的列,而不是 select
中的表達式.
SQL Server does not recognize aliases in the GROUP BY
. So DepositeDate
is always going to refer to a column defined in the FROM
clause and not the expression in the select
.
這篇關于在不使用 CTE 的情況下在結果集中按列獲取分組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!