SQL 按问题分组
我有一个跟踪产品视图的表。
TrackId ProductId CreatedOn
1 1 01/01/2011
2 4 01/01/2011
3 4 01/01/2011
4 10 01/01/2011
我想要做的是返回一个没有两个相邻的 ProductId 的数据集。我想从上面的数据集中返回 IE:
TrackId ProductId CreatedOn
1 1 01/01/2011
2 4 01/01/2011
4 10 01/01/2011
据我所知,我不能使用不同的,因为这是基于行的?
帮助表示赞赏。
I have a table which tracks views of products.
TrackId ProductId CreatedOn
1 1 01/01/2011
2 4 01/01/2011
3 4 01/01/2011
4 10 01/01/2011
What I want to do is return a dataset which doesn't have two ProductIds next to each other. I.E from the above data set I would want to return:
TrackId ProductId CreatedOn
1 1 01/01/2011
2 4 01/01/2011
4 10 01/01/2011
I can't use distinct as far as I am aware as this is row based?
Help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为每个 ProductID 生成 行号 序列,进行第一个
编辑:
如果想要使用聚合,首先需要一个单独的子查询以确保结果一致。直接 MIN 不起作用。
这是基于我对问题的评论
Generate a row number sequence per ProductID, take the first
Edit:
If you want to use an aggregate, you need a separate subquery first to ensure consistent results. A straight MIN won't work.
This is based on my comment to the question
如果日期不重要,您可以对 TrackID 和 ProductID 进行 GroupBy,并对 CreatedOn 执行 Min 操作。
如果日期相同,您可以按所有三个进行分组
You can GroupBy on the TrackID and ProductID and do a Min of the CreatedOn if the date is not important.
If the date is the same you can group by all three