SQL 服务器:按列与另一列分组
SQL Server : group by column with another column
从如下 table 中,我想获取 itemName 和电子邮件,其中相同的 itemName 已通过电子邮件发送到不同的电子邮件地址。相同的电子邮件可以接收不同的 itemNames。相同的 itemname 可以出现在 table 中的多个位置,它们并不总是按 id 排序。 ItemNames 是唯一的,因为可以通过 itemname 自行加入。
我试过了:
我用 row_number、group by 、having 等尝试了一堆查询,但无法正确查询。
有人能帮忙吗?
示例数据:
declare @t table (id int, itemname nvarchar(50), emailto nvarchar(50))
insert into @t
values (1, 'item1', 'email1') --include 1 & 2 because same item went to different emails
, (2, 'item1', 'email2')
, (3, 'item2', 'email1') --exclude because even though email1 received an email before, item2 went to a sinle email
, (4, 'item3', 'email3') --exclude 4, 5, 6 becuase all item3 went to the same email
, (5, 'item3', 'email3')
, (6, 'item3', 'email3')
, (7, 'item4', 'email6')
, (8, 'item4', 'email6') --include 8 & 9, only reason to exclude 7 is to get a distinct list of itemName and email pairs
, (9, 'item4', 'email7')
, (10, 'item3', 'email3') --exclude 10 becuase all item3 went to the same email, this is the same item from 4, 5, 6
;with expectedOutput as
(
select
t.itemname, t.emailto
from @t t
where
t.id IN (1, 2, 8, 9)
)
select *
from expectedOutput
/*
Expected output:
itemname emailto
item1 email1
item1 email2
item4 email6
item4 email7
*/
这是一种方法 - 使用 CTE 获取发送到多个电子邮件的所有项目,然后将该 cte 与原始 table:
合并
;WITH Items AS
(
SELECT itemname
FROM @t
GROUP BY itemname
HAVING COUNT(DISTINCT emailto) > 1
)
SELECT DISTINCT t.itemname, emailto
FROM @t t
INNER JOIN Items i ON t.itemname = i.itemname
结果:
itemname emailto
item1 email1
item1 email2
item4 email6
item4 email7
假设您要查找的是独特的电子邮件和项目对。
with expectedOutput as
(select distinct
t.itemname,
t.emailto
from @t t),
steptwo as (
select tt.itemname, count(distinct tt.emailto) as nemails
from expectedOutput tt
group by tt.itemname
)
select tw.itemname,e.emailto from steptwo tw join expectedOutput e
on tw.itemname = e.itemname
WHERE nemails > 1
屈服
item1 email1
item1 email2
item4 email6
item4 email7
我们都去过那里。
从如下 table 中,我想获取 itemName 和电子邮件,其中相同的 itemName 已通过电子邮件发送到不同的电子邮件地址。相同的电子邮件可以接收不同的 itemNames。相同的 itemname 可以出现在 table 中的多个位置,它们并不总是按 id 排序。 ItemNames 是唯一的,因为可以通过 itemname 自行加入。
我试过了:
我用 row_number、group by 、having 等尝试了一堆查询,但无法正确查询。
有人能帮忙吗?
示例数据:
declare @t table (id int, itemname nvarchar(50), emailto nvarchar(50))
insert into @t
values (1, 'item1', 'email1') --include 1 & 2 because same item went to different emails
, (2, 'item1', 'email2')
, (3, 'item2', 'email1') --exclude because even though email1 received an email before, item2 went to a sinle email
, (4, 'item3', 'email3') --exclude 4, 5, 6 becuase all item3 went to the same email
, (5, 'item3', 'email3')
, (6, 'item3', 'email3')
, (7, 'item4', 'email6')
, (8, 'item4', 'email6') --include 8 & 9, only reason to exclude 7 is to get a distinct list of itemName and email pairs
, (9, 'item4', 'email7')
, (10, 'item3', 'email3') --exclude 10 becuase all item3 went to the same email, this is the same item from 4, 5, 6
;with expectedOutput as
(
select
t.itemname, t.emailto
from @t t
where
t.id IN (1, 2, 8, 9)
)
select *
from expectedOutput
/*
Expected output:
itemname emailto
item1 email1
item1 email2
item4 email6
item4 email7
*/
这是一种方法 - 使用 CTE 获取发送到多个电子邮件的所有项目,然后将该 cte 与原始 table:
合并;WITH Items AS
(
SELECT itemname
FROM @t
GROUP BY itemname
HAVING COUNT(DISTINCT emailto) > 1
)
SELECT DISTINCT t.itemname, emailto
FROM @t t
INNER JOIN Items i ON t.itemname = i.itemname
结果:
itemname emailto
item1 email1
item1 email2
item4 email6
item4 email7
假设您要查找的是独特的电子邮件和项目对。
with expectedOutput as
(select distinct
t.itemname,
t.emailto
from @t t),
steptwo as (
select tt.itemname, count(distinct tt.emailto) as nemails
from expectedOutput tt
group by tt.itemname
)
select tw.itemname,e.emailto from steptwo tw join expectedOutput e
on tw.itemname = e.itemname
WHERE nemails > 1
屈服
item1 email1
item1 email2
item4 email6
item4 email7
我们都去过那里。