Solved

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

Posted on 2009-07-06
3
293 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 45
search for a string in all tables 4 15
SQL Inner Join Vs SubQueries 9 25
add 1 to a field for 100 rows 11 12
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now