Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Complicated SQL...need order by advice

Author  Topic 

amandarobe
Starting Member

2 Posts

Posted - 2013-01-28 : 15:30:44
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-28 : 15:46:33
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 - 2013-01-28 : 16:50:16
That worked great! Thanks so very much!
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-28 : 17:42:15
You are quite welcome - glad to be of help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-29 : 02:42:58
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
   

- Advertisement -