group by date working - 应该返回所有date-psql

时间:2015-01-22 10:24:28

标签: mysql psql

  select user_id,TO_CHAR(TO_TIMESTAMP(date_worked),'YYYY-MM-DD') as "date_worked",exclusion,ROUND(SUM(hours_worked)/3600.00,2) 
    from tablename WHERE (TO_CHAR(TO_TIMESTAMP(date_worked),'YYYY-MM-DD') >= '2015-01-10' AND To_CHAR(TO_TIMESTAMP(date_worked),'YYYY-MM-DD')<= '2015-01-20')
    group by user_id,date_worked,exclusion,hours_worked;

当我执行这样的查询时,我得到了

    user_id    date_worked   exclusion    hours_worked
    ACE1949    2015-01-20    TEST        1
    ACE1949    2015-01-20    WFH        184.64

但我需要像

    user_id    date_worked    exclusion    hours_worked

    ACE1949    2015-01-11                            0
    ACE1949    2015-01-12                            0
    ACE1949    2015-01-13                            0
    ACE1949    2015-01-14                            0
    ACE1949    2015-01-15                            0
    ACE1949    2015-01-16                            0
    ACE1949    2015-01-17                            0
    ACE1949    2015-01-18                            0
    ACE1949    2015-01-19                            0
    ACE1949    2015-01-20    TEST                1
    ACE1949    2015-01-20    WFH        184.64

1 个答案:

答案 0 :(得分:0)

试试这个:

 select user_id,TO_CHAR(TO_TIMESTAMP(date_worked),'YYYY-MM-DD') as    
"date_worked",exclusion,ROUND(SUM(hours_worked)/3600.00,2) 
from tablename WHERE date(date_worked)>= date '2015-01-10' AND   
date(date_worked)<= date '2015-01-20'
group by user_id,date_worked,exclusion,hours_worked;