|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, March 05, 2013 10:39 AM
Points: 16,
Visits: 26
|
|
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.
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Today @ 1:50 AM
Points: 595,
Visits: 2,138
|
|
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
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, March 05, 2013 10:39 AM
Points: 16,
Visits: 26
|
|
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.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 1:49 PM
Points: 145,
Visits: 701
|
|
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.
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Today @ 1:50 AM
Points: 595,
Visits: 2,138
|
|
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
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Today @ 1:50 AM
Points: 595,
Visits: 2,138
|
|
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
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Yesterday @ 7:19 AM
Points: 283,
Visits: 1,239
|
|
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, March 05, 2013 10:39 AM
Points: 16,
Visits: 26
|
|
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, March 05, 2013 10:39 AM
Points: 16,
Visits: 26
|
|
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
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Today @ 10:46 AM
Points: 421,
Visits: 777
|
|
| Wrap the whole thing in another select and get your min and max values.
|
|
|
|