![](/img/trans.png)
[英]How to split a single column into multiple columns in SQL Server select statement?
[英]SQL Server Select Statement Columns
原始查詢:
select StudyID, count(CompletedDate), count(Removed), count(RemovalReason)
from Study a
full outer join Households b
on a.HouseholdID = b.HouseholdID
where StudyID = '123456'
and Removed = 1
and RemovalReason = 5
group by StudyID
我如何寫出此查詢,以便對於每一列(CompletedDate、Removal 和 RemovalReason)不受條件限制(即 Removed = 1、Removal Reason = 5)並且僅適用於特定列。 如果我執行此查詢,它不會顯示 CompletedDate 的總數,因為我將其限制為這些條件。 有沒有辦法直接寫在count旁邊?
表/列 - 研究:HouseholdID(主鍵)、StudyID、CompletedDate
表/列 - 家庭:HouseholdID(主鍵)、已刪除、RemovalReason
我認為您正在尋找這樣的東西,但您的問題在細節上有點松散:
select StudyID
, count(CompletedDate)
, sum(case when Removed = 1 then 1 else 0 end)
, sum(case when RemovalReason = 5 then 1 else 0 end)
from Study a
join Households b
on a.HouseholdID = b.HouseholdID
where StudyID = '123456'
group by StudyID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.