Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Server Pivot tables


SQL Server Pivot tables

Author
Message
SJanki
SJanki
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 224
I have a table that keeps track of a store open, closed, reopen, closed (again) , reopen(again) etc dates.
The table has StoreId , AuditTypeId (Open, Close, Reopen) and a date.

I want a report/TSQL that will give me a result like-->

StoreId, Open Date, Closed Date , ReOpen Date, Closed Date 2, ReOpenDate 2 .......

There can be no Reopen & Re-Close for a store or the store could have been closed twice and reopened twice etc.

Could you please give me an idea how to write the query to get this result.

Thanks!!
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16568 Visits: 17016
SJanki (12/27/2012)
I have a table that keeps track of a store open, closed, reopen, closed (again) , reopen(again) etc dates.
The table has StoreId , AuditTypeId (Open, Close, Reopen) and a date.

I want a report/TSQL that will give me a result like-->

StoreId, Open Date, Closed Date , ReOpen Date, Closed Date 2, ReOpenDate 2 .......

There can be no Reopen & Re-Close for a store or the store could have been closed twice and reopened twice etc.

Could you please give me an idea how to write the query to get this result.

Thanks!!


This is not as simple as it seems. It sounds like you need a dynamic solution where the number of columns is unknown. Please refer to the 2 articles in my signature about cross tabs. The seconds one about dynamic cross tabs is exactly what you need for this sort of thing.

Of course if this is a report, doing it in the front end is likely to perform faster than doing this in sql.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
eklavu
eklavu
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 173
Looks like a challenging solutions and I want to try it. I'm not busy today because my boss is on holiday (yehey!!!) :-).Can you post some data and sample output so I can work something for today.
SJanki
SJanki
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 224
eklavu (12/27/2012)
Looks like a challenging solutions and I want to try it. I'm not busy today because my boss is on holiday (yehey!!!) :-).Can you post some data and sample output so I can work something for today.


Hi,
Please find the example data-->
Master Table-->Store_Audit_Type
Audit_Type_Id Audit_Type_Description
1 New
2 Closed
3 ReOpen


Data in the table
StoreId Audit_Type_Id CreatedDate
1 1 1/1/2012
1 2 4/1/2012
1 3 6/1/2012
1 2 11/1/1012
2 1 1/5/2012
2 2 5/10/2012
3 1 12/1/2012


in the above example, Store 1 was newly opened on 1/1/2012 , then was closed on 4/1/2012.
It was Reopened on 6/1/2012 and again closed on 11/1/2012
Store 2 was newly opened on 1/5/2012, then closed on 5/10/2012
Store 3 was opened on 12/1/2012 ( this one is not yet closed )

Hope this will help to understand the scenario.

Thank you!!
Good luck!
eklavu
eklavu
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 173
I still don't understand some of the requirements.

StoreId, Open Date, Closed Date , ReOpen Date, Closed Date 2, ReOpenDate 2 .......

There can be no Reopen & Re-Close for a store or the store could have been closed twice and reopened twice etc.



you said There can be no Reopen. Why is it that there is a ReOpen Date field in your report?
vinu512
vinu512
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1149 Visits: 1618
Is this what you are looking for?


--Creating Tables

Create Table Store_Audit_Type
(
Audit_Type_Id Int,
Audit_Type_Description NVarchar(30)
)

Create Table Store_Audit_Details
(
StoreId Int,
Audit_Type_Id Int,
CreatedDate Date
)


--Inserting Sample Data

Insert Into Store_Audit_Type
Select 1, 'New'
Union ALL
Select 2, 'Closed'
Union ALL
Select 3, 'ReOpen'

Insert Into Store_Audit_Details
Select 1, 1, '2012/01/01'
Union ALL
Select 1, 2, '2012/04/01'
Union ALL
Select 1, 3, '2012/06/01'
Union ALL
Select 1, 2, '2012/08/01'
Union ALL
Select 1, 3, '2012/10/01'
Union ALL
Select 1, 2, '2012/11/01'
Union ALL
Select 2, 1, '2012/01/05'
Union ALL
Select 2, 2, '2012/05/10'
Union ALL
Select 3, 1, '2012/12/01'


--Dynamic Pivot

Declare @sql NVarchar(MAX), @cols NVarchar(MAX)
Select @cols = STUFF((Select DISTINCT ',MAX(Case When rn = ''' + rn + ''' Then CreatedDate Else NULL End) As ' + rn
From (Select DISTINCT Audit_Type_Description + Cast(ROW_NUMBER() Over (Partition By StoreId, Audit_Type_Description Order By StoreId) As Varchar) As rn
From Store_Audit_Details As a
JOIN Store_Audit_Type As b ON a.Audit_Type_Id = b.Audit_Type_Id ) As q FOR XML PATH('')),1,1,'')
Set @sql = 'Select StoreId, ' + @cols
+ ' From
(
Select StoreId, Audit_Type_Description, CreatedDate, Audit_Type_Description + Cast(ROW_NUMBER() Over (Partition By StoreId, Audit_Type_Description Order By StoreId) As Varchar) As rn
From Store_Audit_Details As a
JOIN Store_Audit_Type As b ON a.Audit_Type_Id = b.Audit_Type_Id
) As p
Group By StoreId'
Execute (@sql)



Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
SJanki
SJanki
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 224
vinu512 (12/28/2012)
Is this what you are looking for?
[/code]


Thank you Venu512, This was the quick help I really needed to produce the year end report!
SJanki
SJanki
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 224
This is not as simple as it seems. It sounds like you need a dynamic solution where the number of columns is unknown. Please refer to the 2 articles in my signature about cross tabs. The seconds one about dynamic cross tabs is exactly what you need for this sort of thing.
Of course if this is a report, doing it in the front end is likely to perform faster than doing this in sql.


Thank you Seane. Really useful articles.
vinu512
vinu512
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1149 Visits: 1618
SJanki (12/28/2012)
vinu512 (12/28/2012)
Is this what you are looking for?
[/code]


Thank you Venu512, This was the quick help I really needed to produce the year end report!


You're Welcome....By the way the name's Vinu not Venu. :-)

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search