1

I have a table that stores entries along with their timestamp. My node.js API should return both the count of rows and count of entries grouped by hour.

example:

{
    "count":"6",
    "countByHour":[
        "00":"1",
        "08":"2",
        "16":"3"
    ]
}

What should be the SQL query to generate this result?

1 Answer 1

1

You can use grouping sets for that, e.g.:

SELECT hour, COUNT(*) FROM hours GROUP BY GROUPING SETS ((hour), ()) 
ORDER BY hour NULLS FIRST

This will return results like:

 hour | count
------+-------
      | 6
   00 | 1
   08 | 2
   16 | 3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.