Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Data Normalization

Author  Topic 

arunlgt
Starting Member

7 Posts

Posted - 2010-03-04 : 16:18:45
Hi everyone: i have a date normalization problem:

I have 2 columns in a table:Addwhen Normalized Add_When
one contains datetime and the other datetime(with the seconds shaved off).I wnat my data in Normalized Add_When normalized such that it rounds off to the nearest 15 minute handle as follows:


Addwhen Normalized Add_When

9:50:3000 9:45:0000

9:47:1500 9:45:0000

9:43:1000 9:30:0000

9:41:1000 9:30:0000

etc....



i want to display the normalized data in a graph in SSRS.

Here is my current code:



USE [Cortex]
GO
/****** Object: StoredProcedure [dbo].[SPNormalizeAddWhen] Script Date: 03/04/2010 16:13:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SPNormalizeAddWhen]
As
declare @colname nvarchar(20)
set @colname='Normalized_Add_When'

if not exists (select * from syscolumns where id=object_id('XXNkk_Fact_PnLSummary1') and name=@colname)
exec('alter table XXNkk_Fact_PnLSummary1 add [' + @colname + '] datetime')


declare @sql nvarchar(500)
set @sql='update XXNkk_Fact_PnLSummary1 set [' + @colname + ']=cast(DATEPART(Hh,[_Addwhen]) as nvarchar(2)) + '':'' + cast(DATEPART(Mi,[_Addwhen]) as nvarchar(2)) '
exec(@sql)





Regards

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-05 : 10:26:13
see logic used here

http://visakhm.blogspot.com/2010/02/aggregating-data-over-time-slots.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

arunlgt
Starting Member

7 Posts

Posted - 2010-03-05 : 14:32:35
my sp works but i need a slight modification,how do i round it to the nearest 15 minutes?


select round(46,-1)= 50 ---> rounds to nearest 10 minute

i want it to round to 15 minutes:

select round(46,?)= 45 ??




Go to Top of Page

arunlgt
Starting Member

7 Posts

Posted - 2010-03-06 : 20:40:22
nothing yet? is it that tough?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-07 : 02:29:58
quote:
Originally posted by arunlgt

nothing yet? is it that tough?


did you check logic used in link at all?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

arunlgt
Starting Member

7 Posts

Posted - 2010-03-07 : 10:26:19
thanks visakh: but my code is very close: it is aggregating to the closest 10 minutes now,just not getting to 15 minutes:here is my code now :


IF OBJECT_ID(N'[dbo].[SPNormalizeAddWhen]') IS NOT NULL
DROP PROCEDURE [dbo].[SPNormalizeAddWhen]

GO

CREATE PROCEDURE [dbo].[SPNormalizeAddWhen]
As
declare @colname nvarchar(20)
set @colname='Normalized Add_When'

if not exists (select * from syscolumns where id=object_id('Risk') and name=@colname)
exec('alter table Risk add [' + @colname + '] datetime')


declare @sql nvarchar(500)
set @sql='update Risk set [' + @colname + ']=cast(DATEPART(yyyy,[add when]) as nvarchar(4)) + ''-'' + cast(DATEPART(mm,[add when]) as nvarchar(2)) + ''-'' + cast(DATEPART(dd,[add when]) as nvarchar(2)) + '' '' + cast(DATEPART(Hh,[add when]) as nvarchar(2)) + '':'' + cast(round(DATEPART(Mi,[add when]),-1) as nvarchar(2)) '
print @sql
exec(@sql)
GO


Go to Top of Page
   

- Advertisement -