2017-04-10 166 views
0

我有一個表,其中包含有關某個日記的類條目的數據。我試圖按周分開結果,所以我每週都會得到這些條目的數量。但是當我按天分組時,我也會得到空值。我想用Null值省略記錄。我怎麼做?SQL Server 2014 - 如何刪除空記錄

我寫了下面的代碼:

SELECT Year(JournalDate) AS YY 
     ,Month(JournalDate) AS MM 
     ,FromClass 
     ,ToClass 
     ,(SELECT Count(JournalID) 
     WHERE Day(JournalDate) >= 1 AND Day(JournalDate)=7) AS CountWeek1 
     ,(SELECT Count(JournalID) 
     WHERE Day(JournalDate) >= 8 AND Day(JournalDate)=14) AS CountWeek2 
     ,(SELECT Count(JournalID) 
     WHERE Day(JournalDate) >= 15 AND Day(JournalDate)=21) AS CountWeek3 
     ,(SELECT Count(JournalID) 
     WHERE Day(JournalDate) >= 22 AND Day(JournalDate)=28) AS CountWeek4 
     ,(SELECT Count(JournalID) 
     WHERE Day(JournalDate) >= 29 AND Day(JournalDate)=31) AS CountWeek5 


    FROM [tblJournal] 

    WHERE [JournalDate] >= '2016-09-01 00:00:00.000' 
    AND FromClass <> ToClass 
    --AND CountWeek1 IS NOT Null 

    GROUP BY Year(JournalDate), Month(JournalDate), Day(JournalDate), FromClass, ToClass 

    ORDER BY YY, MM, FromClass, ToClass 

但我得到空值了。我想刪除空值。

YY MM FrClass ToClass CntWk1 CntWk2 CntWk3 CntWk4 CntWk5 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL 20  NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 12  NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL 29  NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL NULL NULL 
2016 9 1 2 NULL NULL NULL 25  NULL 

如何省略與NULL值的記錄?

編輯:
所以我真的喜歡我的結果是這樣的:

YY MM FrmCls ToClsWk1 Wk2 Wk3 Wk4 Wk5 
2016 9 1 2 12 20 29 25 0 
2016 9 1 3 2 1 6 0 0 
2016 9 1 4 0 1 2 0 0 
2016 9 2 1 0 3 0 2 0 
2016 9 2 3 74 46 84 54 0 
2016 9 2 4 0 0 8 5 0 
2016 9 3 2 0 813 0 0 0 
+0

空值的空白? – maSTAShuFu

+0

或不包含Cntwk1中的所有行 - wk5? – maSTAShuFu

+0

問題:你想要一行,除了最後一列以外的所有值嗎?或者你想要四行(每行至少有一個非空列)? – pmbAustin

回答

1

採取上述數據並插入到一個臨時表。之後,你可以通過suming通過一年的數據,一個月等這樣在你的結果查詢:

SELECT YY, MM, FrClass,ToClass,SUM(cntwk1),SUM(cntwk2),SUM(cntwk3),SUM(cntwk4),SUM(cntwk5) 
FROM #data 
GROUP BY YY, MM, FrClass,ToClass 
+0

謝謝,先生。我如何在代碼中使用該聲明? – user1777929

+0

@ user1777929在group by語句之後。 – Stephanie

+0

這不會工作,因爲它只會給我第4周eek有價值的記錄。所有其他列將爲空。 – user1777929

1

這個怎麼樣

select * from 
(
SELECT Year(JournalDate) AS YY 
     ,Month(JournalDate) AS MM 
     ,FromClass 
     ,ToClass 
     ,isnull((SELECT Count(JournalID) 
     WHERE Day(JournalDate) >= 1 AND Day(JournalDate)=7),0) AS CountWeek1 
     ,isnull((SELECT Count(JournalID) 
     WHERE Day(JournalDate) >= 8 AND Day(JournalDate)=14),0) AS CountWeek2 
     ,isnull((SELECT Count(JournalID) 
     WHERE Day(JournalDate) >= 15 AND Day(JournalDate)=21),0) AS CountWeek3 
     ,isnull((SELECT Count(JournalID) 
     WHERE Day(JournalDate) >= 22 AND Day(JournalDate)=28),0) AS CountWeek4 
     ,isnull((SELECT Count(JournalID) 
     WHERE Day(JournalDate) >= 29 AND Day(JournalDate)=31),0) AS CountWeek5 


    FROM [tblJournal] 

    WHERE [JournalDate] >= '2016-09-01 00:00:00.000' 
    AND FromClass <> ToClass 

    GROUP BY Year(JournalDate), Month(JournalDate), Day(JournalDate), FromClass, ToClass 
) x 
    where CountWeek1 +CountWeek2+CountWeek3+CountWeek4+CountWeek5 <> 0 

    ORDER BY YY, MM, FromClass, ToClass 
+0

謝謝,先生。這工作。但是,有沒有辦法將它們進一步分組?例如;目前,我在1列中得到一個數字,其他列爲0.然後在另一行中有一列的數字在前一行中爲0(對於同一組) – user1777929