2017-10-06 69 views
0

select語句我正在尋找生產這兩個select語句的總和:和兩個SQL

select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV where [Account #] like 4010 

select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV where [Account #] like 4060 

我試圖把它們之間的聯盟,這隻會產生兩種結果在相同的結果窗口。

感謝您的幫助,我是一個絕對的初學者。

+0

SQL Server Management Studio中 – AdMac

+1

感謝墊,我已經添加了標籤。 – AdMac

+0

[如何在同一查詢中添加兩個選擇命令的結果]的可能重複(https://stackoverflow.com/questions/15149775/how-to-add-results-of-two-select-commands-in-same-查詢) –

回答

3

因爲記錄來自同一個表,UNION是superfluous.A簡單INOR是足夠了:

select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV 
where [Account #] in (4060, 4061) 

select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV 
where [Account #] = 4060 OR [Account #] = 4061) 
+0

非常感謝,這也適用! – AdMac

1

簡單或會做:

select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV 
    where [Account #] like 4010 OR [Account #] like 4060 
+0

謝謝,這個作品! – AdMac

1

最新錯誤:

select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV 
where [Account #] like 4010 or [Account #] like 4060 

您的第二個選擇是使用with聲明。讀到它:)

+0

'like'不應該在這裏使用。這是一個明確的條件。用'='去。 – Shawn

+0

這是亞當的要求......我們不知道他的需求是100% –

+0

是真的。但作爲一個「絕對的初學者」,Adam可能會期待'LIKE 4010'將返回'14010','4010'或'40109'。這個問題沒有說明是否需要額外的東西。無論如何,在這種情況下,'LIKE'並不像'='那麼清晰,我一直認爲正確的語法應該在答案中得到加強,特別是在與新開發人員合作時。 – Shawn

0

如果你想使用多個選擇(也許,如果其他表都參與)

select ((select sum(hours) from foo1) + 
     (select sum(hours) from foo2)) as totalHours