将不同行的布尔结果合并到单行中

发布于 2024-12-28 06:42:41 字数 1427 浏览 2 评论 0原文

我有一个返回多行的连接查询

ID    SearchID   Bool1      Bool2    Bool2
1     1          1          1        0
1     1          0          0        0
5     1          1          0        0
6     1          0          0        0
9     1          0          0        0

第一列 ID 可以省略,这让我们得到

SearchID   Bool1      Bool2    Bool2
1          1          1        0
1          0          0        0
1          1          0        0
1          0          0        0
1          0          0        0

由于返回的所有数据都适用于 SearchID 1,我希望最终得到一行,该行将合并所有布尔字段的结果即

SearchID   Bool1      Bool2    Bool2
1          1          1        0

IsButton 应该为 1,因为有一行或多行为 1 AutoRun 应为 1,因为有一行或多行为 1 IsOnMain 应该为 0,因为没有包含 1 的行,

但我想为可能已返回的多行处理此问题,并且我希望为每个 searchid 最终得到一个唯一的行,

ID    SearchID   Bool1      Bool2    Bool2
1     1          1          1        0
1     1          0          0        0
5     1          1          0        0
6     1          0          0        0
9     1          0          0        0
3     2          0          0        0
5     2          0          0        0
3     3          0          0        0
9     3          0          0        1
etc...

SearchID   Bool1      Bool2    Bool2
1          1          1        0
2          0          0        0
3          0          0        1
etc...

我这样做有意义吗?

I have a join query which returns multiple rows

ID    SearchID   Bool1      Bool2    Bool2
1     1          1          1        0
1     1          0          0        0
5     1          1          0        0
6     1          0          0        0
9     1          0          0        0

The first column ID can be omitted, which leaves us with

SearchID   Bool1      Bool2    Bool2
1          1          1        0
1          0          0        0
1          1          0        0
1          0          0        0
1          0          0        0

Since all the data returned applies to SearchID 1, I would like to end up with a row that will merge the results of all the boolean fields i.e.

SearchID   Bool1      Bool2    Bool2
1          1          1        0

IsButton should be 1 as there was one or more row with 1
AutoRun should be 1 as there was one or more row with 1
IsOnMain should be 0 as there was no rows with 1

but I want handle this for multiple rows that may have been returned and I want to end up with a unique row for each searchid

ID    SearchID   Bool1      Bool2    Bool2
1     1          1          1        0
1     1          0          0        0
5     1          1          0        0
6     1          0          0        0
9     1          0          0        0
3     2          0          0        0
5     2          0          0        0
3     3          0          0        0
9     3          0          0        1
etc...

SearchID   Bool1      Bool2    Bool2
1          1          1        0
2          0          0        0
3          0          0        1
etc...

Am I making sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泛泛之交 2025-01-04 06:42:41
SELECT SearchID, 
       CAST(CASE WHEN SUM(CAST(Bool1 AS INT)) > 0 THEN 1 ELSE 0 END AS BIT) AS Bool1,
       CAST(CASE WHEN SUM(CAST(Bool2 AS INT)) > 0 THEN 1 ELSE 0 END AS BIT) AS Bool2,
       CAST(CASE WHEN SUM(CAST(Bool3 AS INT)) > 0 THEN 1 ELSE 0 END AS BIT) AS Bool3
FROM Table
GROUP BY SearchID
SELECT SearchID, 
       CAST(CASE WHEN SUM(CAST(Bool1 AS INT)) > 0 THEN 1 ELSE 0 END AS BIT) AS Bool1,
       CAST(CASE WHEN SUM(CAST(Bool2 AS INT)) > 0 THEN 1 ELSE 0 END AS BIT) AS Bool2,
       CAST(CASE WHEN SUM(CAST(Bool3 AS INT)) > 0 THEN 1 ELSE 0 END AS BIT) AS Bool3
FROM Table
GROUP BY SearchID
懵少女 2025-01-04 06:42:41
SELECT SearchId
     , MAX(CAST(Bool1 AS INT)) AS Bool1
     , MAX(CAST(Bool2 AS INT)) AS Bool2
     , MAX(CAST(Bool3 AS INT)) AS Bool3
FROM TableX
GROUP BY SearchId
SELECT SearchId
     , MAX(CAST(Bool1 AS INT)) AS Bool1
     , MAX(CAST(Bool2 AS INT)) AS Bool2
     , MAX(CAST(Bool3 AS INT)) AS Bool3
FROM TableX
GROUP BY SearchId

如果您使用的是 BIT 数据类型,则无法直接对其使用 SUM 或 MAX 函数。您必须先将其转换。

--Sample table
CREATE TABLE #test
(
    SearchID INT, 
    Bool1 BIT, 
    Bool2 BIT, 
    Bool3 BIT
)
GO

INSERT #test VALUES (1,1,1,0)
INSERT #test VALUES (1,0,0,0)
INSERT #test VALUES (1,1,0,0)
INSERT #test VALUES (1,0,0,0)
INSERT #test VALUES (1,0,0,0)
Go

 SELECT 
    SearchId
    ,MAX(CONVERT(INT,Bool1)) AS Bool1
    ,MAX(CONVERT(INT,Bool2)) AS Bool2
    ,MAX(CONVERT(INT,Bool3)) AS Bool3
FROM #test
GROUP BY SearchId

If you are using a BIT data type then you can't make use of SUM or MAX function on it directly. You have to convert it first.

--Sample table
CREATE TABLE #test
(
    SearchID INT, 
    Bool1 BIT, 
    Bool2 BIT, 
    Bool3 BIT
)
GO

INSERT #test VALUES (1,1,1,0)
INSERT #test VALUES (1,0,0,0)
INSERT #test VALUES (1,1,0,0)
INSERT #test VALUES (1,0,0,0)
INSERT #test VALUES (1,0,0,0)
Go

 SELECT 
    SearchId
    ,MAX(CONVERT(INT,Bool1)) AS Bool1
    ,MAX(CONVERT(INT,Bool2)) AS Bool2
    ,MAX(CONVERT(INT,Bool3)) AS Bool3
FROM #test
GROUP BY SearchId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文