October 8, 2013 at 8:18 am
Hi :-),
I am having problems understand the function unpivot on sql.
I have a query:
select RecordDay, ClickCount into #myclicks from dbo.clicks
where RecordDay >= getdate()-38
that the outcome will be (with the hours, minutes and seconds)
RecordDayClickCount
8/31/20131
8/31/20131
8/31/20136
8/31/20131
8/31/20131
8/31/20131
8/31/20131
8/31/20131
8/31/20131
8/31/20131
8/31/20131
8/31/20131
8/31/20131
8/31/20131
8/31/20131
8/31/20132
I want to build from my temp table something that will look like this, where the measure will be the sum of ClickCount
RecordDay8/31/20139/7/20139/14/20139/21/2013
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
I know I will need to define on the columns
datepart(weekday,getdate()) = datepart(weekday,datecreated)
in order to get the same day of the week, and I imagine I will need to do a dateadd to group it by minutes on the rows.
But I am getting extremely lost here.
Help please :w00t:
October 8, 2013 at 8:30 am
Your post is lacking any level of detail required for anybody to understand the problem, let alone the question. In order to help we will need a few things:
1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data
Please take a few minutes and read the first article in my signature for best practices when posting questions.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 8, 2013 at 8:45 am
hi,
please find the sample requested
CREATE TABLE clicks (RecordDay datetime, ClickCount INT)
GO
INSERT INTO clicks (RecordDay datetime, ClickCount INT)
VALUES(2013-10-05 13:23:44, 2)
INSERT INTO clicks (RecordDay datetime, ClickCount INT)
VALUES(2013-10-05 15:45:21, 6)
INSERT INTO clicks (RecordDay datetime, ClickCount INT)
VALUES(2013-10-05 16:45:21, 1)
INSERT INTO clicks (RecordDay datetime, ClickCount INT)
VALUES(2013-10-05 17:45:21, 12)
INSERT INTO clicks (RecordDay datetime, ClickCount INT)
VALUES(2013-10-04 18:45:21, 3)
INSERT INTO clicks (RecordDay datetime, ClickCount INT)
VALUES(2013-10-03 19:45:21, 24)
INSERT INTO clicks (RecordDay datetime, ClickCount INT)
VALUES(2013-11-02 20:45:21,3)
October 8, 2013 at 8:48 am
Excellent. Now what I don't understand is what do you want from this sample data?
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 8, 2013 at 8:50 am
For anybody else coming along here is the sample data in a format that is consumable.
CREATE TABLE clicks (RecordDay datetime, ClickCount INT)
GO
INSERT INTO clicks (RecordDay, ClickCount)
select '2013-10-05 13:23:44', 2 union all
select '2013-10-05 15:45:21', 6 union all
select '2013-10-05 16:45:21', 1 union all
select '2013-10-05 17:45:21', 12 union all
select '2013-10-04 18:45:21', 3 union all
select '2013-10-03 19:45:21', 24 union all
select '2013-11-02 20:45:21',3
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 8, 2013 at 9:03 am
i am trying to get
on the columns the dates
on the rows the hours (from those dates)
and the sum of the clicks as a measure.
the same i gave has a few lines but the original table has over a million.
i will keep in mind your notes on how to post.
thanks for the help
October 8, 2013 at 9:28 am
astrid 69000 (10/8/2013)
i am trying to geton the columns the dates
on the rows the hours (from those dates)
and the sum of the clicks as a measure.
the same i gave has a few lines but the original table has over a million.
i will keep in mind your notes on how to post.
thanks for the help
I am still a little bit confused as to what you want as output. Can you provide the details of your desired output based on the sample data you provided?
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 8, 2013 at 9:37 am
Here's something for you to start with. To understand this method called CROSS TABS, you could visit this article: http://www.sqlservercentral.com/articles/T-SQL/63681/
To learn how to make it dynamic, use part 2: http://www.sqlservercentral.com/articles/Crosstab/65048/
WITH DayHours AS(
SELECT *
FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),
(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(0))x(ihour)
)
SELECT ihour,
SUM( CASE WHEN CONVERT( char(10), RecordDay, 120) = '2013-10-02' THEN ClickCount ELSE 0 END) AS [2013-10-02],
SUM( CASE WHEN CONVERT( char(10), RecordDay, 120) = '2013-10-03' THEN ClickCount ELSE 0 END) AS [2013-10-03],
SUM( CASE WHEN CONVERT( char(10), RecordDay, 120) = '2013-10-04' THEN ClickCount ELSE 0 END) AS [2013-10-04],
SUM( CASE WHEN CONVERT( char(10), RecordDay, 120) = '2013-10-05' THEN ClickCount ELSE 0 END) AS [2013-10-05]
FROM clicks
RIGHT JOIN DayHours ON DATEPART( HH, RecordDay) = ihour
GROUP BY ihour
October 8, 2013 at 11:27 am
Thanks for your help.
I did a few changes on the query, but i am still encountering a few problems.
it is not summing the clickcount, i do get a three dimensional table like a wanted but without any measures :w00t:
and also the table is dynamic and i dont know how to make the name of the columns dynamic where it will show the date it is checking.
WITH DayHours AS(
SELECT *
FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),
(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(0))x(ihour)
)
SELECT ihour,
SUM( CASE WHEN CONVERT( char(10), RecordDay, 120) >= getdate()-21 and CONVERT(char(10), RecordDay, 120) < getdate()-22 THEN ClickCount ELSE 0 END) AS [2013-10-02],
SUM( CASE WHEN CONVERT( char(10), RecordDay, 120) >= getdate()-14 and CONVERT(char(10), RecordDay, 120) < getdate()-15 THEN ClickCount ELSE 0 END) AS [2013-10-03],
SUM( CASE WHEN CONVERT( char(10), RecordDay, 120) >= getdate()-7 and CONVERT(char(10), RecordDay, 120) < getdate()-8 THEN ClickCount ELSE 0 END) AS [2013-10-04],
SUM( CASE WHEN CONVERT( char(10), RecordDay, 120) >= getdate() THEN ClickCount ELSE 0 END) AS [2013-10-05]
FROM clicks
RIGHT JOIN DayHours ON DATEPART( HH, RecordDay) = ihour
GROUP BY ihour
order by ihour asc
:crying: sql can be so hard on me sometimes....
October 8, 2013 at 11:29 am
p.s. if someone can tell me how to post a table, i will gladly draw the results 😀
October 8, 2013 at 11:52 am
astrid 69000 (10/8/2013)
Thanks for your help.I did a few changes on the query, but i am still encountering a few problems.
it is not summing the clickcount, i do get a three dimensional table like a wanted but without any measures :w00t:
and also the table is dynamic and i dont know how to make the name of the columns dynamic where it will show the date it is checking.
The link that Luis posted above will show you, or you can follow the link in my signature about dynamic cross tabs.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 8, 2013 at 12:03 pm
I will prepare a litter of coffee and sit down for some light reading 😀
thanks to you too so much for your help.
I will let you know how it went. 😎
October 8, 2013 at 12:52 pm
astrid 69000 (10/8/2013)
I will prepare a litter of coffee and sit down for some light reading 😀thanks to you too so much for your help.
I will let you know how it went. 😎
You are quite welcome. Post back if you run into issues and we can help you over the hump. This stuff isn't easy to wrap your head around at first but if you stick with it you will get it.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply