How do I include empty rows in a single GROUP BY DAY(date_field) SQL query?

后端 未结 4 1222
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 12:06

I\'m using MS SQL Server but welcome comparitive solutions from other databases.

This is the basic form of my query. It returns the number of calls per day from the

4条回答
  •  北海茫月
    2021-01-18 12:42

    Can you create the set of dates as part of your query? Something along the lines of:

    SELECT COUNT(*) AS Calls, ...
        FROM incidentsm1 RIGHT OUTER JOIN
             (SELECT date_values
                FROM TABLE(('27 Feb 2009'), ('28 Feb 2009'), ('1 Mar 2009'),
                           ('2 Mar 2009'), ('3 Mar 2009'), ('4 Mar 2009'),
                           ('5 Mar 2009')) AS date_list
             )
             ON ...
    

    This is inspired by a sort of hybrid of Informix and DB2 notations and is pretty much guaranteed to be syntactically incorrect in both. Basically, is there a way in your DBMS of creating a literal table on the fly. One possibility - ugly but barely doable - would be to do a 7-way UNION of date literals selected from 'dual' or some table expression that guarantees one row (in Informix terms, SELECT MDY(2,28,2009) FROM "informix".systables WHERE tabid = 1 UNION ...).

提交回复
热议问题