SQL 按问题分组

发布于 2024-11-07 00:23:33 字数 437 浏览 1 评论 0原文

我有一个跟踪产品视图的表。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星光不落少年眉 2024-11-14 00:23:33

为每个 ProductID 生成 行号 序列,进行第一个

;WITH cte AS
(
    SELECT
       *,
       ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY TrackID) AS rn
    FROM
       MyProductTable
)
SELECT
   TrackId ProductId CreatedOn
FROM
   cte
WHERE
   rn = 1

编辑:

如果想要使用聚合,首先需要一个单独的子查询以确保结果一致。直接 MIN 不起作用。

这是基于我对问题的评论

“相邻的两个行中没有productid。相邻行由下一个/上一个Trackid定义”

SELECT
    M.*
FROM
    myProductTable M
    JOIN
    ( --gets the lowest TrackID for a ProductID
    SELECT ProductID, MIN(TrackID) AS MinTrackID
    FROM myProductTable
    GROUP BY ProductID
    ) M2 ON M.ProductID= M2.ProductID AND M.TrackID= M2.MinTrackID

Generate a row number sequence per ProductID, take the first

;WITH cte AS
(
    SELECT
       *,
       ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY TrackID) AS rn
    FROM
       MyProductTable
)
SELECT
   TrackId ProductId CreatedOn
FROM
   cte
WHERE
   rn = 1

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

"not having productid in two adjacent rows. Adjacent is defined by next/previous Trackid"

SELECT
    M.*
FROM
    myProductTable M
    JOIN
    ( --gets the lowest TrackID for a ProductID
    SELECT ProductID, MIN(TrackID) AS MinTrackID
    FROM myProductTable
    GROUP BY ProductID
    ) M2 ON M.ProductID= M2.ProductID AND M.TrackID= M2.MinTrackID
幼儿园老大 2024-11-14 00:23:33
select min(TrackId), ProductId, CreatedOn
from YourTable
group by ProductId, CreatedOn;
select min(TrackId), ProductId, CreatedOn
from YourTable
group by ProductId, CreatedOn;
述情 2024-11-14 00:23:33

如果日期不重要,您可以对 TrackID 和 ProductID 进行 GroupBy,并对 CreatedOn 执行 Min 操作。

SELECT TrackID ,ProductID ,MIN(CreatedOn)
FROM [table]
GROUP BY TrackID ,ProductID

如果日期相同,您可以按所有三个进行分组

SELECT TrackID ,ProductID ,CreatedOn
FROM [table]
GROUP BY TrackID ,ProductID ,CreatedOn

You can GroupBy on the TrackID and ProductID and do a Min of the CreatedOn if the date is not important.

SELECT TrackID ,ProductID ,MIN(CreatedOn)
FROM [table]
GROUP BY TrackID ,ProductID

If the date is the same you can group by all three

SELECT TrackID ,ProductID ,CreatedOn
FROM [table]
GROUP BY TrackID ,ProductID ,CreatedOn
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文