2016-08-31 207 views
1

我有一個表(名稱VCAR)選擇日期範圍在兩個日期之間並獲得數

id | createddatetime | 
-------------------------- 
1 |2016-08-20 17:23:53 | 
2 |2016-08-20 17:23:53 | 
3 |2016-08-20 17:23:53 | 
4 |2016-08-20 17:23:53 | 
4 |2016-08-20 17:23:53 | 
4 |2016-08-21 17:23:53 | 
3 |2016-08-21 17:23:53 | 
1 |2016-08-21 17:23:53 | 
1 |2016-08-21 17:23:53 | 
2 |2016-08-24 17:23:53 | 
3 |2016-08-24 17:23:53 | 
4 |2016-08-24 17:23:53 | 
2 |2016-08-29 17:23:53 | 
2 |2016-08-29 17:23:53 | 
3 |2016-08-29 17:23:53 | 
4 |2016-08-29 17:23:53 | 
4 |2016-08-30 17:23:53 | 
2 |2016-08-30 17:23:53 | 
3 |2016-08-30 17:23:53 | 
4 |2016-08-30 17:23:53 | 

我試圖得到的id的數量在特定日期範圍使用

SELECT 
count(CAST(createddatetime AS DATE)), 
CAST(createddatetime AS DATE), 
DAYNAME(CAST(createddatetime AS DATE)) 
FROM vcar 
WHERE 
CAST(createddatetime AS DATE)>=date('2016-08-20') AND 
CAST(createddatetime AS DATE)<=date('2016-08-35') 
GROUP BY CAST(createddatetime AS DATE); 

這給了我o/p,但我也需要顯示在某一天有零點命中的日期。如何做到這一點?

O/P我越來越

-------------------------------------- ------------------------------- ---------------------------------------- 

    count(CAST(createddatetime AS DATE)) | CAST(createddatetime AS DATE) | DAYNAME(CAST(createddatetime AS DATE)) | 

     -------------------------------------- ------------------------------- ---------------------------------------- 
              9 | 2016-08-20     | Saturday        | 
              36 | 2016-08-23     | Tuesday        | 
              4 | 2016-08-24     | Wednesday        | 
     -------------------------------------- ------------------------------- ---------------------------------------- 

我需要什麼

-------------------------------------- ------------------------------- ---------------------------------------- 
count(CAST(createddatetime AS DATE)) | CAST(createddatetime AS DATE) | DAYNAME(CAST(createddatetime AS DATE)) | 
-------------------------------------- ------------------------------- ---------------------------------------- 
            9 | 2016-08-20     | Saturday        | 
            0 | 2016-08-21     | Sunday         | 
            0 | 2016-08-22     | Monday         | 
            36 | 2016-08-23     | Tuesday        | 
            4 | 2016-08-24     | Wednesday        | 
            9 | 2016-08-25     | Thursday        | 
            0 | 2016-08-26     | Friday         |       
-------------------------------------- ------------------------------- ---------------------------------------- 
+1

請添加您的預期輸出。 – 1000111

+0

@ 1000111 Ill add –

+0

假設這些日子沒有出現在您的表格中,您可能需要使用日曆表來獲得零點擊天數。 –

回答

2

日曆表將使這容易得多。既然你沒有任何,那麼下面的查詢就可以完成這項工作。

SELECT 
dateTable.day, 
DAYNAME(dateTable.day) AS dayName, 
COALESCE(yourQuery.total,0) AS cnt 
FROM 
(
    SELECT ADDDATE('2016-08-20', INTERVAL @i:[email protected]+1 DAY) AS DAY 
    FROM (
    SELECT a.a 
    FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a 
    CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b 
    CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c 
    ) a 
    JOIN (SELECT @i := -1) r1 
    WHERE 
    @i < DATEDIFF('2016-08-31', '2016-08-20') 
) AS dateTable 
LEFT JOIN 
(
    SELECT 
     DATE(createddatetime) date, 
     count(id) As total 
    FROM vcar 
    WHERE DATE(createddatetime) BETWEEN '2016-08-20' AND '2016-08-31' 
    GROUP BY date 
) AS yourQuery 
ON dateTable.day = yourQuery.date 
ORDER BY dateTable.day 

注:

dateTable別名內的查詢將生成日期從2016-08-202016-08-31

Check this

+0

當我將日期範圍從2016-08-20更改爲2016-09-31時,它給了我空集 –

+0

範圍需要在'dateTable'別名內的查詢中更改。 – 1000111

+0

我將2016-08-31更改爲2016-09-31這給了我空集 –