SQL Server 2014:如何编写查询?

问题描述 投票:-1回答:2

在我的项目中,我使用的是SQL Server 2014.我遇到了需要跟踪结果的地方。

我有这些表条目:

enter image description here

预期的结果是:

enter image description here

我想在值为1的单行中合并唯一ID的行值。

行值始终为0或1。

我尝试了很多CASE WHEN,但我无法得到预期的结果。

请建议。

sql-server sql-server-2014 case-when
2个回答
1
投票

这应该工作 - 请尝试。

select DISTINCT id,
        MAX(A) OVER (PARTITION BY id) A,
        MAX(B) OVER (PARTITION BY id) B,
        MAX(C) OVER (PARTITION BY id) C
 from tableName

1
投票

假设ABC是一些数字数据类型,你想要这样的东西

 select id,
        max(A) as A,
        max(B) as B,
        max(C) as C
 from your_table
 group by id

如果ABC数据类型是bit然后先投了它

 select id,
        max(cast(A as int)) as A,
        max(cast(B as int)) as B,
        max(cast(C as int)) as C
 from your_table
 group by id
© www.soinside.com 2019 - 2024. All rights reserved.