SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Complicated SQL...need order by advice
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

amandarobe
Starting Member

2 Posts

Posted - 01/28/2013 :  15:30:44  Show Profile  Reply with Quote
I have the following SQL statement and I need to order by fp.group_id. When I try to use the order clause at the end as I have it below, I get an error saying: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

My eyes are starting to cross, so I'm hoping someone can tell me where to place the order by clause so that the data is sorted by fp.group_id. I have tried several options and none seem to work.

select distinct lh.lid,ROUND(cast(lh.value as numeric),1),fc.minor_stage,fc.moderate_stage,fc.major_stage,(select fp.group_id from fpinfo fp where lh.lid=fp.lid order by fp.group_id),
(select MAX(validtime) from fcstheight where basistime>'2013-01-25 20:00:00' and fcstheight.lid=lh.lid and ts='FF' and value=(select
MAX(value) from fcstheight fta where fta.lid=lh.lid and fta.basistime=(select MAX(basistime) from fcstheight where
fta.lid=fcstheight.lid))),(select MAX(value) from fcstheight fta where fta.lid=lh.lid and fta.basistime=(select MAX(basistime) from fcstheight where fta.lid=fcstheight.lid) and fta.basistime>'2013-01-25 20:00:00') from fpinfo fp, latestobsvalue lh,fcstheight ft,floodcat fc where lh.lid=ft.lid and fc.lid=lh.lid and lh.pe='HG' and lh.obstime>'2013-01-25 20:00:00' and
lh.value>(select (minor_stage-1) from floodcat fc where fc.lid=lh.lid) and lh.value>(select MAX(fta.value) from fcstheight fta where fta.lid=lh.lid and fta.basistime=(select MAX(basistime) from fcstheight where fta.lid=fcstheight.lid)) group by fp.group_id,lh.lid,ROUND,fc.minor_stage,fc.moderate_stage,fc.major_stage order by fp.group_id;

James K
Flowing Fount of Yak Knowledge

3652 Posts

Posted - 01/28/2013 :  15:46:33  Show Profile  Reply with Quote
Try changing the ORDER BY clause to one of these
ORDER BY
       group_id;

ORDER BY
       6;
       
Go to Top of Page

amandarobe
Starting Member

2 Posts

Posted - 01/28/2013 :  16:50:16  Show Profile  Reply with Quote
That worked great! Thanks so very much!
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3652 Posts

Posted - 01/28/2013 :  17:42:15  Show Profile  Reply with Quote
You are quite welcome - glad to be of help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/29/2013 :  02:42:58  Show Profile  Reply with Quote
i prefer using column names over position as in future any changes in order of resultset will affect the ordering of resultset unless the developer reflects the change to ORDER BY clause too.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000