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


Combining multiple rows into one row per employee


Combining multiple rows into one row per employee

Author
Message
DiabloSlayer
DiabloSlayer
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 434
Hello Everyone,

I'm working on a project where I need to retrieve employees data and then combine the data into single row per employee.

If someone kindly help I will be grateful.

Thank you,


Sample Data:


WITH SampleData (PERSON, [DATA], [FIELD]) AS
(
SELECT 1234,'04/02/2014','Date'
UNION ALL SELECT 1234,'123','Department'
UNION ALL SELECT 1234,80.0,'Rate'

)
SELECT *
FROM SampleData;



The results from the above are as follows:


PERSON DATA FIELD
1234 04/02/2014 Date
1234 123 Department
1234 80.0 Rate



The desired results would be:


PERSON Date Department Rate
1234 04/02/2014 123 80.0


Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8530 Visits: 18130
Check the following article on Cross Tabs: http://www.sqlservercentral.com/articles/T-SQL/63681/

WITH SampleData (PERSON, [DATA], [FIELD]) AS
(
SELECT 1234,'04/02/2014','Date'
UNION ALL SELECT 1234,'123','Department'
UNION ALL SELECT 1234,'80.0','Rate'

)
SELECT PERSON,
MAX( CASE WHEN FIELD = 'Date' THEN DATA END) AS [Date],
MAX( CASE WHEN FIELD = 'Department' THEN DATA END) AS Department,
MAX( CASE WHEN FIELD = 'Rate' THEN DATA END) AS Rate
FROM SampleData
GROUP BY PERSON;




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
DiabloSlayer
DiabloSlayer
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 434
Hi Guys,

I was able to get it done this way, not sure if this is most effective way though so please if you have a better solution please do let me know.

Thank you,



Select DISTINCT PERSONNUM,
(
SELECT max(DATA)
FROM SampleData
WHERE (h.PERSONNUM = PERSONNUM) AND (FIELD = 'Date')
) CONTRACT_DATE,
(
SELECT max(DATA)
FROM SampleData
WHERE (h.PERSONNUM = PERSONNUM) AND (FIELD = 'Department')
) SUMMER_DEPT,
(
SELECT max(DATA)
FROM SampleData
WHERE (h.PERSONNUM = PERSONNUM) AND (FIELD = 'Rate')
) SUMMER_RATE
FROM SampleData H


DiabloSlayer
DiabloSlayer
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 434
Hi Luis,

I just saw your reply, thank you very much for such a quick reply.

I will try your code and I will let you know how it comes out.

Thank you again,
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8530 Visits: 18130
That's a horrible way to do it. You're reading the table 4 times instead of one.
I gave you an example and an article that fully explains that method.


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
DiabloSlayer
DiabloSlayer
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 434
Hi Luis,

This is great!!

Would you kindly tell me how do I use my table in the following query?

My table already has data and the table is as follows:


CREATE TABLE TempSSProf_CustData
(PERSON [nvarchar](15) NOT NULL,
DATA [nvarchar](200) NULL,
FIELD [nvarchar](30) NOT NULL)




Thank you Luis.



WITH SampleData (PERSON, [DATA], [FIELD]) AS -- I will replace SampleData with this table TempSSProf_CustData
(
/**what goes in here?**/
SELECT 1234,'04/02/2014','Date'
UNION ALL SELECT 1234,'123','Department'
UNION ALL SELECT 1234,'80.0','Rate'

)
SELECT PERSON,
MAX( CASE WHEN FIELD = 'Date' THEN DATA END) AS [Date],
MAX( CASE WHEN FIELD = 'Department' THEN DATA END) AS Department,
MAX( CASE WHEN FIELD = 'Rate' THEN DATA END) AS Rate
FROM SampleData -- I will replace SampleData with this table TempSSProf_CustData
GROUP BY PERSON;


Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8530 Visits: 18130
What's your problem? You just need to remove the CTE and replace it with your table. You've mentioned that on the comments, so I can't see the problem.


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
DiabloSlayer
DiabloSlayer
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 434
Hi Luis,

Sorry about the confusion and stupid question :-)

Was doing too many things at the same time and obviously not thinking straight, everything is working great, thank you once again for all the help.

God bless..
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