开发者

How to fill date gaps in MySQL?

开发者 https://www.devze.com 2022-12-14 02:47 出处:网络
How i can fill date gaps in MySQL? Here is my query: SELECT DATE(posted_at) AS date, COUNT(*) AS total,

How i can fill date gaps in MySQL? Here is my query:

SELECT DATE(posted_at) AS date,
    COUNT(*) AS total,
    SUM(attitude = 'positive') AS positive,
    SUM(attitude = 'neutral') AS neu开发者_如何学Ctral,
    SUM(attitude = 'negative') AS negative
    FROM `messages`
    WHERE (`messages`.brand_id = 1)
    AND (`messages`.`spam` = 0
    AND `messages`.`duplicate` = 0
    AND `messages`.`ignore` = 0)
    GROUP BY date ORDER BY date

It returns proper result set - but i want to fill gaps between dates start and end by zeros. How i can do this?


You'll need to create a helper table and fill it with all dates from start to end, then just LEFT JOIN with that table:

SELECT  d.dt AS date,
        COUNT(*) AS total,
        SUM(attitude = 'positive') AS positive,
        SUM(attitude = 'neutral') AS neutral,
        SUM(attitude = 'negative') AS negative
FROM    dates d
LEFT JOIN
        messages m
ON      m.posted_at >= d.dt
        AND m.posted_at < d.dt + INTERVAL 1 DAYS
        AND spam = 0
        AND duplicate = 0
        AND ignore = 0
GROUP BY
        d.dt
ORDER BY
        d.dt

Basically, what you need here is a dummy rowsource.

MySQL is the only major system which lacks a way to generate it.

PostgreSQL implements a special function generate_series to do that, while Oracle and SQL Server can use recursion (CONNECT BY and recursive CTEs, accordingly).


I don't know whether MySQL will support the following/similar syntax; but if not, then you could just create and drop a temporary table.

--Inputs
declare @FromDate datetime, /*Inclusive*/
        @ToDate datetime    /*Inclusive*/
set @FromDate = '20091101'
set @ToDate = '20091130'

--Query
declare @Dates table (
    DateValue datetime NOT NULL
)
set NOCOUNT ON
while @FromDate <= @ToDate /*Inclusive*/
begin
  insert into @Dates(DateValue) values(@FromDate)
  set @FromDate = @FromDate + 1
end
set NOCOUNT OFF

select  dates.DateValue,
        Col1...
from    @Dates dates
        left outer join SourceTableOrView data on
            data.DateValue >= dates.DateValue
        and data.DateValue < dates.DateValue + 1 /*NB: Exclusive*/
where   ...?
0

精彩评论

暂无评论...
验证码 换一张
取 消