我在查詢時遇到困難,我有大量的訂單,我正在從中提取月份和年份,並且需要將每個月內的所有價格總和和一年。我可以想出一個解決方案的唯一方法是遍歷並進行大量昂貴的比較,這似乎破壞了關係數據庫的目的。我正在查詢價格,因爲折扣經常應用於小計。我有所有必要的表加入並匹配,但由於價格計算,我不能使用內置的SUM()函數。先謝謝你!在某個月發生的訂單總結價格
編輯:這是代碼,顯然我今天超級分散。這絕對是對我,謝謝你陪我住:
`SELECT dbo.SalesOrder.SalesOrderNo
,dbo.SalesOrder.CustomerID
,(dbo.InvoiceDetails.ExtendedPrice * (1 - dbo.Invoice.DiscountPct/100)) AS [Price]
,dbo.Customers.NAME AS [Customer Name]
,dbo.Customers.SalesRepID
,dbo.Employees.FirstName + dbo.Employees.LastName AS [SalesRepName]
,dbo.Estimate.DateCreated AS [Date Quoted]
,dbo.SalesOrder.OrderDate AS [Date Ordered]
,dbo.Invoice.BillingDate AS [Date Invoiced]
,MONTH(dbo.Estimate.DateCreated) AS [Month Quoted]
,MONTH(dbo.SalesOrder.OrderDate) AS [Month Ordered]
,MONTH(dbo.Invoice.BillingDate) AS [Month Invoiced]
,YEAR(dbo.Estimate.DateCreated) AS [Year Quoted]
,YEAR(dbo.SalesOrder.OrderDate) AS [Year Ordered]
,YEAR(dbo.Invoice.BillingDate) AS [Year Invoiced]
FROM SalesOrder
INNER JOIN Invoice ON Invoice.SalesOrderID = SalesOrder.SalesOrderID
INNER JOIN InvoiceDetails ON InvoiceDetails.InvoiceID = Invoice.InvoiceID
INNER JOIN Customers ON Customers.CustomerID = SalesOrder.CustomerID
INNER JOIN SalesReps ON Customers.SalesRepID = SalesReps.SalesRepID
INNER JOIN Employees ON Employees.EmployeeID = SalesReps.EmployeeID
INNER JOIN Estimate ON Estimate.EstimateID = SalesOrder.EstimateID
WHERE dbo.Invoice.Approved = '1'
AND (YEAR(dbo.Invoice.BillingDate) >= YEAR(GETDATE()) - 1)
AND (YEAR(dbo.Estimate.DateCreated) >= YEAR(GETDATE()) - 1)
AND (YEAR(dbo.SalesOrder.OrderDate) >= YEAR(GETDATE()) - 1)`
不知道是什麼問題,但只是總結價格和按月份分組應該沒問題。 –
如果您發佈了一個示例架構以及一些示例數據,它將會很有用。但這似乎是一個非常簡單的問題,所以我想知道是否有什麼不明顯的情況發生? – pmbAustin
您能提供樣本輸入數據集和預期輸出嗎? –