What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?

后端 未结 9 1726
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 06:12

I\'m building a quick csv from a mysql table with a query like:

select DATE(date),count(date) from table group by DATE(date) order by date asc;
相关标签:
9条回答
  • 2020-11-22 07:09

    You could use a DateTime object:

    use DateTime;
    my $dt;
    
    while ( my ($date, $sum) = $sth->fetchrow )  {
        if (defined $dt) {
            print CSV $dt->ymd . ",0\n" while $dt->add(days => 1)->ymd lt $date;
        }
        else {
            my ($y, $m, $d) = split /-/, $date;
            $dt = DateTime->new(year => $y, month => $m, day => $d);
        }
        print CSV, "$date,$sum\n";
    }
    

    What the above code does is it keeps the last printed date stored in a DateTime object $dt, and when the current date is more than one day in the future, it increments $dt by one day (and prints it a line to CSV) until it is the same as the current date.

    This way you don't need extra tables, and don't need to fetch all your rows in advance.

    0 讨论(0)
  • 2020-11-22 07:10

    I hope you will figure out the rest.

    select  * from (
    select date_add('2003-01-01 00:00:00.000', INTERVAL n5.num*10000+n4.num*1000+n3.num*100+n2.num*10+n1.num DAY ) as date from
    (select 0 as num
       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) n1,
    (select 0 as num
       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) n2,
    (select 0 as num
       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) n3,
    (select 0 as num
       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) n4,
    (select 0 as num
       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) n5
    ) a
    where date >'2011-01-02 00:00:00.000' and date < NOW()
    order by date
    

    With

    select n3.num*100+n2.num*10+n1.num as date
    

    you will get a column with numbers from 0 to max(n3)*100+max(n2)*10+max(n1)

    Since here we have max n3 as 3, SELECT will return 399, plus 0 -> 400 records (dates in calendar).

    You can tune your dynamic calendar by limiting it, for example, from min(date) you have to now().

    0 讨论(0)
  • 2020-11-22 07:10

    Use some Perl module to do date calculations, like recommended DateTime or Time::Piece (core from 5.10). Just increment date and print date and 0 until date will match current.

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