請這個SQL表查詢幫助,我有一個表像SQL多個日期列,價值桶
+---------------------------------------------+
| DateCol1 DateCol2 DateCol3 DateCol4 |
+---------------------------------------------+
| 2014-10-01 null null 2014_10_04 |
| 2014_10_02 2014_10_01 null 2014_10_04 |
| 2014_10_02 null 2014_10_01 2014_10_04 |
| 2014_10_02 null 2014_10_01 null |
+---------------------------------------------+
如果用戶被賦予輸入像沒有fromdate =「2014-10-01」和todate =' 2014-10 = 05' 我必須表現出像
+------------------------------------------------------------------+
| ActivityDate DateCol1_Cnt DateCol2_Cnt DateCol3_Cnt DateCol4_Cnt |
+------------------------------------------------------------------+
| 2014-10-01 1 1 2 0 |
| 2014-10-02 3 0 0 0 |
| 2014-10-03 0 0 0 0 |
| 2014-10-04 0 0 0 3 |
+------------------------------------------------------------------+
我要表明的是活動的Particluar日期
我做了什麼至今數
Select 'Date1_ACty' Activity ,dateColumn1 Activity_Date, Count(1) Activity_count
From MyTable where dateColumn1 between @FromDate and @ToDate
union all
Select 'Date2_ACty' Activity ,dateColumn2 Activity_Date, Count(1) Activity_count
From MyTable where dateColumn2 between @FromDate and @ToDate
union all .....
迄今剩餘列
請幫我很好地解決
問題是我得到DateCol_Acty
,DateCo2_Acty
等作爲列值不是列本身:(
在您展示的模擬結果中,爲什麼2014-10-02會出現兩次?它應該是2014-10-04,對吧?您還在模擬結果中顯示了日期'2014-10-03',但您沒有任何記錄,這意味着您需要有某種日期表,您可以通過左連接對齊該日期表。 .. – 2014-10-27 17:10:58
是的,對不起,2014-10-04。 – Nishanth 2014-10-27 17:17:03