Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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

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
Kmeli
Asked:
Kmeli
  • 2
1 Solution
 
ralmadaCommented:
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
 
KmeliAuthor Commented:
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
 
ralmadaCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now