根据多行更新列值

时间:2020-04-04 12:51:59

标签: sql sql-server distinct

我有一个表格示例:(Col3是位数据类型)

col1 | col2 | col3 
____________________
abc  |  xyz |  0
abc  |  xyz |  1
abc  |  xyz |  0
abc  |  xyz |  0

我要从表中选择不同于col3的内容,如果col3值不同,则将col3值更新为0。 (如果所有col3值都相同,那么distinct将返回不同的行。)

在这里,我期望输出为:

col1 | col2 | col3 
____________________
abc  |  xyz |  0

有帮助吗?

修改

当col3值相同时:

col1 | col2 | col3 
____________________
abc  |  xyz |  0
abc  |  xyz |  0
abc  |  xyz |  0
abc  |  xyz |  0

col1 | col2 | col3 
____________________
abc  |  xyz |  1
abc  |  xyz |  1
abc  |  xyz |  1
abc  |  xyz |  1

EDIT

如果我只想更新几行,那该怎么办?示例:

如果有数据:

col1 | col2 | col3 
____________________
abc  |  xyz |  0
abc  |  xyz |  1
abc  |  xyz |  1
abc  |  xyz |  0
qwe  |  asd |  1
qwe  |  asd |  0
qwe  |  asd |  0
qwe  |  asd |  0

预期输出:

col1 | col2 | col3 
____________________
 abc  |  xyz |  0
 qwe  |  asd |  0

5 个答案:

答案 0 :(得分:1)

这是您想要的吗?

- name: Execute scipt on remote host
  script: <script-file>
  args:
    arg1: val1
    arg2: val2
  register: out 
- name: name: Print output
  debug:
    msg: "{{out.stdout}}"

或者也许:

select distinct col1, col2, 0 as col3
from t;

这也可以用窗口函数表示:

select col1, col2, 0 as col3
from t
group by col1, col2
having min(col3) <> max(col3)
union all
select col1, col2, col3
from t
where not exists (select 1
                  from t
                  where t2.col1 = t.col1 and t2.col2 = t.col2 and
                        t2.col3 <> t.col3
                 );

答案 1 :(得分:1)

您可以group by col1, col2case的{​​{1}}表达式:

col3

请参见demo
结果:

select col1, col2,
  case when min(cast(col3 as int)) = max(cast(col3 as int)) then 1 else 0 end col3
from tablename
group by col1, col2

答案 2 :(得分:1)

您可以使用row_number()函数,cteinner join尝试以下查询,如下所示。

注意:如果要使用重复的一个值更新值,并保留这些条目中的一个值,则在这种情况下,无需再次与表联接。

create table SampleTable( col1 varchar(10)
, col2 varchar(10)
, col3 bit)

insert into SampleTable values
('abc', 'xyz', 0),
('abc', 'xyz', 1),
('abc', 'xyz', 0),
('abc', 'xyz', 0)

--Before update
select * from SampleTable

--Getting rows with duplicate 
; with cte as (SELECT col1
 , col2
 , col3
 , row_number() over (partition by col1, col2, col3 order by (select null)) as cnt
FROM SampleTable
)UPDATE t 
set t.col1 = t.col1
, t.col2 = t.col2
, t.col3 = 1
from SampleTable t
inner join cte on t.col1 = cte.col1
and t.col2 = cte.col2 and t.col3 = cte.col3
and cnt > 1

--After update
select * from SampleTable

这是现场db<>fiddle演示。

这是使用exists的另一种方式。

第二种方法

update t1
set t1.col1 = t1.col1
, t1.col2 = t1.col2
, t1.col3 = 1
from SampleTable t1
where exists (select 1
              from SampleTable t2
              where t1.col1 = t2.col1
                and t1.col2 = t2.col2
                and t1.col3 = t2.col3
              group by col1, col2, col3
              having count(*) > 1)

答案 3 :(得分:1)

您可以使用Constructor constructor = Class.forName("java.lang.String").getConstructor(String.class); String object = (String) constructor.newInstance("Hello");

EXISTS

答案 4 :(得分:0)

对于您的特定示例,您可以按(col1,col2)分组,然后取min(col3)

相关问题