-2
爲了報告和創建報告(MS SQL SERVER 2014)的目的,該報告將比較上週同一天和上個月同一天的交易數量和金額數量,我需要一些像這樣的查詢,這是不工作:從聯盟中選擇案例
Select
Acquirer = case when Date = @Day then a.Acquirer
when Date = @LastWeekDay then b.Acquirer
when Date = @LastMonthDay then c.Acquirer
end
, RC = case when Date = @Day then a.RC
when Date = @LastWeekDay then b.RC
when Date = @LastMonthDay then c.RC
end
, Desc = case when Date = @Day then a.Desc
when Date = @LastWeekDay then b.Desc
when Date = @LastMonthDay then c.Desc
end
, Day_Tr_Count = case when Date = @Day then a.Count end
, LWD_Tr_Count = case when Date = @LastWeekDay then b.Count end
, LMD_Tr_Count = case when Date = @LastMonthDay then c.Count end
, Day_Tr_Amount = case when Date = @Day then a.Amount end
, LWD_Tr_Amount = case when Date = @LastWeekDay then b.Amount end
, LMD_Tr_Amount = case when Date = @LastMonthDay then c.Amount end
from
(select * from T1
union
select * from T2
union
select * from T3) x
group by Acquirer,RC,Desc
所有3臺擁有漂亮的結構相同:
T1,T2,T3(acquirer,RC,Desc,Count,Amount,Date).
報告應該是:
result_table(Acquirer,RC,Desc,Daily_Count,Last_Week_Day_Count,Last_Month_Count,Daily_Amount,Last_Week_Day_Amount,Last_Month_Day_Amount)
第1步:您可能需要'UNION ALL'。 – jarlh
它是如何*不工作*? –
一般的GROUP BY規則說:「如果指定了GROUP BY子句,則SELECT列表中的每個列引用都必須標識分組列或作爲set函數的參數。」 – jarlh