我在查询结果中遇到问题,如果我在order by
之后返回的结果集上使用group by
,则分组顺序会受到干扰。
select a.location_id,a.feed_date
from p_article_table
inner join m_misc_details l on a.location_id = l.misc_detail_id
and misc_master_id=2
and a.location_id in (1,2,4,5,6,7,8)
and convert(varchar(11),a.feed_date,108)<'09:01:00'
group by a.location_id,a.feed_date
order by a.feed_date
我该怎么办?
location_id上的GROUP BY之后的结果为
1
1
1
1
5
7
7
7
8
但是在feed_date上使用ORDER BY后,结果会受到干扰
1 1 五 7 1 8 1 ..
答案 0 :(得分:2)
分组操作不受订单的影响。按顺序返回分组记录的顺序是任意的。
你想要这个:
select a.location_id,a.feed_date
from p_article_table
inner join m_misc_details l on a.location_id = l.misc_detail_id
and misc_master_id=2
and a.location_id in (1,2,4,5,6,7,8)
and convert(varchar(11),a.feed_date,108)<'09:01:00'
group by a.location_id, a.feed_date
order by a.location_id, a.feed_date