2013-10-25 71 views
0

我有2個「訂單」和「客戶」表。SQL查詢按客戶類型獲得總訂單(計數)

我想根據客戶類型在每個月,每週和每個訂單的數量。

訂單表具有orderid和CustomerIDfields。

Customers表具有CustomerID和CustomerTypeID字段。

我曾嘗試下面的查詢: -

SELECT 
       "Month" = month(o.OrderDate) 
       , "Year" = year(o.OrderDate) 
       , NoOfCustomer = Count(o.Total)      
      FROM 
       Orders o   
       INNER JOIN 
       Customers C on C.CustomerID = o.CustomerID       
      WHERE 
       o.OrderDate >= convert(DATETIME, '1/1/2013 12:00:00 AM') 
       AND o.OrderDate < convert(DATETIME, '12/31/2013 12:00:00 AM') 
      GROUP BY 
       month(o.OrderDate) 
       , year(o.OrderDate) 

      ORDER BY 
       year(o.OrderDate) 
       , month(o.OrderDate) 

我想導致類似下面: -

Month Year NoOfCustomer CustomerType 
1  2013 45625   1 
2  2013 12131   2 
3  2013 54544   3 
4  2013 7888   4 
1  2013 5652   1 
2  2013 5655   2 
3  2013 5522   3 
4  2013 555   4 

在此先感謝。

+0

你與查詢 – Armand

+0

從我看到你應該子句添加客戶類型進行分組太 – Raphael

回答

0

我找到了解決辦法,感謝所有爲您的幫助和時間: -

SELECT 2013 as [Year], 
     months.number, 
     Amount = SUM(COALESCE(o.Total,0)), 
     C.CustomerTypeID 
FROM Customers C 
CROSS JOIN 
(SELECT number FROM master..spt_values WHERE type='p' and number between 1 and 12) months 
LEFT JOIN [Orders] o on C.CustomerId = o.CustomerId and YEAR(o.OrderDate) = 2013 and MONTH(o.OrderDate) = months.number 
GROUP BY months.number, C.CustomerTypeID 
ORDER BY months.number, C.CustomerTypeID 
0

也許這將幫助

DECLARE @DateStart DATETIME, @DateEnd DATETIME; 
    SET @DateStart = CONVERT(DATETIME, '1/1/2013 12:00:00 AM'); 
    SET @DateEnd = CONVERT(DATETIME, '12/31/2013 12:00:00 AM'); 

    SELECT 
    MONTH(o.OrderDate) [Month] 
    , YEAR(o.OrderDate) [Year] 
    , ISNULL(C.CustomerType,0) [CustomerType] 
    , COUNT(o.Total) [NoOfCustomer]     
    FROM Orders o   
    LEFT OUTER JOIN Customers C on C.CustomerID = o.CustomerID       
    WHERE o.OrderDate BETWEEN @DateStart AND @DateEnd 
    GROUP BY MONTH(o.OrderDate), YEAR(o.OrderDate), ISNULL(C.CustomerType,0) 
    ORDER BY [Year],[Month],[CustomerType]; 

UPDATE

DECLARE @DateStart DATETIME, @DateEnd DATETIME; 
    DECLARE @CustTypeTable TABLE(CustomerType INT); 
    DECLARE @i INT = 0; 
    /*Create table variable for customer types*/ 
    WHILE @i < 5 
    BEGIN 
     INSERT INTO @CustTypeTable 
     SELECT @i 
     SET @i = @i + 1 
    END 

    /*The statement will force all CustomerTypes with a "fake" table*/ 
    DECLARE @StagingTable TABLE([Month] INT, [Year] INT, CustomerType INT) 
    INSERT INTO @StagingTable 
    SELECT 
    MONTH(o.OrderDate) [Month] 
    , YEAR(o.OrderDate) [Year] 
    , T.CustomerType 
    FROM Orders o 
    CROSS JOIN @CustTypeTable T 
    GROUP BY MONTH(o.OrderDate), YEAR(o.OrderDate), T.CustomerType 


    SET @DateStart = CONVERT(DATETIME, '1/1/2013 12:00:00 AM'); 
    SET @DateEnd = CONVERT(DATETIME, '12/31/2013 12:00:00 AM'); 

    /*Create another staging table for results*/ 
    DECLARE @StagingTable2 TABLE([Month] INT, [Year] INT, CustomerType INT, [NoOfCustomer] BIGINT) 
    INSERT INTO @StagingTable2 
    SELECT 
    MONTH(o.OrderDate) [Month] 
    , YEAR(o.OrderDate) [Year] 
    , C.CustomerType [CustomerType] 
    , COUNT(o.Total) [NoOfCustomer]     
    FROM Orders o   
    LEFT OUTER JOIN Customers C on C.CustomerID = o.CustomerID       
    WHERE o.OrderDate BETWEEN @DateStart AND @DateEnd 
    GROUP BY MONTH(o.OrderDate), YEAR(o.OrderDate), C.CustomerType 
    ORDER BY [Year],[Month],[CustomerType]; 

    /*Now lets join them up*/ 
    SELECT 
    T1.Year 
    , T1.Month 
    , T1.CustomerType 
    , T2.[NoOfCustomer] 
    FROM @StagingTable T1 
    LEFT OUTER JOIN @StagingTable2 T2 ON T2.Year = T1.Year AND T2.Month = T1.Month AND T2.CustomerType = T1.CustomerType 

