2017-04-03 65 views
1

我在查詢時遇到困難,我有大量的訂單,我正在從中提取月份和年份,並且需要將每個月內的所有價格總和和一年。我可以想出一個解決方案的唯一方法是遍歷並進行大量昂貴的比較,這似乎破壞了關係數據庫的目的。我正在查詢價格,因爲折扣經常應用於小計。我有所有必要的表加入並匹配,但由於價格計算,我不能使用內置的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)` 
+0

不知道是什麼問題,但只是總結價格和按月份分組應該沒問題。 –

+0

如果您發佈了一個示例架構以及一些示例數據,它將會很有用。但這似乎是一個非常簡單的問題,所以我想知道是否有什麼不明顯的情況發生? – pmbAustin

+0

您能提供樣本輸入數據集和預期輸出嗎? –

回答

0

沒有任何其他信息,是有一個原因,你不能做這樣的事?

SELECT YEAR(ORDER_DATE) AS [Year], MONTH(ORDER_DATE) AS [Month], SUM(AMOUNT) AS [Total] 
    FROM Orders 
GROUP BY YEAR(ORDER_DATE), MONTH(ORDER_DATE) 
ORDER BY YEAR(ORDER_DATE), MONTH(ORDER_DATE) 

假設表名是「訂單」,它有列「ORDER_DATE」爲一個日期時間,和「量」的數值。


基礎上顯示的價格計算您更新的查詢,身邊的事簡單的方式就是建立一個CTE(公共表表達式)從現有的查詢,然後選擇/ GROUP BY的.. 。是這樣的:

WITH sales AS (
    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) 
) 
SELECT [Month Ordered], [Year Ordered], SUM([Price]) AS TotalOrderedPrice 
    FROM Sales 
GROUP BY [Month Ordered], [Year Ordered] 
ORDER BY [Month Ordered], [Year Ordered] 

這將創建一個名爲「銷售」使用原始查詢verbatum CTE,然後使用在選擇CTE,在底部做分組和排序上(包括求和計算出的價格)關於該查詢的結果。這是一種簡單的'語法糖',它使得表達這種東西比如果你不得不重做查詢來總結計算值或子選擇或者你有什麼要容易得多,並且是一種有用的技巧。

+0

問題是我正在計算查詢中的價格,因爲它不在數據庫中,我被禁止修改它。它不會讓我在語句中計算成本,不幸的是,我不熟悉SQL。我不確定嵌套的SELECT語句是否會修復它,或者如果有更好的選項,更不用說如何格式化它。 – Kishi

+0

嗯,我甚至不知道你在說什麼,所以提供一個例子,顯示你已經或已經嘗試過的SQL,模式等等,將會非常有用。我在這裏回答了你問的問題。顯然你想問一個不同的問題,所以你應該編輯你的問題更準確。 – pmbAustin

+0

價格是在我寫的查詢中計算的,我有一個小計和折扣率,我正在計算成最終價格:'(dbo.InvoiceDetails.ExtendedPrice *(1 - dbo.Invoice.DiscountPct/100))AS [價格]'這似乎阻止我把SUM()放在價格上,從我所知道的。我非常感謝你的回答,因爲它解決了我對查詢的一些擔憂,並且我很抱歉留下了這些細節(我想坐在這裏試圖弄清楚這個問題太久了)。有沒有辦法在同一個SELECT語句中引用價格? – Kishi

相關問題