2

I have a result like this from an SQL query:

Month    Day  Customer     Item
------------------------------------------
January   1   John         Pencil
January   1   Jack         ---
January   1   Steve        Stapler
January   2   John         ---
January   2   Jack         ---
January   2   Steve        VisitingCard
January   3   John         ---
January   3   Jack         Marker
January   3   Steve        ---
January   4   John         ---
January   4   Jack         ---
January   4   Steve        ---
January   5   John         Scrapbook
January   5   Jack         ---
January   5   Steve        ---
....
....
....
February  1   John         ---
February  1   Jack         ---
February  1   Steve        Marker

I'd like this to be represented in an ASP.NET gridview as below:

                             January
Customer 1 2 3 4 5 6 7 8 9 10 11 ... 25 26 27 28 29 30 31
---------------------------------------------------------
John     P - - - N - - - - -- --     -- -- -- -- -- -- --
Jack     - - M - - - - - - -- --     -- -- -- -- -- -- --
Steve    S V - - - - - - - -- --     -- -- -- -- -- -- --
                             February
Customer 1 2 3 4 5 6 7 8 9 10 11 ... 25 26 27 28
------------------------------------------------
John     - - - - - - - - - -- --     -- -- -- --
Jack     - - - - - - - - - -- --     -- -- -- --
Steve    M - - - - - - - - -- --     -- -- -- --

How can I get this done either in SQL Server 2005 or in ASP.NET 2.0?

Please give the sample query for PIVOT as it requires some aggregate function to be used.

0

4 Answers 4

4

You require the Pivot command. I'm not familiar with the command's usage, but geekswithblogs has a good tutorial on how to use it.

Sign up to request clarification or add additional context in comments.

1 Comment

The second link is (effectively) broken. It redirects to Geekswithblogs.net, the End of an Era.
3

You cannot (cleanly) achieve this using the PIVOT function as the PIVOT function requires that you predefine the columns that are output.

Since months have varying numbers of days, you would have to write 12 queries.

IMO, pivoting should always be done as part of the UI. Aggregation is an appropriate thing to do at the SQL end though.

4 Comments

I have 3 tables after joining which I get the four columns shown in the first table. I've given P, M, S, etc.,. for explaining the question here. In actual we'd be displaying small icons in the grid. (Using template columns depending upon the value, appropriate icon will be displayed. Say If the value is Pencil, a pencil icon will be displayed.)
So can a given customer have multiple items on a given date?
No. Max. One record per customer on any given date.
I'm afraid in that case the resultset you're already returned is as much as you should do on the SQL side. You'll need someone better than me at ASP.NET / Linq to help further. Personally I would do this in SSRS as a Matrix, you would say repeat [month] per page and then set the columns to be [day]. Value would [item].
1

I think you want to look at using the PIVOT command in your SQL query.

Comments

0

You might want to use a tool like DevExpress which provides you with a pivot table. You can then bind the pivot table to your data and it does the work for you.

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.