簡單分析sql統(tǒng)計(jì)查詢

簡單分析sql統(tǒng)計(jì)查詢

ID:45973033

大小:73.00 KB

頁數(shù):3頁

時(shí)間:2019-11-19

簡單分析sql統(tǒng)計(jì)查詢_第1頁
簡單分析sql統(tǒng)計(jì)查詢_第2頁
簡單分析sql統(tǒng)計(jì)查詢_第3頁
資源描述:

《簡單分析sql統(tǒng)計(jì)查詢》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫

1、示例表關(guān)系如下[v_MatchInReward]視圖如卜CTEATEVIEW[dbo].[v_MatchInReward]ASSELECTdbo.Matchinfo.DemandID,dbo.Matchinfo.DemandUserID,dbo.Matchinfo.MatchType,dbo.Matchinfo.IsReadByDemandProvider,dbo?Matchinfo.Status,dbo.MAtchlnfo.OpertorlD,dbo.Matchinfo.IsAudited,dbo.Matchinfo.IsDeleted,dbo?Reward?BountyAmoun

2、tFROMdbo.MatchinfoINNERJOINdbo.RewardONdbo.MatchInfo.DemandID=dbo.Reward.RewardlD要求按照Matchinfo.DemandUserID分紐.統(tǒng)計(jì)Status=4的次數(shù)IsReadByDemandProvider=lfKj^&BountyAmount金額的總數(shù)以及在MatchlnfolH現(xiàn)的數(shù)據(jù)條數(shù)setstatisticstimeoffsetstatistiesioon/版本一錯誤用法,先使用casewhen再使用count,就相當(dāng)于先根據(jù)casewhen分組,然后才統(tǒng)計(jì),不是真正的按條件/SELECTD

3、emandUserID,IsNull(COUNT(1),0)asInRewardCount,IsNull(SUM(BountyAmount),0)ASBountyAmountCount,IsNull(CASEIsReadByDemandProviderWHEN0THENCOUNT(1)END,0)ASNotSeeCount.IsNull(CASE[Status]WHEN4THENCOUNT(1)END,0)ASCandidateCountFROMv_MatchInRewardWHEREDemandUserID=1ANDMatchType=3GROUPBYDemandUserID,Is

4、ReadByDemandProvider,[Status]/版本二累贅用法,使用主查詢分組,多個子查詢來統(tǒng)計(jì),造成多次表掃描/SELECTm.DemandUserIDfIsNull(COUNT(1),0)asInRewardCountzIsNull((selectSUM(BountyAmount)fromRewardinnerjoinmatchinfoonRewardID=matchinfo.DemandIDwherematchinfo.DemandUserID=m?DemendUserlD),0)ASBountyAmountCountzIsNull((selectcount(1)f

5、rommatchinfowhereIsReadByDemandProvider=0anddemandUserId=m?DemandUserlD)z0)ASNotSeeCount,IsNull((selectcount(l)frommatchinfowhere[Status]=4anddemandUserId=m?DemandUserlD)z0)ASCandidateCountFROMmatchinfoasmWHEREm.DemandUserID=1ANDm.MatchType=3GROUPBYm?DemandUserlD版本三止確用法,在count函數(shù)屮使用casewhen,不會導(dǎo)致

6、多層級分組,得到的是真實(shí)的數(shù)據(jù)缺陷是,count雖然沒有統(tǒng)計(jì)ssewhen得到的rw□值(不管設(shè)定沒設(shè)定),但是消息框中還是有整告,盡管沒有很實(shí)際的數(shù)據(jù)說明這種情況的不妥,但還是要避免SELECTm?DemandUserlD,IsNull(COUNT(1),0)asInRewardCount,IsNull(SUM(BountyAmount)z0)ASBountyAmountCount,IsNull(count(caseIsReadByDemandProviderwhen0then1end),0)ASNotSeeCount,IsNull(count(case[Status]when4t

7、hen1end),0)ASCandidateCountFROMmetchinfoasminnerjoinRewardasronr.RewardID=m.DemAndlDWHEREm.DemandUserID=1ANDm.MatchType=3GROUPBYm.DemandUserlD/版本四優(yōu)化-,結(jié)合視圖在sum函數(shù)屮使用casewhen和count的原理是一樣的,不會導(dǎo)致多層級分紐.,得到的是真實(shí)的數(shù)據(jù),而且sum函數(shù)也很好的避免了rm丄丄值統(tǒng)計(jì)時(shí)的警

當(dāng)前文檔最多預(yù)覽五頁,下載文檔查看全文

此文檔下載收益歸作者所有

當(dāng)前文檔最多預(yù)覽五頁,下載文檔查看全文
溫馨提示:
1. 部分包含數(shù)學(xué)公式或PPT動畫的文件,查看預(yù)覽時(shí)可能會顯示錯亂或異常,文件下載后無此問題,請放心下載。
2. 本文檔由用戶上傳,版權(quán)歸屬用戶,天天文庫負(fù)責(zé)整理代發(fā)布。如果您對本文檔版權(quán)有爭議請及時(shí)聯(lián)系客服。
3. 下載前請仔細(xì)閱讀文檔內(nèi)容,確認(rèn)文檔內(nèi)容符合您的需求后進(jìn)行下載,若出現(xiàn)內(nèi)容與標(biāo)題不符可向本站投訴處理。
4. 下載文檔時(shí)可能由于網(wǎng)絡(luò)波動等原因無法下載或下載錯誤,付費(fèi)完成后未能成功下載的用戶請聯(lián)系客服處理。