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 ««123»»

timeline in cross-tab?? Expand / Collapse
Author
Message
Posted Wednesday, April 10, 2013 3:12 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Sunday, August 17, 2014 4:06 AM
Points: 51, Visits: 289
A slight alternative that might be easier to understand:

Sample data:

If Object_Id('TempDB..#Editions', 'U') Is Not Null
Drop Table #Editions

Create Table #Editions
(
Edition_ID Int Primary Key Clustered,
Edition_Year int,
Edition_Start date ,
Edition_Finish date
)

Set Dateformat DMY

Insert into #Editions
(Edition_ID, Edition_Year, Edition_Start, Edition_Finish)
Select '1', '2012', '27-07-2012', '11-08-2012' UNION ALL
Select '2', '2008', '07-08-2008', '23-08-2008'

-- second table #Events

If Object_Id('TempDB..#Events', 'U') Is Not Null
Drop Table #Events

Create Table #Events
(
Event_ID Int Primary Key Clustered,
Event_Name nvarchar(10),
Event_Year int,
Event_Start date ,
Event_Finish date
)
Set Dateformat DMY

Insert into #Events (Event_ID, Event_Name, Event_Year, Event_Start, Event_Finish)
Select '1', 'AA', '2012', '04-08-2012','07-08-2012' UNION ALL
Select '2', 'BB', '2012', '07-08-2012','10-08-2012' UNION ALL
Select '3', 'CC', '2012', '04-08-2012','06-08-2012' UNION ALL
Select '4', 'DD', '2012', '31-07-2012','01-08-2012' UNION ALL
Select '5', 'EE', '2012', '27-07-2012','03-08-2012' UNION ALL
Select '6', 'AA', '2008', '17-08-2008','20-08-2012' UNION ALL
Select '7', 'BB', '2008', '12-08-2008','13-08-2012' UNION ALL
Select '8', 'CC', '2008', '20-08-2008','20-08-2012' union all
Select '9', 'ZZ', '2012', '12-08-2012','12-08-2012' --- extra row to prove that gaps work




The following will only work for a defined year:


Declare @SelectedYear char(4)
, @SqlCmd varchar(8000)
, @Columns varchar(8000)
, @min_Date date
, @max_Date date;

set @SelectedYear = '2012';

-- Work out the Data range
select @min_Date = min( Event_Start )
, @max_Date = max ( Event_Finish )
from #Events
where Event_Year = @SelectedYear ;

-- Create a table to hold each day offset
Declare @Tally table ( Number int )

insert @Tally ( Number )
select Number
from [master].[dbo].[spt_Values]
where Number between 0 and ( select datediff( dd , @min_Date , @max_Date ) )



-- Build up a column list that will determine whether or not the event occurs on that day
Set @Columns = '' ;


Select @Columns = @Columns + ' , CASE WHEN ''' + Event_Day +''' '
+ 'BETWEEN [Event_Start] AND [Event_Finish] THEN ''X'' ELSE '''' END AS ['+Event_Day_Label+']' + char(13) + char(10)
from (
Select distinct top 100 percent
Event_Day = cast( dateAdd ( dd , Number , @min_Date ) as varchar(20) )
, Event_Day_Label = convert ( char(5), dateAdd ( dd , Number , @min_Date ) , 105 )
, Number
from @Tally
order by Number
) as x


--- Create a string that holds the query
Select @SqlCmd =
'Select Event_Name , Event_Year ' + char(13) + char(10)
+ @Columns
+ 'from #Events ' + char(13) + char(10)
+ 'where Event_Year = ' + @SelectedYear + char(13) + char(10)
+ 'order by Event_Name' + char(13) + char(10)


-- Show the dynamic query
print @SqlCmd


-- Run the query
exec ( @SqlCmd )




Hope this helps.
Post #1440706
Posted Wednesday, April 10, 2013 4:56 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, March 3, 2014 12:25 AM
Points: 73, Visits: 158
Thanks a lot Steve: I understand you solution a little bit better than the others, but the subject stays difficult for me. When I look into the messages as a result of running your solution I see a perfect (dynamic) table. Is it possible to export that table to a webpage (eg Visual Web .aspx)? I want to show the results in a gridview, but the SQL-variable is of course no table with rows. In fact is your solution in the end a table and could be the final product of a stored procedure.
The (silly) question I still have is: is the above mentioned possible or am I talking rubbish?

Any answers/reactions would be appreciated.
Above all: compliments for your solution - I gonna try to understand it.

Robert
Post #1440731
Posted Wednesday, April 10, 2013 5:35 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Sunday, August 17, 2014 4:06 AM
Points: 51, Visits: 289
To turn into a stored procedure then its simple:

create proc sp_GetEventOccurances @SelectedYear	char(4) 
as
begin
Declare @SqlCmd varchar(8000)
, @Columns varchar(8000)
, @min_Date date
, @max_Date date;



-- Work out the Data range
select @min_Date = min( Event_Start )
, @max_Date = max ( Event_Finish )
from #Events
where Event_Year = @SelectedYear ;

-- Create a table to hold each day offset
Declare @Tally table ( Number int )

insert @Tally ( Number )
select Number
from [master].[dbo].[spt_Values]
where Number between 0 and ( select datediff( dd , @min_Date , @max_Date ) )



-- Build up a column list that will determine whether or not the event occurs on that day
Set @Columns = '' ;


Select @Columns = @Columns + ' , CASE WHEN ''' + Event_Day +''' '
+ 'BETWEEN [Event_Start] AND [Event_Finish] THEN ''X'' ELSE '''' END AS ['+Event_Day_Label+']' + char(13) + char(10)
from (
Select distinct top 100 percent
Event_Day = cast( dateAdd ( dd , Number , @min_Date ) as varchar(20) )
, Event_Day_Label = convert ( char(5), dateAdd ( dd , Number , @min_Date ) , 105 )
, Number
from @Tally
order by Number
) as x


--- Create a string that holds the query
Select @SqlCmd =
'Select Event_Name , Event_Year ' + char(13) + char(10)
+ @Columns
+ 'from #Events ' + char(13) + char(10)
+ 'where Event_Year = ' + @SelectedYear + char(13) + char(10)
+ 'order by Event_Name' + char(13) + char(10)


-- Show the dynamic query
print @SqlCmd


-- Run the query
exec ( @SqlCmd )

end

Then to call it:
    sp_GetEventOccurances @SelectedYear = '2012'

You can certainly call a stored procedure via ado.net or Linq

Iterate through the result set and add columns dynamically and then populate the cells in a gridview.

An alternative is if you know that all the events last X days then you can then use a pivot table or a fixed query to return the data. Make it easy to write and code but you will not have the flexability when events are extended.


Hope this helps..
Post #1440743
Posted Wednesday, April 10, 2013 5:56 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:47 PM
Points: 36,947, Visits: 31,452
r_slot (4/9/2013)
To all repliers,

Due to the time difference I just saw your proposals: I must say they look great! I am going to examine them and I hope to give a reaction to you all today (is your night).
I hope that Jeff Moden is not lost on his way home. The other possibility is that he was so impressed by the given replies that his reply got redundant.
Who knows?

That some of my data were wrong surprises me because I tested them before sending to sql server central. I am sorry for the inconvenience.

Thanks to you all.

Robert


My apologies. Right after I made the promise, we got nailed with a string of problems at work. I worked until 2:30 in the morning that first night and until 11:30 last night.

I'm taking a look at the solutions offered now. You've got some good people working on it.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1440751
Posted Wednesday, April 10, 2013 6:02 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, March 3, 2014 12:25 AM
Points: 73, Visits: 158
Jeff,

I was just kidding of course: there are no obligations what so ever related to this kind of forums.

