2

I am using PostgreSQL version 8.1. I have a table as follows:

          datetime     |    usage 
-----------------------+----------
2015-12-16 02:01:45+00 |    71.615
2015-12-16 03:14:42+00 |    43.000
2015-12-16 01:51:43+00 |    25.111
2015-12-17 02:05:26+00 |    94.087

I would like to add the integer values in the usage column based on the date in the datetime column.

Simply, I would like the output to look as below:

          datetime     |    usage 
-----------------------+----------
2015-12-16             |    139.726
2015-12-17             |    94.087

I have tried SELECT dateTime::DATE, usage, SUM(usage) FROM tableName GROUP BY dateTime::DATE, lngusage; which does not perform as expected. Any assistance would be appreciated. Thanks in advance.

1

4 Answers 4

2

Below query should give you the desired result:

select to_char(timestamp, 'YYYY-MM-DD') as time, sum(usage)
from table
group by time
Sign up to request clarification or add additional context in comments.

Comments

0

This one is for postgreSQL, I see you added MySQL also.

SELECT
  dt
  SUM(usage),
FROM (
  SELECT
    DATE_TRUNC('day', datetime) dt,
    usage
  FROM
    tableName
) t
GROUP BY
  dt

Comments

0
SELECT to_char(datetime, 'format'), sum(usage)
FROM table
group by to_char(datetime, 'format')

Comments

0

In addition you could a window function.

SELECT DATETIME
,SUM(USAGE) OVER(PARTITION BY CAST(datetime AS DATE) ORDER BY datetime) AS Usage
FROM TableName

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.