我如何只能从相同的 id_category 和 id_service 中获取具有最大日期的行
How I can get only row from same id_category and id_service with the max date of them
我有这个 table:
id_category | id_service | amount | date
这个 table 有不止一行 id_category 和 id_service。我如何只能从相同的 id_category 和 id_service 中获取具有最大日期的行?
示例数据:
1 | 1 | 0.1 | 2015-05-05
1 | 1 | 0.12 | 2015-05-06
1 | 2 | 0.2 | 2015-05-04
1 | 2 | 0.25 | 2015-05-05
1 | 2 | 0.30 | 2015-05-06
2 | 1 | 0.15 | 2015-05-05
我想得到这个结果:
1 | 1 | 0.12 | 2015-05-06
1 | 2 | 0.30 | 2015-05-06
2 | 1 | 0.15 | 2015-05-05
谢谢!
http://sqlfiddle.com/#!9/ad96b/3
SELECT t1.*
FROM t1
LEFT JOIN t1 t2
ON t1.id_category = t2.id_category
AND t1.id_service = t2.id_service
AND t1.`date` < t2.`date`
WHERE t2.date IS NULL
select id_category, id_service, max(amount), min(date)
from table_name
group by id_category, id_service;
也许你的查询是这样的
select a.* from table a
where a.date =
(select max(b.date) from table b where a.id=b.id group by b.id_service, b.id_category)
group by a.id_category, a.id_service;
您可以使用派生 table 然后使用内部联接
MYSQL 版本 SQL FIDDLE
SELECT gd.*, t.amount
FROM (Select id_category, id_service, max(date) date
FROM t
group by id_category, id_service) gd
INNER JOIN t ON gd.id_service = t.id_service
AND gd.id_category = t.id_category
AND gd.date = t.date
ORDER BY gd.id_category, gd.id_service
MSSQL 版本:- 如果 SQL 服务器 SQLFIDDLE
,这将通过 CTE 获得
;WITH GroupedData AS
(
select id_category, id_service, max(date) date
FROM t
group by id_category, id_service
)
SELECT gd.*, t.amount
FROM GroupedData gd
INNER JOIN t ON gd.id_service = t.id_service
AND gd.id_category = t.id_category
AND gd.date = t.date
ORDER BY gd.id_category, gd.id_service
我有这个 table:
id_category | id_service | amount | date
这个 table 有不止一行 id_category 和 id_service。我如何只能从相同的 id_category 和 id_service 中获取具有最大日期的行?
示例数据:
1 | 1 | 0.1 | 2015-05-05
1 | 1 | 0.12 | 2015-05-06
1 | 2 | 0.2 | 2015-05-04
1 | 2 | 0.25 | 2015-05-05
1 | 2 | 0.30 | 2015-05-06
2 | 1 | 0.15 | 2015-05-05
我想得到这个结果:
1 | 1 | 0.12 | 2015-05-06
1 | 2 | 0.30 | 2015-05-06
2 | 1 | 0.15 | 2015-05-05
谢谢!
http://sqlfiddle.com/#!9/ad96b/3
SELECT t1.*
FROM t1
LEFT JOIN t1 t2
ON t1.id_category = t2.id_category
AND t1.id_service = t2.id_service
AND t1.`date` < t2.`date`
WHERE t2.date IS NULL
select id_category, id_service, max(amount), min(date)
from table_name
group by id_category, id_service;
也许你的查询是这样的
select a.* from table a
where a.date =
(select max(b.date) from table b where a.id=b.id group by b.id_service, b.id_category)
group by a.id_category, a.id_service;
您可以使用派生 table 然后使用内部联接
MYSQL 版本 SQL FIDDLE
SELECT gd.*, t.amount
FROM (Select id_category, id_service, max(date) date
FROM t
group by id_category, id_service) gd
INNER JOIN t ON gd.id_service = t.id_service
AND gd.id_category = t.id_category
AND gd.date = t.date
ORDER BY gd.id_category, gd.id_service
MSSQL 版本:- 如果 SQL 服务器 SQLFIDDLE
,这将通过 CTE 获得;WITH GroupedData AS
(
select id_category, id_service, max(date) date
FROM t
group by id_category, id_service
)
SELECT gd.*, t.amount
FROM GroupedData gd
INNER JOIN t ON gd.id_service = t.id_service
AND gd.id_category = t.id_category
AND gd.date = t.date
ORDER BY gd.id_category, gd.id_service