按值组合存在与否分组并选择行。

huangapple go评论58阅读模式
英文:

Group by and select rows based on if value combinations exist

问题

我有一个表格 PetsTable

Id Type key value
1 "猫" 10 5
1 "猫" 9 2
2 "狗" 10 5
1 "猫" 8 4
1 "猫" 6 3
2 "狗" 8 4
2 "狗" 6 3
3 "猫" 13 5
3 "猫" 10 0
3 "猫" 8 0

如何将这些数据插入到新表格 MyPets 中,并满足以下条件:

  • 按 Id 分组
  • 仅选择在组内存在 (key = 10 and value = 5)、(key = 8 and value = 4) 和 (key = 6 and value = 3) 的行
  • 如果存在 key = 9,则标记 hasFee = 1,否则 hasFee = 0

最终表格应如下所示:

Id Type hasFee
1 "猫" 1
2 "狗" 0
英文:

I have a table PetsTable:

Id Type key value
1 "Cat" 10 5
1 "Cat" 9 2
2 "dog" 10 5
1 "Cat" 8 4
1 "Cat" 6 3
2 "dog" 8 4
2 "dog" 6 3
3 "Cat" 13 5
3 "Cat" 10 0
3 "Cat" 8 0

How to insert this data into a new table MyPets from PetsTable with these conditions:

  • Group by Id
  • Only select rows when in the group exists (key = 10 and value = 5) and (key = 8 and value = 4) and (key = 6 and value = 3)
  • If exists key = 9, then mark hasFee = 1 else hasFee = 0

Final table should look like:

Id Type hasFee
1 "Cat" 1
2 "dog" 0

答案1

得分: 4

以下是翻译好的部分:

一种方法是使用窗口函数来评估您的条件,然后您可以将其作为条件应用于使用公共表表达式的情况。

这将创建您所需的数据,然后将其轻松插入到您选择的表中。

create table Test (Id int, [Type] varchar(3), [Key] int, [Value] int);

insert into Test (Id, [Type], [Key], [Value])
values
(1, 'Cat', 10, 5),
(1, 'Cat', 9,  2),
(2, 'Dog', 10, 5),
(1, 'Cat', 8,  4),
(1, 'Cat', 6,  3),
(2, 'Dog', 8,  4),
(2, 'Dog', 6,  3),
(3, 'Cat', 13, 5),
(3, 'Cat', 10, 0),
(3, 'Cat', 8,  0);

with cte as (
  select *
    , sum(case when [Key] = 10 and [Value] = 5 then 1 else 0 end) over (partition by Id) Cond1
    , sum(case when [Key] = 8 and [Value] = 4 then 1 else 0 end) over (partition by Id) Cond2
    , sum(case when [Key] = 6 and [Value] = 3 then 1 else 0 end) over (partition by Id) Cond3
    , sum(case when [Key] = 9 then 1 else 0 end) over (partition by Id) HasFee
  from Test
)
select Id, [Type], HasFee
from cte
where Cond1 = 1 and Cond2 = 1 and Cond3 = 1
group by Id, [Type], HasFee;

返回结果:

Id Type HasFee
1 Cat 1
2 Dog 0

注意:如果您以这种格式提供样本数据(DDL+DML),将会使人们更容易提供帮助。

英文:

One approach is to use window functions to evaluate your conditions, which you can then apply as conditions using a CTE.

This creates the data you desire, its then trivial to insert into a table of your choice.

create table Test (Id int, [Type] varchar(3), [Key] int, [Value] int);

insert into Test (Id, [Type], [Key], [Value])
values
(1, 'Cat', 10, 5),
(1, 'Cat', 9,  2),
(2, 'Dog', 10, 5),
(1, 'Cat', 8,  4),
(1, 'Cat', 6,  3),
(2, 'Dog', 8,  4),
(2, 'Dog', 6,  3),
(3, 'Cat', 13, 5),
(3, 'Cat', 10, 0),
(3, 'Cat', 8,  0);

with cte as (
  select *
    , sum(case when [Key] = 10 and [Value] = 5 then 1 else 0 end) over (partition by Id) Cond1
    , sum(case when [Key] = 8 and [Value] = 4 then 1 else 0 end) over (partition by Id) Cond2
    , sum(case when [Key] = 6 and [Value] = 3 then 1 else 0 end) over (partition by Id) Cond3
    , sum(case when [Key] = 9 then 1 else 0 end) over (partition by Id) HasFee
  from Test
)
select Id, [Type], HasFee
from cte
where Cond1 = 1 and Cond2 = 1 and Cond3 = 1
group by Id, [Type], HasFee;

Returns:

Id Type HasFee
1 Cat 1
2 Dog 0

Note: If you provide your sample data in this format (DDL+DML) you make it much easier for people to assist.

db<>fiddle

huangapple
  • 本文由 发表于 2023年6月1日 11:24:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定