2

I am a beginner to writing queries and have been racking my brain trying to figure out what the best approach to this is. I have created a temp table and wrote a query to give me the following data this result from the following data.

SELECT Temp.OrderType,Temp.OrderDate,COUNT(*) as prodCount
FROM Temp
GROUP BY Temp.OrderType, Temp.OrderDate
ORDER BY Temp.OrderType;

RESULTS

OrderType    OrderDate                  prodCount
1            2012-06-04 00:00:00.000    1
1            2012-06-06 00:00:00.000    1
2            2012-06-07 00:00:00.000    2
3            2012-06-05 00:00:00.000    1
3            2012-06-06 00:00:00.000    2
3            2012-06-07 00:00:00.000    1
7            2012-06-05 00:00:00.000    1
11           2012-06-07 00:00:00.000    1

How can I go about to get the data to display in this format instead with the count totals going under the date ? Date1 Date2 etc, are 6/04, 6/05, 6/06, 6/07. Any help or guidance is appreciated. Thanks you!

OrderType Date1 Date2 Date3 Date4
1            1                1
2                             2
3                1      2     1
4
7                1
11                      1

SAMPLE DATA

OrderDate                  OrderType
2012-06-06 00:00:00.000    1
2012-06-04 00:00:00.000    1
2012-06-05 00:00:00.000    7
2012-06-05 00:00:00.000    3
2012-06-06 00:00:00.000    3
2012-06-06 00:00:00.000    3
2012-06-07 00:00:00.000    3
2012-06-07 00:00:00.000    2
2012-06-07 00:00:00.000    2
2012-06-07 00:00:00.000    3
2012-06-07 00:00:00.000    11
5
  • 1
    Which db engine? SQL Server, MySQL, Oracle, etc. Commented Jun 10, 2012 at 23:20
  • 2
    What you're looking for is typically called a "pivot" or "crosstab" query. How to write that depends on which database you're using. Commented Jun 10, 2012 at 23:21
  • Also, because your table will keep acquiring dates (columns, if you writer the query as you intend) I would recommend you write your query with the Order Types as columns, and the dates as rows. Your query will be a lot simpler and will scale with your data. Commented Jun 11, 2012 at 0:21
  • Thanks all for the replies! This is a MSSQL DB. I will try the suggestion by dbrosier and check back in. Commented Jun 11, 2012 at 3:53
  • In your particular case the version of the DBMS is also important. SQL Server 2005 and later versions have a feature called PIVOT that greatly simplifies this kind of queries. Commented Jun 11, 2012 at 5:02

2 Answers 2

1

If your DB engine is MSSQL, try this:

SELECT OrderDate, 
OrderType_1 = SUM(CASE OrderType WHEN 1 THEN 1 ELSE 0 END),
OrderType_2 = SUM(CASE OrderType WHEN 2 THEN 1 ELSE 0 END ),
OrderType_3 = SUM(CASE OrderType WHEN 3 THEN 1 ELSE 0 END),
OrderType_4 = SUM(CASE OrderType WHEN 4 THEN 1 ELSE 0 END),
OrderType_7 = SUM(CASE OrderType WHEN 7 THEN 1 ELSE 0 END),
OrderType_11= SUM(CASE OrderType WHEN 11 THEN 1 ELSE 0 END)
FROM Temp
GROUP BY OrderDate
ORDER BY OrderDate
Sign up to request clarification or add additional context in comments.

2 Comments

Forgot to"end" the case statements. Oooops.
Um, yes, and your query should work in any SQL product too (or in most of them, at least). Only the output should be the other way round: dates should go as columns, types as rows. Also, it seems like the OP wants the column names (the dates) to be dynamic (well, that is my reading of the question, anyway).
1

Please try this

   DECLARE @sql varchar(4000)

    SET @sql =
        '
        SELECT  * 
        FROM    
        (
            SELECT OrderType,CONVERT(VARCHAR(10),OrderDate,112) AS OrderDate,prodCount 
            FROm t 
        )  st
        PIVOT 
        (
        SUM(prodCount)
        FOR OrderDate IN ( '
        +  
        STUFF(
            (
             SELECT ',['  + OrderDate + ']'
             FROM 
                (SELECT distinct CONVERT(VARCHAR(10),OrderDate,112) AS OrderDate  FROM t)  a
             FOR XML PATH('')
             ),1,1,'')
        + ')
        ) as pvt
        ORDER BY pvt.OrderType
        '

    PRINT @sql
    EXEC(@sql)

SQLFiddle link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.