大約相當於這就是我能爲你做

+0

感謝@Adrian得到什麼結果,這是非常接近。 http://www.evernote.com/shard/s364/sh/81461f09-38b2-42d4-bdec-c3017d4a1672/944ff1f6e7f6915344ff0fdebae2b2d4,customertype 4對於第4個月沒有任何價值,我們可以顯示0作爲它的默認值嗎? – AnandMeena

+0

更改JOIN並添加ISNULL –

+0

忘了更改GROUP by以包含ISNULL,現在應該工作 –

2

試試這個:

SELECT MONTH(o.OrderDate) as MonthValue, 
     YEAR(o.OrderDate) as YearValue, 
     C.CustomerType, Count(o.Total) as NoOfOrders 
FROM Orders o   
INNER JOIN Customers C on C.CustomerID = o.CustomerID       
WHERE o.OrderDate >= CONVERT(DATETIME, '1/1/2013 00:00:00 AM') 
AND o.OrderDate <= CONVERT(DATETIME, '12/31/2013 23:59:59 PM') 
GROUP BY MONTH(o.OrderDate), 
     YEAR(o.OrderDate), 
     C.CustomerType 
ORDER BY MONTH(o.OrderDate), 
     YEAR(o.OrderDate) 

UP日期:

如果你想獲得導致每月爲每一位客戶類型,那麼你需要添加UNION象下面這樣:

; with CTE AS 
(
     SELECT MONTH(CAST('01/01/2013' AS DateTime)) [Month], 2013 [Year], CustomerType, 0 NoOfCustomers 
     FROM Customers 
     UNION ALL 
     SELECT [MONTH] + 1, 2013 [Year], CustomerType, 0 NoOfCustomers 
     FROM CTE 
     WHERE [Month] <= 12 
     AND CustomerType NOT IN 
     (
      SELECT C.CustomerType 
      FROM [Orders] O INNER JOIN Customers C ON C.CustomerID = o.CustomerID  
      WHERE MONTH(O.OrderDate) = CTE.[Month] AND YEAR(O.OrderDate) = 2013 
     ) 
) 

SELECT * FROM 
(
    SELECT DISTINCT [Month], [Year], CustomerType, NoOfCustomers FROM CTE 
    UNION 
    SELECT MONTH(o.OrderDate) as [Month], 
      2013 as [Year], 
      C.CustomerType, 
      COUNT(o.Total) as NoOfCustomers 
    FROM [Orders] o   
    INNER JOIN Customers C on C.CustomerID = o.CustomerID 
    WHERE YEAR(o.OrderDate) = 2013 
    GROUP BY MONTH(o.OrderDate), C.CustomerType 
    ) tt 
ORDER BY [Month], [Year], CustomerType 
+0

謝謝@Upendra,但它給錯誤「關鍵字'ORDER'附近的語法不正確。「 – AnandMeena

+0

對不起,一個逗號在那裏,我已經更新了答案,現在就來試試。我也有日期時間改爲比較,你中午12點爲12月31日之後,錯過了記錄和中午12點之前的1月1日 –

+0

感謝@Upendra,據工作好的,只需要一個,請看http://www.evernote.com/shard/s364/sh/a4f44f1c-2f0a-4ace-9005-a1af205d298c/e14d43ed26f9886378cbdacf5789ec99,customertype 4對於第4個月沒有任何價值,我們可以顯示0作爲其默認的? – AnandMeena