我想寫一個SQL查詢,將總結從以下兩個例子表總產量:SQL總和與非唯一日期
Table: CaseLots
DateProduced kgProduced
October 1, 2013 10000
October 1, 2013 10000
October 2, 2013 10000
Table: Budget
OperatingDate BudgetHours
October 1, 2013 24
October 2, 2013 24
我想輸出表如下:
TotalProduction TotalBudgetHours
30000 48
以下是我對到目前爲止的代碼:
SELECT
Sum(kgProduced) AS TotalProduction, Sum(BudgetHours) AS TotalBudgetHours
FROM
dbo.CaseLots INNER JOIN dbo.Budget ON dbo.CaseLots.DateProduced = dbo.Budget.OperatingDate
WHERE
dbo.Budget.OperatingDate BETWEEN '2013-10-01' AND '2013-10-02'
看來,查詢是雙總結在instan預算小時ces在一天內生產多個案例。我得到的表如下:
Total Production BudgetHours
30000 72
我該如何解決這個問題?
如果您只想要1行返回的數據,如果有大於1的情況,您可以使用TOP 1 – andrewb