gpt4 book ai didi

sql - 在 SQL 中交叉应用 bool 值

转载 作者:行者123 更新时间:2023-12-04 02:37:11 24 4
gpt4 key购买 nike

我有下面的表格,我需要创建一个类别列将 bool 值存储为类别。

enter image description here

我想将类别捕获为单个列,并且我不介意 View 中有重复的行。我希望第一行返回契约(Contract),第二行返回所选的其他值,用于相同的引用 ID。

我使用下面的查询实现了他:

select distinct t.*, tt.category
from t cross apply
( values ('Contracts', t.Contracts),
('Tender', t.Tender),
('Waiver', t.Waiver),
('Quotation', t.Quotation)
) tt(category, flag)
where flag = 1;

enter image description here

我如何捕获一个额外的类别,其中契约(Contract)、投标、弃权和报价的所有实例都是0

最佳答案

None 可以直接进入您的 VALUES 子句。您 case 使用 case 表达式作为逻辑。或者,您可以使用 sign() 的技巧:

select distinct t.*, tt.category
from t cross apply
( values ('Contracts', t.Contracts),
('Tender', t.Tender),
('Waiver', t.Waiver),
('Quotation', t.Quotation),
('None', 1 - sign(t.Contracts + t.Tender + t.Waiver + t.Quotation))
) tt(category, flag)
where flag = 1;

我猜你的原始表中没有重复项,所以你应该放弃 SELECT DISTINCT

关于sql - 在 SQL 中交叉应用 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61143131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com