Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Add Minute Expand / Collapse
Author
Message
Posted Thursday, November 01, 2012 5:09 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 7:40 PM
Points: 1,059, Visits: 1,150
Hi,

I've got quite an unusual question.

I'm running this simple query:

SELECT SourceHub, SnapshotDate, NetworkName, QOSGroup, CountOfRemotes, SumOfTX, SumOfRX
FROM ConfigurationSnapshot

this is the some of the data I get:

SourceHub SnapshotDate NetworkName QOSGroup
nms-Crawford-nss10-T11 2012-08-29 Evolution_AMC12_Network Advantage
nms-Crawford-nss10-T11 2012-08-29 Evolution_AMC12_Network Content Oasis Optimize
nms-Crawford-nss10-T11 2012-08-29 Evolution_AMC12_Network IT telecom

you can see that the snapshotdate is a round date (in this case 2012-08-29).

I want the data to be represented every minute. I mean that the first row in this example will appear 1440 times in this timestamp: 2012-08-29 00:00:00 the next after that is 2012-08-29 00:01:00 and than 2012-08-29 00:02:00 and so on...

is it possible?
Post #1379737
Posted Thursday, November 01, 2012 5:17 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81, Visits: 286
Is it what you want
declare @temptable table
(
datecol date
)

insert into @temptable
select * from
(
values
('10/28/2012'),
('10/28/2012'),
('10/28/2012'),
('10/28/2012'),
('10/28/2012'),
('10/28/2012')
)a (datecol)

select
dateadd(MI,ROW_NUMBER() over (order by datecol)-1,CONVERT(Datetime,datecol)) as DateTimecol
from
@temptable


Post #1379741
Posted Thursday, November 01, 2012 5:38 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 7:40 PM
Points: 1,059, Visits: 1,150
Thanks Bri,

can u plz explain why you put -1 in query?
Post #1379752
Posted Thursday, November 01, 2012 5:41 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 7:40 PM
Points: 1,059, Visits: 1,150
ohhhh I am clear with that now...
Post #1379753
Posted Thursday, November 01, 2012 5:43 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81, Visits: 286
Just Becuase Row_Number() starts with 1 and in 1st row you want to start with 0 min
so -1 in each row
Post #1379755
Posted Thursday, November 01, 2012 6:44 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 12:42 AM
Points: 2,338, Visits: 3,158
Perhaps it is something like this that you seek:

;WITH Dates AS (
SELECT MyDate
FROM (
VALUES ('2012-10-28'),('2012-10-29'),('2012-10-30')) a (MyDate)),
Tally (n) AS (
SELECT TOP (1440) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t1 (n1)
CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t2 (n2)
CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t3 (n3)
CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t4 (n4))
SELECT MyDate, DateWithMinute=DATEADD(minute, n, MyDate)
FROM Dates
CROSS APPLY Tally
ORDER BY DATEADD(minute, n, MyDate)





No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1380096
Posted Thursday, November 01, 2012 8:01 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:21 PM
Points: 32,893, Visits: 26,765
kapil_kk (11/1/2012)
Hi,

I've got quite an unusual question.

I'm running this simple query:

SELECT SourceHub, SnapshotDate, NetworkName, QOSGroup, CountOfRemotes, SumOfTX, SumOfRX
FROM ConfigurationSnapshot

this is the some of the data I get:

SourceHub SnapshotDate NetworkName QOSGroup
nms-Crawford-nss10-T11 2012-08-29 Evolution_AMC12_Network Advantage
nms-Crawford-nss10-T11 2012-08-29 Evolution_AMC12_Network Content Oasis Optimize
nms-Crawford-nss10-T11 2012-08-29 Evolution_AMC12_Network IT telecom

you can see that the snapshotdate is a round date (in this case 2012-08-29).

I want the data to be represented every minute. I mean that the first row in this example will appear 1440 times in this timestamp: 2012-08-29 00:00:00 the next after that is 2012-08-29 00:01:00 and than 2012-08-29 00:02:00 and so on...

is it possible?


Life can get real simple if you have the correct tools. The tool I'm using to make this particular query so easy is called a "Tally Table". (of course, untested because I don't have your tables on my machine)

 SELECT SourceHub, 
SnapshotMinute = DATEADD(mi,t.N,SnapshotDate),
NetworkName,
QOSGroup,
CountOfRemotes,
SumOfTX,
SumOfRX
FROM ConfigurationSnapshot cs
CROSS JOIN dbo.Tally0 t
WHERE t.Number < 1440
;

The Tallly Table used above is a "zero based" Tally Table running from 0 to 11,000 (total). Here's how to build one...

--===== Do this in a nice safe place that everyone has
USE TempDB;
IF OBJECT_ID('TempDB..Tally0','U') IS NOT NULL
DROP TABLE Tally0;
GO

--===================================================================
-- Create a Tally table from 0 to 11000
--===================================================================
--===== Create and populate the Tally table on the fly.
SELECT TOP 11001
IDENTITY(INT,0,1) AS N
INTO dbo.Tally0
FROM Master.sys.ALL_Columns ac1
CROSS JOIN Master.sys.ALL_Columns ac2
;
--===== Add a CLUSTERED Primary Key to maximize performance
ALTER TABLE dbo.Tally0
ADD CONSTRAINT PK_Tally0_N
PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100
;
--===== Allow the general public to use it
GRANT SELECT ON dbo.Tally0 TO PUBLIC
;
GO

... and here's (at the following URL) what it is and how it can be used to easily replace certain types of WHILE loops in a big hurry.
http://www.sqlservercentral.com/articles/T-SQL/62867/

Understanding how a Tally Table (or the equivalent like what Dwain built above) will change your professional database programming life. They make difficult code mere childs play and they blow the doors off of any loop or form of recursion you may care to try in T-SQL.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1380108
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse