簡體   English   中英

將多個布爾列合並為一列

[英]Combine multiple boolean columns into a single column

我正在從 ERP 系統生成報告,其中為用戶提供了一個復選框,該復選框為所選的每個項目返回一個布爾值。 數據庫托管在 SQL Server 上。

但是,用戶也可以選擇具有其他值的合約,如下所示。

在此處輸入圖片說明

我想將類別捕獲為單列,並且我不介意視圖中的行重復。 對於相同的參考 ID,我希望第一行返回Contract ,第二行返回選擇的另一個值。

在此處輸入圖片說明

您可以使用apply

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;

我想一個簡單的方法是:

select *, 'Contract' as [Category] from [TableOne] where [Contract] = 1
union all select *, 'Tender' as [Category] from [TableOne] where [Tender] = 1
union all select *, 'Waiver' as [Category] from [TableOne] where [Waiver] = 1
union all select *, 'Quotation' as [Category] from [TableOne] where [Quotation] = 1
union all select *, '(none)' as [Category] from [TableOne] where [Contract]+[Tender]+[Waiver]+[Quotation] = 0
order by [Reference ID]

請注意,最后一行放在那里以防萬一您需要處理全零的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM