Sql 过滤列+插入
我有两张桌子:Sql 过滤列+插入,sql,sql-server,Sql,Sql Server,我有两张桌子: TABLE1: field1 | field2 | field3 1 5 aaa 2 10 bbb 3 10 ccc 4 10 ddd 5 10 eee 6 6
TABLE1:
field1 | field2 | field3
1 5 aaa
2 10 bbb
3 10 ccc
4 10 ddd
5 10 eee
6 6 fff
7 7 ggg
TABLE2:
will have the insert of all values that contain in field2 >= 2 equals value
so in this case it should be like this:
TABLE2:
field1 | field2 | field3
2 10 bbb
3 10 ccc
4 10 ddd
5 10 eee
我如何知道哪些值具有>=2的相同名称?然后进行此插入?如果我理解,您希望第二个表包含与第一个表中的字段2相关的所有重复项
select field1, field2, field3
into table2
from (select t.*, count(*) over (partition by field2) as cnt
from table1 t
) t
where cnt >= 2;
这将使用select into创建第二个表。如果已存在,请使用“插入…”。改为选择