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

Wierd behavior of PARTITION BY Over clause with UNBOUNDED PRECEDING Expand / Collapse
Author
Message
Posted Saturday, March 29, 2014 2:41 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, July 21, 2014 11:32 AM
Points: 372, Visits: 414
having issue sorting this data last date for every minute of data, while last date for min is coming fine, cant make last bid qty using same method, first_value works though??

Seems like a bug, any thoughts?

Meanwhile using other methods to get this done.....

declare @bidData table (activityDate datetime,BidQtyRaw bigint)
insert into @bidData select 'Mar 28 2014 12:03:07:000PM',730384100
insert into @bidData select 'Mar 28 2014 12:03:44:000PM',720528600
insert into @bidData select 'Mar 28 2014 12:03:45:000PM',720528600
insert into @bidData select 'Mar 28 2014 12:19:26:000PM',661768600
insert into @bidData select 'Mar 28 2014 12:19:27:000PM',662768600
insert into @bidData select 'Mar 28 2014 12:19:28:000PM',670718600
insert into @bidData select 'Mar 28 2014 12:19:29:000PM',670718600
insert into @bidData select 'Mar 28 2014 12:19:30:000PM',721218600
insert into @bidData select 'Mar 28 2014 12:19:32:000PM',724218600
insert into @bidData select 'Mar 28 2014 12:50:02:000PM',120538700
insert into @bidData select 'Mar 28 2014 12:50:09:000PM',60678700

select
ActivityDate,datepart(minute,ActivityDate) ActivityMin,BidQtyRaw
,max(BidQtyRaw) OVER
(
PARTITION BY datepart(minute,ActivityDate)
order by ActivityDate desc
Rows UNBOUNDED PRECEDING
) AS BidQtyMin
,first_value(BidQtyRaw) OVER
(
PARTITION BY datepart(minute,ActivityDate)
order by ActivityDate desc
Rows UNBOUNDED PRECEDING
) AS BidQtyMinFirst
,max(ActivityDate) OVER
(
PARTITION BY datepart(minute,ActivityDate)
order by ActivityDate desc
Rows UNBOUNDED PRECEDING
) AS LastActivityDateMin
from @bidData order by ActivityDate




Prakash Heda
Lead DBA Team - www.sqlfeatures.com
Video sessions on Performance Tuning and SQL 2012 HA
Post #1556240
Posted Saturday, March 29, 2014 6:10 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:43 AM
Points: 2,421, Visits: 6,738
Prakash Heda (3/29/2014)
having issue sorting this data last date for every minute of data, while last date for min is coming fine, cant make last bid qty using same method, first_value works though??

Seems like a bug, any thoughts?

Meanwhile using other methods to get this done.....

declare @bidData table (activityDate datetime,BidQtyRaw bigint)
insert into @bidData select 'Mar 28 2014 12:03:07:000PM',730384100
insert into @bidData select 'Mar 28 2014 12:03:44:000PM',720528600
insert into @bidData select 'Mar 28 2014 12:03:45:000PM',720528600
insert into @bidData select 'Mar 28 2014 12:19:26:000PM',661768600
insert into @bidData select 'Mar 28 2014 12:19:27:000PM',662768600
insert into @bidData select 'Mar 28 2014 12:19:28:000PM',670718600
insert into @bidData select 'Mar 28 2014 12:19:29:000PM',670718600
insert into @bidData select 'Mar 28 2014 12:19:30:000PM',721218600
insert into @bidData select 'Mar 28 2014 12:19:32:000PM',724218600
insert into @bidData select 'Mar 28 2014 12:50:02:000PM',120538700
insert into @bidData select 'Mar 28 2014 12:50:09:000PM',60678700

select
ActivityDate,datepart(minute,ActivityDate) ActivityMin,BidQtyRaw
,max(BidQtyRaw) OVER
(
PARTITION BY datepart(minute,ActivityDate)
order by ActivityDate desc
Rows UNBOUNDED PRECEDING
) AS BidQtyMin
,first_value(BidQtyRaw) OVER
(
PARTITION BY datepart(minute,ActivityDate)
order by ActivityDate desc
Rows UNBOUNDED PRECEDING
) AS BidQtyMinFirst
,max(ActivityDate) OVER
(
PARTITION BY datepart(minute,ActivityDate)
order by ActivityDate desc
Rows UNBOUNDED PRECEDING
) AS LastActivityDateMin
from @bidData order by ActivityDate




The problem here is the default framing, when the clause is shortcut-ed, it defaults to the presets. For example, first_value has a default of rows between unbounded preceding and unbounded following while last_value has the default of rows between unbounded preceding and current row. Obviously for an ascending order, that is always going to return the current row.

Compare the output of the previous query to this on;
declare @bidData table (activityDate datetime,BidQtyRaw bigint)
insert into @bidData select 'Mar 28 2014 12:03:07:000PM',730384100
insert into @bidData select 'Mar 28 2014 12:03:44:000PM',720528600
insert into @bidData select 'Mar 28 2014 12:03:45:000PM',720528600
insert into @bidData select 'Mar 28 2014 12:19:26:000PM',661768600
insert into @bidData select 'Mar 28 2014 12:19:27:000PM',662768600
insert into @bidData select 'Mar 28 2014 12:19:28:000PM',670718600
insert into @bidData select 'Mar 28 2014 12:19:29:000PM',670718600
insert into @bidData select 'Mar 28 2014 12:19:30:000PM',721218600
insert into @bidData select 'Mar 28 2014 12:19:32:000PM',724218600
insert into @bidData select 'Mar 28 2014 12:50:02:000PM',120538700
insert into @bidData select 'Mar 28 2014 12:50:09:000PM',60678700

select
ActivityDate,datepart(minute,ActivityDate) ActivityMin,BidQtyRaw
,max(BidQtyRaw) OVER
(
PARTITION BY datepart(minute,ActivityDate)
order by ActivityDate desc
Rows BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING
) AS BidQtyMin
,first_value(BidQtyRaw) OVER
(
PARTITION BY datepart(minute,ActivityDate)
order by ActivityDate desc
Rows BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING
) AS BidQtyMinFirst
,last_value(BidQtyRaw) OVER
(
PARTITION BY datepart(minute,ActivityDate)
order by ActivityDate asc
Rows BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING
) AS BidQtyMinFirstL
,max(ActivityDate) OVER
(
PARTITION BY datepart(minute,ActivityDate)
order by ActivityDate desc
Rows BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING
) AS LastActivityDateMin
from @bidData order by ActivityDate

Post #1556254
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse