Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-07-06
3
Medium Priority
?
302 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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…
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

670 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