如何 select table 中的所有列与多列不同
How to select all columns in table with Distinct on multiple columns
我看过一些与此类似的帖子,但我没有找到真正有意义的或我尝试过的作品。本质上,我需要 select 区分 3 列,但 return table 中的所有列。到目前为止,这就是我所拥有的:
SELECT *
FROM tbl1
WHERE tblKey IN (SELECT DISTINCT personKey, contentkey, EmailKey
FROM tbl1
WHERE (Application = 'website' OR Application =
'connect') AND
(contentKey IN (12, 13, 14, 16, 17, 18 , 19)) AND
(channelKey = 1))
子查询中的 where 语句是因为 This only should apply to the content that is true to the where clause (obviously, but just make it known that Im not trying to remove duplicates in the where clause).这个查询显然不起作用,因为 tblKey 不在子查询中,但我想不出解决这个问题的方法。
如果您希望每个键组合有一个示例行,那么您的代码将使用聚合函数:
SELECT *
FROM tbl1
WHERE tblKey IN (SELECT MAX(tblKey)
FROM tbl1
WHERE Application IN ('website', 'connect') AND
contentKey IN (12, 13, 14, 16, 17, 18 , 19) AND
channelKey = 1
GROUP BY personKey, contentkey, EmailKey
);
我确实认为使用 window 函数来表达它会更常见:
SELECT t.*
FROM (SELECT t.*
ROW_NUMBER() OVER (PARTITION BY personKey, contentkey, EmailKey ORDER BY (SELECT NULL)) as seqnum
FROM tbl1 t
WHERE Application IN ('website', 'connect') AND
contentKey IN (12, 13, 14, 16, 17, 18 , 19) AND
channelKey = 1
) t
WHERE seqnum = 1;
我看过一些与此类似的帖子,但我没有找到真正有意义的或我尝试过的作品。本质上,我需要 select 区分 3 列,但 return table 中的所有列。到目前为止,这就是我所拥有的:
SELECT *
FROM tbl1
WHERE tblKey IN (SELECT DISTINCT personKey, contentkey, EmailKey
FROM tbl1
WHERE (Application = 'website' OR Application =
'connect') AND
(contentKey IN (12, 13, 14, 16, 17, 18 , 19)) AND
(channelKey = 1))
子查询中的 where 语句是因为 This only should apply to the content that is true to the where clause (obviously, but just make it known that Im not trying to remove duplicates in the where clause).这个查询显然不起作用,因为 tblKey 不在子查询中,但我想不出解决这个问题的方法。
如果您希望每个键组合有一个示例行,那么您的代码将使用聚合函数:
SELECT *
FROM tbl1
WHERE tblKey IN (SELECT MAX(tblKey)
FROM tbl1
WHERE Application IN ('website', 'connect') AND
contentKey IN (12, 13, 14, 16, 17, 18 , 19) AND
channelKey = 1
GROUP BY personKey, contentkey, EmailKey
);
我确实认为使用 window 函数来表达它会更常见:
SELECT t.*
FROM (SELECT t.*
ROW_NUMBER() OVER (PARTITION BY personKey, contentkey, EmailKey ORDER BY (SELECT NULL)) as seqnum
FROM tbl1 t
WHERE Application IN ('website', 'connect') AND
contentKey IN (12, 13, 14, 16, 17, 18 , 19) AND
channelKey = 1
) t
WHERE seqnum = 1;