February 20, 2013 at 8:20 am
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.
February 20, 2013 at 9:06 am
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
February 20, 2013 at 10:00 am
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.
February 20, 2013 at 10:04 am
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.
February 20, 2013 at 10:09 am
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
February 20, 2013 at 10:18 am
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
February 20, 2013 at 10:20 am
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.
February 20, 2013 at 10:30 am
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.
February 20, 2013 at 10:34 am
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
February 20, 2013 at 1:17 pm
Wrap the whole thing in another select and get your min and max values.
February 20, 2013 at 5:39 pm
Steven Willis (2/20/2013)
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.
+1 that is definitely the cause of the syntax error.
My thought question: Have you ever been told that your query runs too fast?
My advice:
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?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply