我有一個SQL查詢,我無法以我想要的方式顯示。基本上,我們已經有了一張充滿交易的表格,我希望從今年2月份開始購買一筆交易(特定類型的交易)。下面是該查詢:顯示SELECT查詢結果 - 從相同的表 - 並排
select sum(amount) as "2011" from transactions
where transaction_type = 'registration'
and date_entered > '2011-02-01'
and date_entered < GetDate()
現在,我也想看看這些相同的交易的總和,但前一年:
select sum(amount) as "2010" from transactions
where transaction_type = 'registration'
and date_entered > '2010-02-01'
and date_entered < DateAdd(yy, -1, GetDate())
似乎我無法弄清楚是如何並行獲得這些總和。我試過UNION all
,但這些顯示在單獨的行中,而不是列。
select sum(amount) as "2011" from transactions
where transaction_type = 'registration'
and date_entered > '2011-02-01'
and date_entered < GetDate()
UNION all
select sum(amount) as "2010" from transactions
where transaction_type = 'registration'
and date_entered > '2010-02-01'
and date_entered < DateAdd(yy, -1, GetDate())
我也讀到這裏對堆棧溢出是PIVOT
可能是一種選擇,但我還沒有看到,我可以操控/調整上面查詢的例子。
有關我如何並行獲取此數據的任何建議?我確信我忽略了一些簡單的東西。
非常感謝!
你永遠只能去本年度來比較以前還是有這個方程的變量?如果沒有其他變量,那麼我不明白爲什麼你需要使用PIVOT,波希米亞的例子就足夠了。 – deutschZuid