简体   繁体   中英

Finding a sql query to get the latest associated date for each grouping

I have a sql table of payroll data that has wage rates and effective dates associated with those wage rates, as well as hours worked on various dates. It looks somewhat like this:

EMPID  DateWorked  Hours  WageRate  EffectiveDate

1      1/1/2010     10     7.00      6/1/2009
1      1/1/2010     10     7.25      6/10/2009
1      1/1/2010     10     8.00      2/1/2010
1      1/10/2010 ...
2      1/1/2010  ...

...

And so on. Basically, the data has been combined in such a way that for every day worked, all of the employee's wage history is joined together, and I want to grab the wage rate associated with the LATEST effective date that is not later than the date worked. So in the example above, the rate of 7.25 that become effective on 6/10/2009 is what I want.

What kind of query can I put together for this? I can use MAX(EffectiveDate) alongwith a criteria based on being before the work date, but that only gives me the latest date itself, I want the associated wage. I am using Sql Server for this.

Alternatively, I have the original tables that were used to create this data. One of them contains the dates worked, and the hours as well as EMPID, the other contains the list of wage rates and effective dates. Is there a way to join these instead that would correctly apply the right wage rate for each work day?

I was thinking that I'd want to group by EMPID and then DateWorked, and do something from there. I want to get a result that gives me the wage rate that actually is the latest effective rate for each date worked

select p.*
from (
    select EMPID, DateWorked, Max(EffectiveDate) as MaxEffectiveDate
    from Payroll
    where EffectiveDate <= DateWorked
    group by EMPID, DateWorked
) pm
inner join Payroll p on pm.EMPID = p.EMPID and pm.DateWorked = p.DateWorked and pm.MaxEffectiveDate = p.EffectiveDate

Output:

EMPID       DateWorked              Hours       WageRate                                EffectiveDate
----------- ----------------------- ----------- --------------------------------------- -----------------------
1           2010-01-01 00:00:00.000 10          7.25                                    2009-06-10 00:00:00.000

try this:

DECLARE @YourTable table (EMPID int, DateWorked datetime, Hours int
                         ,WageRate numeric(6,2), EffectiveDate datetime)
INSERT INTO @YourTable VALUES (1,'1/1/2010' ,10,     7.00,  '6/1/2009')
INSERT INTO @YourTable VALUES (1,'1/1/2010' ,10,     7.25, '6/10/2009')
INSERT INTO @YourTable VALUES (1,'1/1/2010' ,10,     8.00,  '2/1/2010')
INSERT INTO @YourTable VALUES (1,'1/10/2010',10,     20.00,'12/1/2010')
INSERT INTO @YourTable VALUES (2,'1/1/2010' ,8 ,     12.00, '2/1/2009')

SELECT
    e.EMPID,e.WageRate,e.EffectiveDate
    FROM @YourTable  e
        INNER JOIN (SELECT
                        EMPID,MAX(EffectiveDate) AS EffectiveDate
                        FROM @YourTable
                        WHERE EffectiveDate<GETDATE()+1
                        GROUP BY EMPID
                   ) dt ON e.EMPID=dt.EMPID AND e.EffectiveDate=dt.EffectiveDate
   ORDER BY e.EMPID

OUTPUT

EMPID       WageRate                                EffectiveDate
----------- --------------------------------------- -----------------------
1           8.00                                    2010-02-01 00:00:00.000
2           12.00                                   2009-02-01 00:00:00.000

(2 row(s) affected)

Something like this ought to work:

SELECT T.* FROM T
INNER JOIN (
    SELECT EMPID, MAX(EFFECTIVEDATE) EFFECTIVEDATE
    FROM T
    WHERE DATEWORKED <= EFFECTIVEDATE
    GROUP BY EMPID) t2
  ON T2.EMPID = T.EMPID 
  AND T2.EFFECTIVEDATE = T.EFFECTIVEDATE
SELECT TOP 1 EMPID, WageRate
 FROM wages
WHERE ...... 
ORDER BY EffectiveDate DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM