Finding an SQL query to get the latest related date for each group

I have a payroll data sql table that shows the wage rates and effective dates associated with these wage rates, as well as hours of work on different dates. It looks something 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  ...

...

Etc. In principle, the data were combined in such a way that for each working day the entire history of the employee’s wages is combined together, and I want to get the wage rate associated with the last effective date, which is no later than the work date. So in the above example, the 7.25 rate, which will take effect on 6/10/2009, is what I want.

What request can I put together for this? I can use MAX (EffectiveDate) along with criteria based on being up to the date of work, but this gives me the latest date, I want a related wage. For this I use Sql Server.

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

, EMPID, DateWorked - . , , .

+3
4
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

:

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

:

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

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)
+2

Something like this should 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
+2
source
SELECT TOP 1 EMPID, WageRate
 FROM wages
WHERE ...... 
ORDER BY EffectiveDate DESC
0
source

Source: https://habr.com/ru/post/1736171/


All Articles