I agree: I have got some great solutions which are, that is to me, difficult to understand. The problem is (see Steve's posts) that at the end a cross tab is produced in sql server but presented in most cases as a variable which is then executed and the result is perfect in SQL Server environment. The last step to me is: how to export the table to visual web developer or better a webpage? I want to populate a gridview with the results and the necessary input is then a table. That table should be offered via a stored procedure (Steve produced one) and transferred to a webpage.
In a normal crosstab the table itself is to be seen in the stored procedure, but in this case not. May be because of the dynamic character because the events and the 'X' with dates differ by edition.
This is a bit beyond sql server, but may be you recognise this problem. If not, I have to try it somewhere else. I still think (and hope) that the solution can be offered in Sql Server in producing a stored procedure with a table result. Unfortunately I am to much a newbee (or something else) to figure it our myself...

Grz,

Robert
Post #1440752
Posted Wednesday, April 10, 2013 6:11 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:47 PM
Points: 36,947, Visits: 31,452
Looking through the solutions, I'd have come up with pretty much the same thing as what Luis did. He also came up with it at about the same time that I would have had I been able to keep my promise.

Not sure if you still need an explanation but his code puts together a classic "Cross Tab" using some date driven dynamic SQL. He uses the Tally Table, which is a simple table of sequential numbers, to build some dates and to control how many aggregated case statements (one for each display column and does the "pivot").

The article at the link that previously provided explains much more.

Well done everyone and thank you very much for a much needed cover.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1440759
Posted Wednesday, April 10, 2013 9:36 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:19 PM
Points: 3,499, Visits: 7,549
r_slot (4/10/2013)
Thanks a lot Steve: I understand you solution a little bit better than the others, but the subject stays difficult for me. When I look into the messages as a result of running your solution I see a perfect (dynamic) table. Is it possible to export that table to a webpage (eg Visual Web .aspx)? I want to show the results in a gridview, but the SQL-variable is of course no table with rows. In fact is your solution in the end a table and could be the final product of a stored procedure.
The (silly) question I still have is: is the above mentioned possible or am I talking rubbish?

Any answers/reactions would be appreciated.
Above all: compliments for your solution - I gonna try to understand it.

Robert


To answer the question
Yes, it's possible to do it. Unfortunately, I don't know how to do it. A developer in a previous job used that in the web app, but I'm not sure how he did it.
If you find it easier to have a physical table, you can use SELECT...INTO to create a table.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1440898
Posted Wednesday, April 10, 2013 10:12 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, March 3, 2014 12:25 AM
Points: 73, Visits: 158
Luis,

A Select Into will do, but where in your code does it appear. In between in place of something else or at the end but with what variables?
It sounds stupid, but I have no idea: too much standarised thinking or no thinking at all.
Can you give me a hint once more?
I would appreciate that very much.

Robert
Post #1440923
Posted Wednesday, April 10, 2013 10:18 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:19 PM
Points: 3,499, Visits: 7,549
Check the code and look for the part where the sql statement is completed and stored into @SQL.
In there, you just have to add the INTO clause.

You might want to drop the table before creating it and check if it exists before dropping it.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1440925
Posted Wednesday, April 10, 2013 11:44 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, March 3, 2014 12:25 AM
Points: 73, Visits: 158
I tried the following (after making a table calles Results):

SELECT @SQL = String FROM SQL_String

SET @SQL = 'SELECT ev.Event_Name INTO dbo.Results' + CHAR(10) + @SQL +
'FROM #Events ev ' + CHAR(10) +
'WHERE ev.event_year = @Year '

PRINT @SQL

EXEC sp_executesql @SQL, N'@Year int', @Year = @Edition_Year

but that does not work at all. The table has two fields: Event_Name and @SQL.
I also tried the following:

SET @SQL = 'SELECT ev.Event_Name' + CHAR(10) + @SQL + 'INTO dbo.Results' +
'FROM #Events ev ' + CHAR(10) +
'WHERE ev.event_year = @Year '

with the same result. Nothing but errors.

What am I doing wrong?

Robert
Post #1440958
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse