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


DateTime Rows to Columns


DateTime Rows to Columns

Author
Message
rootfixxxer
rootfixxxer
SSC-Enthusiastic
SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)

Group: General Forum Members
Points: 140 Visits: 531
Hi

I have something like a table of users and other table with dates and working time for the users, i want to display the working hours for the users, but i need to see the date in columns.

I created a little script to show what i want, but i'm having a problem with the duplicate users.

DROP TABLE #Users
DROP TABLE #Working
CREATE TABLE #Users (Num INT, NAME NVARCHAR(5))
CREATE TABLE #Working (WorkDay SMALLDATETIME,Num INT, TimeWorked INT)


INSERT INTO #Users
SELECT 1,'User1'
UNION ALL
SELECT 2,'User2'
UNION ALL
SELECT 3,'User3'



INSERT INTO #Working
SELECT '01/01/2013',1,2
UNION ALL
SELECT '02/01/2013',1,0
UNION ALL
SELECT '03/01/2013',1,1
UNION ALL
SELECT '04/01/2013',1,4
UNION ALL
SELECT '05/01/2013',1,0
UNION ALL

SELECT '01/01/2013',2,0
UNION ALL
SELECT '02/01/2013',2,0
UNION ALL
SELECT '03/01/2013',2,20
UNION ALL
SELECT '04/01/2013',2,20
UNION ALL
SELECT '05/01/2013',2,20
UNION ALL

SELECT '01/01/2013',3,30
UNION ALL
SELECT '02/01/2013',3,30
UNION ALL
SELECT '03/01/2013',3,0
UNION ALL
SELECT '04/01/2013',3,0
UNION ALL
SELECT '05/01/2013',3,0


SELECT * FROM #Users
SELECT * FROM #Working


SELECT DISTINCT
U.Num,
U.NAME,
[1] = CASE WHEN MONTH(W.WorkDay) = 1 THEN W.TimeWorked ELSE 0 END,
[2] = CASE WHEN MONTH(W.WorkDay) = 2 THEN W.TimeWorked ELSE 0 END,
[3] = CASE WHEN MONTH(W.WorkDay) = 3 THEN W.TimeWorked ELSE 0 END,
[4] = CASE WHEN MONTH(W.WorkDay) = 4 THEN W.TimeWorked ELSE 0 END,
[5] = CASE WHEN MONTH(W.WorkDay) = 5 THEN W.TimeWorked ELSE 0 END
FROM #Users U INNER JOIN #Working W ON U.Num = W.Num



In the end the query will be built on the fly, with the help of the tally table, i just pass 2 paramteres to the sp, the year and the month...

Thanks
Erin Ramsay
Erin Ramsay
Mr or Mrs. 500
Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)

Group: General Forum Members
Points: 561 Visits: 1099
The request is a little unclear to me. Can you supply a sample of what you would expect from the resultset so we can understand further?
rootfixxxer
rootfixxxer
SSC-Enthusiastic
SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)

Group: General Forum Members
Points: 140 Visits: 531
Hi
I have to reply with text, in this machine i don't have any sql server installed.

So if i run the above code i get something like this:
Num Name 1 2 3 4 5 ---> Column Names
1 User1 2 0 0 0 0
1 User1 0 0 0 0 0
1 User1 0 0 1 0 0
1 User1 0 0 0 4 0
1 User1 0 0 0 0 0
and something equal for the other users...

WHat i'm trying to achieve it's to only have one row for each user like this:
Num Name 1 2 3 4 5 ---> Column Names
1 User1 2 0 1 4 0
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45113 Visits: 39916
You were SO close...

SELECT 
U.Num,
U.NAME,
[1] = SUM(CASE WHEN MONTH(W.WorkDay) = 1 THEN W.TimeWorked ELSE 0 END),
[2] = SUM(CASE WHEN MONTH(W.WorkDay) = 2 THEN W.TimeWorked ELSE 0 END),
[3] = SUM(CASE WHEN MONTH(W.WorkDay) = 3 THEN W.TimeWorked ELSE 0 END),
[4] = SUM(CASE WHEN MONTH(W.WorkDay) = 4 THEN W.TimeWorked ELSE 0 END),
[5] = SUM(CASE WHEN MONTH(W.WorkDay) = 5 THEN W.TimeWorked ELSE 0 END)
FROM #Users U INNER JOIN #Working W ON U.Num = W.Num
GROUP BY U.Num, U.Name;



You can learn more about this technique (cross tab) at the following link.
http://www.sqlservercentral.com/articles/T-SQL/63681/
And thank you VERY much for the test data setup and the sample code. It made helping you very easy to do. I wish more people would take the small bit of time.

--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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

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

Group: General Forum Members
Points: 140 Visits: 531
Blush

End of the day of friday, hard week... dumb questions... Satisfied

Now it's obvious that i need to sum the values... :-D

Thanks
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