SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Finding min using over(order by) in sql server


Finding min using over(order by) in sql server

Author
Message
sunder.mekala
sunder.mekala
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 55
Hi All,

I am trying to write a sql to find the minimum of a dataset
i am using sql server.

select ST.BG_DTTM
,MIN(ST.BG_DTTM) over(ORDER BY ST.BG_DTTM) AS "Minim"
from
tablename ST

but it is giving me a error saying

Error: Incorrect syntax near 'order'. (State:37000, Native Code: 66)


any help is appreciated.

Thanks,
Sam.
Andy Hyslop
Andy Hyslop
SSC Eights!
SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)

Group: General Forum Members
Points: 997 Visits: 3037
Hi

Is there any reason you cant use:


SELECT
MIN(ST.BG_DTTM) 'Minim'
FROM
tablename ST



Cheers

Andy

==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
sunder.mekala
sunder.mekala
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 55
if i go woth your suggestion then i need to group it by the other non aggregated columns, if i group it my results will go wrong, so i am thinking to use min(column1) over (order by column1) .

Any suggestion is appreciated.

Thanks,
Sam.
peacesells
peacesells
Old Hand
Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)

Group: General Forum Members
Points: 342 Visits: 1481
sunder.mekala (2/20/2013)
if i go woth your suggestion then i need to group it by the other non aggregated columns, if i group it my results will go wrong, so i am thinking to use min(column1) over (order by column1) .

Any suggestion is appreciated.

Thanks,
Sam.



Looks like you need to put your minimum part as a sub query if other columns are involved that you can't group by.
Andy Hyslop
Andy Hyslop
SSC Eights!
SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)

Group: General Forum Members
Points: 997 Visits: 3037
if i go woth your suggestion then i need to group it by the other non aggregated columns, if i group it my results will go wrong, so i am thinking to use min(column1) over (order by column1) .


What other columns, there were non specified? Why will the results be wrong, in what way?

Sorry but without a little more information (i.e. DDL's and sample data would be perfect) we just can't see based upon this sorry..

Cheers

Andy

==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
Andy Hyslop
Andy Hyslop
SSC Eights!
SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)SSC Eights! (997 reputation)

Group: General Forum Members
Points: 997 Visits: 3037
Looks like you need to put your minimum part as a sub query if other columns are involved that you can't group by.


If this is what the OP is looking for then maybe..?:

(Just a guess ;-) )


SELECT
<SomeCols>

FROM
Sometable AS ST

INNER JOIN -- If no NULLs are needed from this sub query
(
SELECT
<SomeID>
,MIN(ST.BG_DTTM) 'Minim'
FROM
tablename ST
GROUP BY
,<SomeID>

) AS SubQ

ON ST.<SomeID> = SubQ.<SomeID>





Cheers

Andy

==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (853 reputation)SSC Eights! (853 reputation)SSC Eights! (853 reputation)SSC Eights! (853 reputation)SSC Eights! (853 reputation)SSC Eights! (853 reputation)SSC Eights! (853 reputation)SSC Eights! (853 reputation)

Group: General Forum Members
Points: 853 Visits: 1721
sunder.mekala (2/20/2013)
Hi All,

I am trying to write a sql to find the minimum of a dataset
i am using sql server.

select ST.BG_DTTM
,MIN(ST.BG_DTTM) over(ORDER BY ST.BG_DTTM) AS "Minim"
from
tablename ST

but it is giving me a error saying

Error: Incorrect syntax near 'order'. (State:37000, Native Code: 66)


any help is appreciated.

Thanks,
Sam.

I think you need PARTITION BY rather than ORDER BY when using an aggregate with the OVER() clause.

 
sunder.mekala
sunder.mekala
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 55
select PKG_NME, BG_DTTM, END_DTTM, PKG_STU
from
(select ST.PKG_NME, ST.BG_DTTM, ST.END_DTTM
,ST.PKG_STU
,BG_DTTM = select min(ST.BG_DTTM) from
,row_number() over(partition by ST.PKG_NME order by ST.BG_DTTM desc) as LatestRun
from
dbo.VW_JOB_EXCTN_RPT ST) ST1
where (ST1.LatestRun=1 and
PKG_NME like '%EDW_IDS%FACT')or
(ST1.LatestRun=1 and
PKG_NME like 'MST_EDW%CHILD_MASTER')
order by PKG_NME

Basically i have the above query which gives me PKG_NME which has latest time associated with it.

my requirement is among the PKG_NME from the above dataset which are assocated with BG_DTTM, END_DTTM, i have to find the difference of time between min(BG_DTTM) and max(END_DTTM) from the above dataset ONLY, here the reason why i am saying only is PKG_NME will have different BG_DTTM and END_DTTM as it is a logging table, but here i have find the difference between the LATEST MIN(BG_DTTM)) and max(END_DTTM).

basic table :

PKG_NME BG_DTTM END_DTTM
A 2/20/2013 11:19:50.000 2/20/2013 11:20:45.000
A 2/19/2013 10:20:20.000 2/19/2013 10:21:50.000
B 2/20/2013 11:20:30.000 2/20/2013 11:21:45.000
B 2/19/2013 11:19:50.000 2/19/2013 11:20:45.000
C 2/20/2013 21:38:50.000 2/20/2013 11:40:45.000
C 2/19/2013 11:35:50.000 2/19/2013 11:41:45.000


Above Query gives me

PKG_NME BG_DTTM END_DTTM
A 2/20/2013 11:19:50.000 2/20/2013 11:20:45.000
B 2/20/2013 11:20:30.000 2/20/2013 11:21:45.000
C 2/20/2013 21:38:50.000 2/20/2013 11:40:45.000


now i have to find out the difference min(BG_DTTM) that is 2/20/2013 11:19:50.000
and max(END_DTTM) that is 2/20/2013 11:40:45.000

i should get datediff(MM,min(BG_DTTM),max(END_DTTM)) = 40-19 = 21 Minutes in a seperate column.


Any help is appreciated.

Thanks,
Sam.
sunder.mekala
sunder.mekala
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 55
Sorry the above has some mistakes,pls consider this


select PKG_NME, BG_DTTM, END_DTTM, PKG_STU
from
(select ST.PKG_NME, ST.BG_DTTM, ST.END_DTTM
,ST.PKG_STU
,row_number() over(partition by ST.PKG_NME order by ST.BG_DTTM desc) as LatestRun
from
dbo.VW_JOB_EXCTN_RPT ST) ST1
where (ST1.LatestRun=1 and
PKG_NME like '%EDW_IDS%FACT')or
(ST1.LatestRun=1 and
PKG_NME like 'MST_EDW%CHILD_MASTER')
order by PKG_NME
Erin Ramsay
Erin Ramsay
SSC Eights!
SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)

Group: General Forum Members
Points: 827 Visits: 1099
Wrap the whole thing in another select and get your min and max values.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search