Mysql按日期排序,按结果分组
我的表结构如下Mysql按日期排序,按结果分组,mysql,group-by,Mysql,Group By,我的表结构如下 id parent last_date sub_type ---------------------------------- 11 9 2017-02-28 1101 10 9 2016-08-26 1101 8 1 2017-02-20 1101 12 12 2016-08-31 1102 14 12 2016-12-20 1102
id parent last_date sub_type
----------------------------------
11 9 2017-02-28 1101
10 9 2016-08-26 1101
8 1 2017-02-20 1101
12 12 2016-08-31 1102
14 12 2016-12-20 1102
9 9 2016-12-31 1101
13 12 2017-03-23 1102
2 1 2017-01-25 1101
1 1 2016-12-31 1101
我想根据最长的第一个日期获取每个子类型的行。我试着回答以下问题
SELECT * FROM mytable GROUP BY sub_type ORDER BY ISNULL(last_date) DESC, last_date DESC
结果呢
id parent last_date sub_type
--------------------------------
1 1 2016-12-31 1101
12 12 2016-08-31 1102
但我预期结果如下
id parent last_date sub_type
--------------------------------
13 12 2017-03-23 1102
11 9 2017-02-28 1101
请引导我获得以上结果
编辑:
最后一个日期可能有空值,这将最大程度地优先于日期项。这就是为什么我选择ISNULL DESC order。这是一个典型的问题,通过某种聚合在每组中获取一条记录。试试这个:
select
mytable.*
from mytable
join (
select max(last_date) as last_date, sub_type from mytable group by sub_type
) t1 on mytable.sub_type = t1.sub_type and mytable.last_date = t1.last_date
请参阅本文
和右侧的相关链接:
用小提琴演奏
编辑:
如果没有2个相同的最后日期和空优先级,请尝试以下操作:
select
mytable.*
from mytable
join (
select
max(last_date) as last_date,
max(isnull(last_date)) nulled,
sub_type
from mytable
group by sub_type
) t1 on mytable.sub_type = t1.sub_type
and (t1.nulled and mytable.last_date is null or (t1.nulled <> 1 and mytable.last_date = t1.last_date))
同样在sqlfiddle中。这是一个典型的问题,通过某种聚合在每组中获取一条记录。试试这个:
select
mytable.*
from mytable
join (
select max(last_date) as last_date, sub_type from mytable group by sub_type
) t1 on mytable.sub_type = t1.sub_type and mytable.last_date = t1.last_date
请参阅本文
和右侧的相关链接:
用小提琴演奏
编辑:
如果没有2个相同的最后日期和空优先级,请尝试以下操作:
select
mytable.*
from mytable
join (
select
max(last_date) as last_date,
max(isnull(last_date)) nulled,
sub_type
from mytable
group by sub_type
) t1 on mytable.sub_type = t1.sub_type
and (t1.nulled and mytable.last_date is null or (t1.nulled <> 1 and mytable.last_date = t1.last_date))
同样在sqlfiddle中。您也可以根据sub_type列和last_date列的降序给出行号 质疑
您还可以根据sub_type列和last_date列的降序给出行号 质疑
您编写了一个错误的查询 在Where子句之后指定Where条件 下面的查询将给出您的预期结果
SELECT id,parent,max(last_Date),sub_type FROM mytable GROUP BY sub_type
您编写了一个错误的查询 在Where子句之后指定Where条件 下面的查询将给出您的预期结果
SELECT id,parent,max(last_Date),sub_type FROM mytable GROUP BY sub_type
可以在WHERE子句中使用ORDER BY和LIMIT 1的相关子查询来查找要查找的行的id
SELECT *
FROM mytable t1
WHERE id = (
SELECT id
FROM mytable t2
WHERE t2.sub_type = t1.sub_type
ORDER BY ISNULL(last_date) DESC, last_date DESC, id DESC
LIMIT 1
)
演示:
注意:子类型应编入索引。您可以在WHERE子句中使用ORDER BY和LIMIT 1的相关子查询来查找要查找的行的id
SELECT *
FROM mytable t1
WHERE id = (
SELECT id
FROM mytable t2
WHERE t2.sub_type = t1.sub_type
ORDER BY ISNULL(last_date) DESC, last_date DESC, id DESC
LIMIT 1
)
演示:
注意:子类型应编入索引。按子类型从mytable组中选择子类型、MAXlast日期?id是主键吗?如果存在多个空记录怎么办?您要获取哪条记录?@Forward。没有两个条目具有相同的最后日期、父项和子类型。例如:父项:9,最后日期:2017-02-28,子项类型:1101,无重复项。@Spiegel yes id为主键。这里我显示了有限的列,行只是为了不太混淆。但概念是相同的,从每个组中获取一个最大日期的条目,包括空日期选择子类型,按子类型从mytable组中获取最大最后日期?id是主键吗?如果存在多个空记录怎么办?您要获取哪条记录?@Forward。没有两个条目具有相同的最后日期、父项和子类型。例如:父项:9,最后日期:2017-02-28,子项类型:1101,无重复项。@Spiegel yes id为主键。这里我显示了有限的列,行只是为了不太混淆。但概念是相同的,从每个组获取一个最大日期的条目,包括空日期@Ajay Singh,谢谢你的注释。它的类型错误。更正:nowid和父列不带聚合函数,也不在group by子句中。@ullas在mysql中,没有必要为group by指定所有列,它将在group by上分组expression@Ark此查询将获得所需的确切结果集。在最后添加“按子类型描述”的订单,结果如下:expected@AjaySingh:通过您的查询,OP将得到他现在得到的结果集。但op期待另一个结果集。@Ajay Singh谢谢你的提示。它的类型错误。更正:nowid和父列不带聚合函数,也不在group by子句中。@ullas在mysql中,没有必要为group by指定所有列,它将在group by上分组expression@Ark此查询将获得所需的确切结果集。在最后添加“按子类型描述”的订单,结果如下:expected@AjaySingh:通过您的查询,OP将得到他现在得到的结果集。但是op期待另一个结果集。非常感谢你的回答。我会试试你的建议。非常感谢你的回答。我会试试你的建议。谢谢你的回答。我会试试你的建议谢谢你的回答。我会试试你的建议谢谢你的回答。我会试试你的建议谢谢你的回答。我会试试你的建议