sql >> Database teknologi >  >> RDS >> PostgreSQL

hvordan får man denne forespørgsel til at returnere rækker med 0 tælleværdi?

        -- CTE to the rescue!!!
WITH cal AS (
        SELECT generate_series('2012-02-02 00:00:00'::timestamp , '2012-03-02 05:00:00'::timestamp , '1 hour'::interval) AS stamp
        )
, qqq AS (
        SELECT date_trunc('hour', executiontime) AS stamp
        , count(*) AS zcount
        FROM mytable
        GROUP BY date_trunc('hour', executiontime)
        )
SELECT cal.stamp
        , COALESCE (qqq.zcount, 0) AS zcount
FROM cal
LEFT JOIN qqq ON cal.stamp = qqq.stamp
ORDER BY stamp ASC
        ;


  1. Langsom opstart af SQL Reporting Services 2008 i Native Mode

  2. SQL vælger gennemsnitsscore over datointerval

  3. Find parameternavnene for en lagret procedure

  4. Problem med operatøren som