How to max(date) and use the in feature in sql server in one query?

风流意气都作罢 提交于 2019-12-06 09:32:58

In standard SQL any of the following queries solve the issue:

Option 1

SELECT t1.* FROM t t1
LEFT JOIN t t2
ON t1.color = t2.color AND t1.shade = t2.shade AND t1.date < t2.date
WHERE t2.date IS NULL

Option 2

SELECT t1.* FROM t t1
JOIN (
  SELECT color, shade, max(date) date FROM t
  GROUP BY color, shade
) t2
ON t1.color = t2.color AND t1.shade = t2.shade AND t1.date = t2.date

In your example data, all the result should be returned, so it doesn't make much sense to operate on it. I've slightly changed it into this:

| ID | COLOR | SHADE |       DATE |
|----|-------|-------|------------|
|  1 |   red |  dark | 1990-01-01 |
|  2 |   red |  dark | 2013-09-16 |
|  3 | green | light | 2010-08-15 |
|  4 | green |  dark | 2012-09-18 |
|  5 | green |  dark | 2013-08-20 |
|  6 | white |  dark | 2013-08-31 |
|  7 | white | light | 2012-08-30 |
|  8 | white | light | 2010-08-20 |

The output of the queries would be:

| ID | COLOR | SHADE |       DATE |
|----|-------|-------|------------|
|  2 |   red |  dark | 2013-09-16 |
|  3 | green | light | 2010-08-15 |
|  5 | green |  dark | 2013-08-20 |
|  6 | white |  dark | 2013-08-31 |
|  7 | white | light | 2012-08-30 |

Make sure you apply the appropriate orderings.

Fiddle here.

bvr

Try this

SELECT t1.* FROM t t1
JOIN 
(
  SELECT color, MAX([date]) [date] FROM t
  WHERE color IN ('red', 'green')
  GROUP BY color
) t2
ON t1.color = t2.color AND t1.date = t2.date
ORDER BY ID

OUTPUT

id  color   shade   date
2   red     light   2013-09-16
5   green   dark    2013-08-20

OR

SELECT id, color, shade, [date]
FROM 
(
  SELECT *,
        row_number() OVER (PARTITION BY color ORDER BY [date] DESC) AS sno
  FROM t
  WHERE color IN ('red', 'green')
) tt
WHERE sno = 1
ORDER BY ID

OUTPUT

id  color   shade   date
2   red     light   2013-09-16
5   green   dark    2013-08-20

OR

SELECT id, color, shade, [date]
FROM 
(
  SELECT *,
        MAX([date]) OVER (PARTITION BY color) AS maxdate
  FROM t
  WHERE color IN ('red', 'green')
) tt
WHERE [date] = maxdate
ORDER BY ID

OUTPUT

id  color   shade   date
2   red     light   2013-09-16
5   green   dark    2013-08-20
select id, color, shade, max(date) over() AS m_date from mutable

OVER CLAUSE used IN analyze functions

A simple way to think about this is by first creating a query that gives you the latest date for each colour:

select color, max([date]) as maxdate
from mytable
group by color;

Then by joining this query with a simple select query that just retrieves all rows from your original table:

select t.[id], t.color, t.shade, sq.maxdate
from mytable as t
inner join (
  select color, max([date]) as maxdate
  from mytable
  group by color
) as sq
on (t.color = sq.color);

Then, to restrict the results to only certain colours, just add on your where clause at the end of the query:

where color in ('red', 'green');

The easiest way to do this is with window/ranking functions. Here is an example with row_number():

select id, color, shade, "date"
from (select t.*,
             row_number() over (partition by color order by "date" desc) as seqnum
      from mytable
     ) t
where seqnum = 1;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!