Skip to content

Instantly share code, notes, and snippets.

@sofakingworld
Last active June 9, 2019 00:38
Show Gist options
  • Select an option

  • Save sofakingworld/29bb8c514a0e1a7dc5aa7c7386bfaa75 to your computer and use it in GitHub Desktop.

Select an option

Save sofakingworld/29bb8c514a0e1a7dc5aa7c7386bfaa75 to your computer and use it in GitHub Desktop.
;WITH hours as (
...
),
incomes as (
SELECT *
FROM sensor_triggers
WHERE ...
GROUP BY hour(created_at)
),
outcomes as (
SELECT *
FROM sensor_triggers
WHERE ...
GROUP BY hour(created_at)
),
rows as (
SELECT *
FROM hours
LEFT JOIN incomes on incomes.hour = hours.hour
LEFT JOIN outcomes on outcomes.hour = hours.hour
),
final_row as (
SELECT
'Итог' literal
sum(rows.incomes),
sum(rows.outcomes)
FROM rows
)
SELECT * FROM rows
UNION
select * FROM final_row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment