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 «««123

Converting Hour and Minute to Decimal Expand / Collapse
Author
Message
Posted Saturday, August 2, 2014 1:21 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:36 PM
Points: 35,546, Visits: 32,131
Ed Wagner (7/31/2014)
It sounds to me like you're after the number of units picked per hour for a specific period of time. It reminds me of a shop floor productivity report. Is this correct? If so, simply subtract the times and divide the quantity by the difference. This example uses the difference in minutes for precision.

with times as (
select dateadd(minute, -90, getdate()) starting_time,
GETDATE() ending_time,
14 quantity
union all
select dateadd(hour, -2, getdate()) starting_time,
GETDATE() ending_time,
160 quantity)
select starting_time, ending_time, quantity,
convert(numeric(12, 6), DATEDIFF(minute, starting_time, ending_time)) / 60 hours,
round(quantity / convert(numeric(12, 6), DATEDIFF(minute, starting_time, ending_time)) * 60, 3) uph
from times;

Am I over-simplifying this?


No. But it can be made a bit simpler still using the hidden power of the DATETIME data type.

   WITH TestData (StartDT,EndDT,Quantity) AS
(
SELECT DATEADD(mi,-90,GETDATE()), GETDATE(), 14 UNION ALL
SELECT DATEADD(hh,- 2,GETDATE()), GETDATE(), 160
)
SELECT StartDT
,EndDT
,Quantity
,UPH = CAST(Quantity/(CAST(EndDT-StartDT AS DECIMAL(17,12))%1*24) AS DECIMAL(9,3))
FROM TestData
;




--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1598979
Posted Saturday, August 2, 2014 5:13 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:36 PM
Points: 35,546, Visits: 32,131
Eirikur Eiriksson (8/2/2014)
The datetime format is still the same upto and including 2014.


Thanks Eirikur. I've never looked at it but figured it might be so. That does, however, make my disappointment in the DATETIME2 data type even deeper. Except for the precision byte, it's virtually identical to DATETIME... why would they cripple the data type by making direct addition and subtraction impossible?

What adds to the disappointment is that the DATETIME2 data type, even when scaled back to DATETIME2(3) to produce the same number of digits (but, not the same accuracy) as DATETIME is 49% slower than when the same code is used for DATETIME. Yeah, it takes millions of rows for that to really add up but I have millions of rows that I work with.

The DATETIME2 solution that requires DATEPART is also 72% slower than DATETIME with the decimal conversion.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1598996
Posted Sunday, August 3, 2014 9:58 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:36 PM
Points: 35,546, Visits: 32,131
For anyone that would like to do their own testing, here's the test table I've been using for all of this...

   WITH cteRandomDT AS
(
SELECT TOP 1000000
DateTimeDT = RAND(CHECKSUM(NEWID())) * DATEDIFF(dd,'2000','2020') + CAST('2000' AS DATETIME)
FROM sys.all_columns ac1
CROSS JOIN sys.all_columns ac2
)
SELECT DateTimeDT
,DateTime2DT = CAST(DateTimeDT AS DATETIME2(3))
INTO #JBMTest
FROM cteRandomDT
;




--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1599057
Posted Sunday, August 3, 2014 12:25 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 10:22 AM
Points: 2,391, Visits: 6,620
Jeff Moden (8/2/2014)
Eirikur Eiriksson (8/2/2014)
The datetime format is still the same upto and including 2014.


Thanks Eirikur. I've never looked at it but figured it might be so. That does, however, make my disappointment in the DATETIME2 data type even deeper. Except for the precision byte, it's virtually identical to DATETIME... why would they cripple the data type by making direct addition and subtraction impossible?

What adds to the disappointment is that the DATETIME2 data type, even when scaled back to DATETIME2(3) to produce the same number of digits (but, not the same accuracy) as DATETIME is 49% slower than when the same code is used for DATETIME. Yeah, it takes millions of rows for that to really add up but I have millions of rows that I work with.

The DATETIME2 solution that requires DATEPART is also 72% slower than DATETIME with the decimal conversion.


I agree, as anything that sounds too good to be true, datetime2 isn't. Although it saves 3 bytes and it's ansi/ iso compliant, honestly, no improvement over smalldatetime though, same number of bytes and realistically, unless one is doing far fetched statistics, the range of smalldatetime is more than enough.

Post #1599066
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse