Solved

SQL Query that will return multiple records for an employee in one row

Posted on 2009-07-06
3
297 Views
Last Modified: 2013-11-30
I am trying to return a pay rate and date of that rate when changed from a database that has the information by  week
ie
PersonID  Weekending  PayRate  DatePaid ...for every week in 2009.  Each week is a row.  I want to return the starting payrate and first date paid and then in the same row return any new pay rate and date paid it was first applied
PersonID PayRate1 Datepaid1  payrate2 datepaid2 payrate3 datepaid3
If there is no difference in the payrate from 1 to 2 for instance I do not want any data in that colume.  
I am not getting the data back correctly
Thank you
0
Comment
Question by:Kmeli
  • 2
3 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 24787633
in SQL 2005 you can do something like this:


SELECT DISTINCT
c.PersonID, details = REPLACE( 
( 
	SELECT 
	cast(a.PayRate as varchar) + '-' + convert(varchar, a.datepaid, 101) AS [data()] 
	FROM 
	yourtable a 
	WHERE a.PersonID = c.PersonID
	ORDER BY 
	a.Weekending 
	FOR XML PATH ('') 
	), ' ', ',')
FROM 
yourtable c
Order by c.PersonID

Open in new window

0
 

Author Comment

by:Kmeli
ID: 24787774
Great...it looks like it returns the data correctly.  Is there a way to put that into a table?  Stored procedure?
We need to report on it please
0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 24787866
into a table, you can do it like this

create table detailperperson (
personid int,
details varchar(1000)
)

insert detailperperson
SELECT DISTINCT
c.PersonID, details = REPLACE(
(
 SELECT
 cast(a.PayRate as varchar) + '-' + convert(varchar, a.datepaid, 101) AS [data()]
 FROM
 yourtable a
 WHERE a.PersonID = c.PersonID
 ORDER BY
 a.Weekending
 FOR XML PATH ('')
 ), ' ', ',')
FROM
yourtable c
Order by c.PersonID
 
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question