亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

一個關于sql中 case when then 的語法問題

一個關于sql中 case when then 的語法問題

郎朗坤 2018-12-07 07:13:04
《1》 ?select case 3 when 1 then 1 when 2 then 2 when 3 then 3 when 3 then 4 end from test; 《2》 ?select case 3 when 1 then 1 when 2 then 2 when 3 then 4 when 3 then 3 end from test; ? 我想問的是 為什么第一條sql語句的結果是3 ?第二條結果是4呢 百思不得其解 (我想通了 一旦when中匹配case的條件 就會跳出case循環 這個我懂了 下面還一個問題) ? ? select a.VR_tagID,hr_maindeptid,Hr_staffName ,(case when datepart(mm,writeTime)=1 then count(a.VR_tagID) else 0 end) Jan,(case when datepart(mm,writeTime)=2 then count(a.VR_tagID) else 0 end) Feb,(case when datepart(mm,writeTime)=3 then count(a.VR_tagID) else 0 end) Mar,(case when datepart(mm,writeTime)=4 then count(a.VR_tagID) else 0 end) Apr,(case when datepart(mm,writeTime)=5 then count(a.VR_tagID) else 0 end) May,(case when datepart(mm,writeTime)=6 then count(a.VR_tagID) else 0 end) June,(case when datepart(mm,writeTime)=7 then count(a.VR_tagID) else 0 end) July,(case when datepart(mm,writeTime)=8 then count(a.VR_tagID) else 0 end) Aug,(case when datepart(mm,writeTime)=9 then count(a.VR_tagID) else 0 end) Sept,(case when datepart(mm,writeTime)=10 then count(a.VR_tagID) else 0 end) Oct,(case when datepart(mm,writeTime)=11 then count(a.VR_tagID) else 0 end) Nov,(case when datepart(mm,writeTime)=12 then count(a.VR_tagID) else 0 end) Dec ,count(a.VR_tagID) Toal from dbo.VR_WorkCheckDetail ainner join hr_staff b on a.VR_TagID=b.VR_TagID group by datepart(mm,writeTime), a.VR_tagID,hr_maindeptid,Hr_staffName ? ? 這個語句 大意就是查詢 vr_tagid 在?VR_WorkCheckDetail ?一月出現的次數 二月出現的次數 ?三月... ?以此類推 ?但是我發現,比如只要一月有了一次以上的次數 那么后面所有月份的次數都是0 ?這是為什么呢 求大神指教?。?!
查看完整描述

4 回答

?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

感覺要用子查詢,

select a.VR_tagID,hr_maindeptid,Hr_staffName ,
(select count(a.VR_tagID)? from table where case when datepart(mm,writeTime)=1 ) as ?Jan,

查看完整回答
反對 回復 2019-01-07
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

select a.VR_tagID,hr_maindeptid,Hr_staffName ,
max(case when datepart(mm,writeTime)=1 then count(a.VR_tagID) else 0 end) Jan,
max(case when datepart(mm,writeTime)=2 then count(a.VR_tagID) else 0 end) Feb,
max(case when datepart(mm,writeTime)=3 then count(a.VR_tagID) else 0 end) Mar,
max(case when datepart(mm,writeTime)=4 then count(a.VR_tagID) else 0 end) Apr,
max(case when datepart(mm,writeTime)=5 then count(a.VR_tagID) else 0 end) May
,
max(case when datepart(mm,writeTime)=6 then count(a.VR_tagID) else 0 end) June
,
max(case when datepart(mm,writeTime)=7 then count(a.VR_tagID) else 0 end) July
,
max(case when datepart(mm,writeTime)=8 then count(a.VR_tagID) else 0 end) Aug

,
max(case when datepart(mm,writeTime)=9 then count(a.VR_tagID) else 0 end) Sept
,
max(case when datepart(mm,writeTime)=10 then count(a.VR_tagID) else 0 end) Oct
,
max(case when datepart(mm,writeTime)=11 then count(a.VR_tagID) else 0 end) Nov
,
max(case when datepart(mm,writeTime)=12 then count(a.VR_tagID) else 0 end) Dec 
,count(a.VR_tagID) Toal
 
from dbo.VR_WorkCheckDetail a
inner join hr_staff b on a.VR_TagID=b.VR_TagID
 
group by datepart(mm,writeTime), a.VR_tagID,hr_maindeptid,Hr_staffName

?

你這個語句有錯誤。這個應該是行轉列的吧?你這樣寫,會出現你看到的現象,而你的需求肯定不是這樣的, 修改:

對每個case 都增加一個MAX聚合:

查看完整回答
反對 回復 2019-01-07
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

我嘗試你的寫法 報錯: ?不能對包含聚合或子查詢的表達式執行聚合函數。

查看完整回答
反對 回復 2019-01-07
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

需求其實是這樣的 ?vr_tagid是標識卡列 代表一個人 而

VR_WorkCheckDetail  是記錄標識卡每個月出現的次數 現在我就是想統計出 所有人 每個月 出現的次數
查看完整回答
反對 回復 2019-01-07
  • 4 回答
  • 0 關注
  • 1357 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號