Getting daily counts for events that don't happen every day

后端 未结 3 1139
醉话见心
醉话见心 2021-01-20 21:29

I have a customer table in which a new row is inserted when a customer signup occurs.

Problem

I want to know the total numb

相关标签:
3条回答
  • 2021-01-20 21:32

    Look here

    Create teble with calendar and join it in your query.

    0 讨论(0)
  • 2021-01-20 21:48

    You're looking for a way to get all the days listed, even those days that aren't represented in your customer table. This is a notorious pain in the neck in SQL. That's because in its pure form SQL lacks the concept of a contiguous sequence of anything ... cardinal numbers, days, whatever.

    So, you need to introduce a table containing a source of contiguous cardinal numbers, or dates, or something, and then LEFT JOIN your existing data to that table.

    There are a few ways of doing that. One is to create yourself a calendar table with a row for every day in the present decade or century or whatever, then join to it. (That table won't be very big compared to the capability of a modern database.

    Let's say you have that table, and it has a column named date. Then you'd do this.

     SELECT calendar.date AS created,
            ISNULL(a.customer_count, 0) AS customer_count
       FROM calendar
       LEFT JOIN ( 
                SELECT COUNT(*) AS customer_count,
                       DATE(created) AS created
                  FROM customer
                 GROUP BY DATE(created)
            ) a ON calendar.date = a.created
       WHERE calendar.date BETWEEN start AND finish 
       ORDER BY calendar.date
    

    Notice a couple of things. First, the LEFT JOIN from the calendar table to your data set. If you use an ordinary JOIN the missing data in your data set will suppress the rows from the calendar.

    Second, the ISNULL in the toplevel SELECT to turn the missing, null, values from your dataset into zero values.

    Now, you ask, where can I get that calendar table? I respectfully suggest you look that up, and ask another question if you can't figure it out.

    I wrote a little essay on this, which you can find here.http://www.plumislandmedia.net/mysql/filling-missing-data-sequences-cardinal-integers/

    0 讨论(0)
  • 2021-01-20 21:55
    DECLARE @MinDate DATE = '2015-07-01',
            @MaxDate DATE = '2015-07-10';
    
    Create Table tblTempDates 
    (created date, signup int)
    
    insert into tblTempDates
    SELECT  TOP (DATEDIFF(DAY, @MinDate, @MaxDate) + 1)
            Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @MinDate),  0 As Signup
    FROM    sys.all_objects a
            CROSS JOIN sys.all_objects b;
    
    Create Table tblTempQueryDates
    (created date, signup int)
    
    INSERT INTO tblTempQueryDates
    SELECT 
        created AS created, COUNT(scandate) AS signup
    FROM
        customer
    WHERE
        created BETWEEN @MinDate AND @MaxDate
    GROUP BY created
    
    UPDATE    tblTempDates
    SET  tblTempDates.signup = tblTempQueryDates.signup            
    FROM         tblTempDates INNER JOIN
                          tblTempQueryDates ON tblTempDates.created = tblTempQueryDates.created
    
    select * from tblTempDates
    order by created
    
    Drop Table tblTempDates
    Drop Table tblTempQueryDates
    

    Not pretty, but it gives you what you want.

    0 讨论(0)
提交回复
热议问题