Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Server Pivot tables Expand / Collapse
Author
Message
Posted Thursday, December 27, 2012 1:18 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, August 7, 2014 4:24 PM
Points: 70, Visits: 195
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!!
Post #1400732
Posted Thursday, December 27, 2012 1:29 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:32 PM
Points: 13,302, Visits: 12,168
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)
Post #1400735
Posted Thursday, December 27, 2012 6:25 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, June 16, 2014 5:32 AM
Points: 55, Visits: 158
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.
Post #1400766
Posted Thursday, December 27, 2012 7:50 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, August 7, 2014 4:24 PM
Points: 70, Visits: 195
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!
Post #1400778
Posted Thursday, December 27, 2012 8:26 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, June 16, 2014 5:32 AM
Points: 55, Visits: 158
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?
Post #1400783
Posted Friday, December 28, 2012 1:56 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Sunday, August 24, 2014 5:44 AM
Points: 1,125, Visits: 1,589
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
Post #1400828
Posted Friday, December 28, 2012 5:41 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, August 7, 2014 4:24 PM
Points: 70, Visits: 195
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!
Post #1401100
Posted Friday, December 28, 2012 5:42 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, August 7, 2014 4:24 PM
Points: 70, Visits: 195
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.
Post #1401101
Posted Friday, December 28, 2012 10:19 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Sunday, August 24, 2014 5:44 AM
Points: 1,125, Visits: 1,589
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
Post #1401124
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse