Solved

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

Posted on 2009-07-06
3
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

752 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