2

我有一张这样的桌子

id   color  shade   date
---  ----   -----   -----
1    red    dark    01/01/1990
2    red    light   09/16/2013
3    green  light   08/15/2010
4    green  dark    09/18/2012
5    maroon dark    08/20/2013
6    white  dark    08/31/2013
7    white  light   08/30/2012
8    purple light   08/20/2010

我想要最新日期的每种颜色的条目。所以我试着做:

select id, color, shade, max(date) from mytable;

这不起作用并给了我错误:

is invalid in the select list because it is not contained in either an aggregate 
function or the GROUP BY clause.

好的,所以我Group By按照错误的建议将其添加到

select id, color, shade, max(date) from mutable
Group by id, color, shade

这给了我想要的结果。

问题

现在,我希望只对某些颜色重复上述相同的查询。例如,我只想看到红色和绿色的。

所以我做了:

select id, color, shade, max(date) from mutable
Where color in ('red', 'green')
Group by id, color, shade;

但是,这并没有给我正确数量的结果,因为我猜它们是按shade.

获取我想要的结果的最佳方法是什么?基本上我想max date从同一事物的两个实例中取出,并且在表格之外只想要某些颜色。

4

5 回答 5

2

考虑这一点的一种简单方法是首先创建一个查询,为您提供每种颜色的最新日期:

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

然后通过一个简单的选择查询加入这个查询,该查询只从原始表中检索所有行:

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);

然后,要将结果限制为仅某些颜色,只需where在查询末尾添加您的子句:

where color in ('red', 'green');
于 2013-09-17T01:23:17.793 回答
2

尝试这个

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

输出

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

或者

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

输出

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

或者

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

输出

id  color   shade   date
2   red     light   2013-09-16
5   green   dark    2013-08-20
于 2013-09-17T05:42:23.630 回答
2

在标准 SQL 中,以下任何查询都可以解决该问题:

选项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

选项 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

在您的示例数据中,应该返回所有结果,因此对其进行操作没有多大意义。我稍微把它改成了这样:

| 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 |

查询的输出将是:

| 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 |

确保应用适当的顺序。

在这里拉小提琴。

于 2013-09-17T00:57:39.013 回答
1
select id, color, shade, max(date) over() AS m_date from mutable

OVER CLAUSE 用于分析函数

于 2013-09-17T00:35:56.973 回答
0

最简单的方法是使用窗口/排名函数。这是一个例子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;
于 2013-09-17T03:59:21.630 回答