郎朗坤
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語句的結(jié)果是3 ?第二條結(jié)果是4呢 百思不得其解
(我想通了 一旦when中匹配case的條件 就會(huì)跳出case循環(huán) 這個(gè)我懂了 下面還一個(gè)問題)
?
?
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
?
?
這個(gè)語句 大意就是查詢 vr_tagid 在?VR_WorkCheckDetail ?一月出現(xiàn)的次數(shù) 二月出現(xiàn)的次數(shù) ?三月... ?以此類推 ?但是我發(fā)現(xiàn),比如只要一月有了一次以上的次數(shù) 那么后面所有月份的次數(shù)都是0 ?這是為什么呢 求大神指教?。。?
4 回答

慕娘9325324
TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
感覺要用子查詢,
select a.VR_tagID,hr_maindeptid,Hr_staffName ,
(select count(a.VR_tagID)? from table where case when datepart(mm,writeTime)=1 ) as ?Jan,

慕仙森
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
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
?
你這個(gè)語句有錯(cuò)誤。這個(gè)應(yīng)該是行轉(zhuǎn)列的吧?你這樣寫,會(huì)出現(xiàn)你看到的現(xiàn)象,而你的需求肯定不是這樣的, 修改:
對(duì)每個(gè)case 都增加一個(gè)MAX聚合:

胡說叔叔
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
我嘗試你的寫法 報(bào)錯(cuò): ?不能對(duì)包含聚合或子查詢的表達(dá)式執(zhí)行聚合函數(shù)。

心有法竹
TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
需求其實(shí)是這樣的 ?vr_tagid是標(biāo)識(shí)卡列 代表一個(gè)人 而
VR_WorkCheckDetail 是記錄標(biāo)識(shí)卡每個(gè)月出現(xiàn)的次數(shù) 現(xiàn)在我就是想統(tǒng)計(jì)出 所有人 每個(gè)月 出現(xiàn)的次數(shù)
- 4 回答
- 0 關(guān)注
- 1356 瀏覽
添加回答
舉報(bào)
0/150
提交
取消