SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


timeline in cross-tab??


timeline in cross-tab??

Author
Message
Steve JP
Steve JP
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 Visits: 328
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.
r_slot
r_slot
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 176
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
Steve JP
Steve JP
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 Visits: 328
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..
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85655 Visits: 41082
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
r_slot
r_slot
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 176
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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85655 Visits: 41082
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Luis Cazares
Luis Cazares
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: 16442 Visits: 19086
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
r_slot
r_slot
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 176
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
Luis Cazares
Luis Cazares
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: 16442 Visits: 19086
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
r_slot
r_slot
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 176
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
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