1

We have the table T with the following data and structure

__________________________
ID  |  Grp   |     Dt     |
____|________|____________|
1   |   A    |  2007-11-22|  
2   |   A    |  2008-01-03|  
3   |   A    |  2008-01-03|  
4   |   A    |  2011-04-13|  
5   |   B    |  2007-11-22|  
6   |   B    |  2010-04-28|  
7   |   B    |  2009-03-19|  
8   |   B    |  2007-11-22|  
9   |   C    |  2010-04-28|  
10  |   C    |  2009-03-19|  
11  |   C    |  2011-04-13|  
12  |   C    |  2012-02-22|  
13  |   D    |  2007-11-22|  
14  |   D    |  2010-04-28|  
15  |   D    |  2009-03-19|  
16  |   E    |  2007-11-22|  
17  |   E    |  2010-04-28|  
18  |   E    |  2011-04-13|  
19  |   F    |  2007-11-22|  
20  |   G    |  2007-11-22|  
21  |   H    |  2007-11-22|  
22  |   H    |  2010-04-28|  
23  |   H    |  2009-03-19|  
24  |   H    |  2008-03-15|
____|________|____________|

Given @date_from = '2007-01-01' and @date_to = '2008-06-01' write a query that returns the max records of the filtered subset of @date_from to @date_to.

The result should be as follows:

__________________________
ID  |  Grp   |     Dt     |
____|________|____________|
2   |   A    |  2008-01-03|  
3   |   A    |  2008-01-03|  
5   |   B    |  2007-11-22|  
8   |   B    |  2007-11-22|  
13  |   D    |  2007-11-22|  
16  |   E    |  2007-11-22|  
19  |   F    |  2007-11-22|  
20  |   G    |  2007-11-22|  
21  |   H    |  2008-03-05|  
____|________|____________|

One possible solution is:

DECLARE @date_from AS DATE = '2007-01-01'
DECLARE @date_to   AS DATE = '2008-06-01'

WITH TFltr AS ( SELECT ID, Grp, Dt FROM T WHERE @date_from <= Dt AND Dt <= @date_to )
SELECT t1.ID, t1.Grp, t1.Dt 
FROM TFltr t1
LEFT OUTER JOIN TFltr t2 ON t1.Grp = t2.Grp AND t1.Dt < t2.Dt
WHERE t2.ID IS NULL

So do you know of a better/faster aproach to do this.

Thanks.

4

4 に答える 4

1

RANK分析関数をお勧めします:

DECLARE @date_from AS DATE = '2007-01-01'
DECLARE @date_to   AS DATE = '2008-06-01'

SELECT * FROM (
  SELECT ID, Grp, Dt,
    RANK() OVER (PARTITION BY Grp ORDER BY Dt DESC) AS DateRank
  FROM T
  WHERE Dt BETWEEN  @date_from AND @date_to) InnerT
WHERE DateRank = 1

内部クエリは、各内の日付を高から低にランク付けしますGrp。最高の日付DateRankは1になります。外部クエリには、が含まれる行のみが含まれますDateRank = 1。私はあなたの投稿のデータに対してこのクエリを試し、あなたが望む結果を得ました。

于 2013-03-21T16:48:19.993 に答える
1
select T1.*
from T T1
inner join
(
select max(dt) as max_dt, Grp
from T
where @date_from <= dt and dt <= @date_to
group by Grp
) X
on T1.Grp = X.Grp and T1.dt = X.max_dt
于 2013-03-21T15:47:49.330 に答える
1
select ID, Grp, Dt
from TFltr
where dt between @date_from  and @date_to
group by ID, Grp, Dt
having dt=max(dt)

I changed the query above to:

select ID as dt, Grp, Dt from T where dt between @date_from and @date_to group by ID, Grp, Dt having dt=max(dt) and ID=max(ID)

I am getting "better" results, but it is not yet correct.

于 2013-03-21T16:02:01.273 に答える
1

Another solution:

SQLFIDDLEExample

SELECT t.*
FROM TFltr t
WHERE t.Dt >='2007-01-01'
AND t.Dt <= '2008-06-01'
AND t.Dt = (SELECT MAX(h.Dt)
            FROM TFltr h
            WHERE h.Dt >='2007-01-01'
            AND h.Dt <= '2008-06-01' 
            AND h.Grp = t.Grp)
ORDER BY t.ID

Result:

| ID | GRP |                              DT |
----------------------------------------------
|  2 |   A |  January, 03 2008 00:00:00+0000 |
|  3 |   A |  January, 03 2008 00:00:00+0000 |
|  5 |   B | November, 22 2007 00:00:00+0000 |
|  8 |   B | November, 22 2007 00:00:00+0000 |
| 13 |   D | November, 22 2007 00:00:00+0000 |
| 16 |   E | November, 22 2007 00:00:00+0000 |
| 19 |   F | November, 22 2007 00:00:00+0000 |
| 20 |   G | November, 22 2007 00:00:00+0000 |
| 24 |   H |    March, 15 2008 00:00:00+0000 |

Your Query:

DECLARE @date_from AS DATE = '2007-01-01'
DECLARE @date_to   AS DATE = '2008-06-01'

SELECT t.*
FROM TFltr t
WHERE t.Dt >=@date_from
AND t.Dt <= @date_to
AND t.Dt = (SELECT MAX(h.Dt)
            FROM TFltr h
            WHERE h.Dt >=@date_from
            AND h.Dt <= @date_to 
            AND h.Grp = t.Grp)
ORDER BY t.ID
于 2013-03-22T09:39:38.550 に答える