Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The table structure has a timestamp(datetime), and a value, similar to this..

SQL
-----timestamp-----             --value--
2009-08-13 08:12:00              200.40
2009-08-13 07:47:00               70.00
2009-08-13 07:33:00               84.00
2009-08-13 07:12:00               350.00
2009-08-12 06:12:00                10.70
2009-08-12 05:12:00                50.86



I want output as following format for a month:

SQL
------------Time-------------                 -----Sum_Value------

28-AUG-2014 06 AM - 29-AUG-2014 06 AM	       553.212
29-AUG-2014 06 AM - 30-AUG-2014 06 AM	       553.212
30-AUG-2014 06 AM - 31-AUG-2014 06 AM	      1053.212



please help me to write a sql query
Posted
Comments
Gihan Liyanage 12-Sep-14 8:24am    
This query returns the sum for the day. I have tested for you. I dont have time now(got to go), Please try and see whether you can improve the query.

select CAST([timestamp] AS DATE), SUM(value) as 'Sum FOr The Day'
from tblExample
group by CAST([timestamp] AS DATE)

I don't have access to an Oracle database. I did this solution using SQL Server 2012. Maybe you can use it to help you with your Oracle solution.

The requirement that a "day" for your query is from 6:00AM on one day until 6:00AM on the next day presents a bit of a challenge. To solve that, I first used a Common Table Expression to sum the values by a date. I use the CASE statement to compute the date. If the time is less than 6AM, then the date is set to the day before. After creating the CTE, I select the results and format the "Date" column to your specification including the dashes in the date.


Create the table
SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblLog](
    [TimeStamp] [datetime] not NULL,
    [Value] [decimal](18, 3) not NULL
) ON [PRIMARY]
GO


Put some data into the table
SQL
insert into tblLog values('2014-09-01 07:00:00',100.1);
insert into tblLog values('2014-09-01 08:00:00',100.1);
insert into tblLog values('2014-09-02 07:00:00',100.1);
insert into tblLog values('2014-09-02 08:00:00',100.1);
insert into tblLog values('2014-09-03 07:00:00',100.1);
insert into tblLog values('2014-09-03 08:00:00',100.1);
insert into tblLog values('2014-09-01 05:00:00',100.1);
insert into tblLog values('2014-09-01 06:00:00',100.1);
insert into tblLog values('2014-09-02 05:00:00',100.1);
insert into tblLog values('2014-09-02 06:00:00',100.1);
insert into tblLog values('2014-09-03 05:00:00',100.1);
insert into tblLog values('2014-09-03 06:00:00',100.1);


Execute the query
SQL
With Sums (LogDate,Sum)
AS
(
SELECT
 CASE
 WHEN CAST(timestamp AS time)<'06:00 am' THEN DATEADD(day,-1,CAST(timestamp AS date)) ELSE CAST(timestamp AS date)
 END AS LogDate,
 SUM(value) AS Sum
 FROM tblLog
 GROUP BY
 CASE
 WHEN CAST(timestamp AS time)<'06:00 am' THEN DATEADD(day,-1,CAST(timestamp AS date)) ELSE CAST(timestamp AS date)
 END
 )
 SELECT CONCAT(REPLACE(CONVERT(NVARCHAR,LogDate,113),' ','-'),' 06 AM - ',REPLACE(CONVERT(NVARCHAR,DATEADD(day,1,LogDate),113),' ','-'),' 06 AM') AS LogDate,Sum from Sums
 ORDER BY CAST(LogDate AS DATE)


The results
LogDate                                 Sum
31-Aug-2014 06 AM - 01-Sep-2014 06 AM	100.100
01-Sep-2014 06 AM - 02-Sep-2014 06 AM	400.400
02-Sep-2014 06 AM - 03-Sep-2014 06 AM	400.400
03-Sep-2014 06 AM - 04-Sep-2014 06 AM	300.300
 
Share this answer
 
v5
Comments
Gihan Liyanage 15-Sep-14 0:19am    
5d..
Smart003 2-Dec-14 7:49am    
working........
Please chechk this

SQL
select trunc(datecolumn) - 1 || 'AM - ' || trunc(datecolumn) || 'AM' as RecordDate,sum(valuecolumn) as AverageValue  from Yourtable group by trunc(datecolumn);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900