Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Distinct A+','+B ...Order By Case @Orderby when 1 A+','+B End 1

Status
Not open for further replies.

PWise

Programmer
Dec 12, 2002
2,633
US
I am trying to write a SP Select Statement to select distinct rows that contains concatenated fields and to sort depending on a parameter value

Code:
Select Distinct A +','+ B As Field1 ,C + ',' + B As Field2,.....
From TableName
inner join ......
Order by 
Case @OrderBy when 1 then A +','+ B end,
Case @OrderBy when 1 then C +','+ D end


This is the error that I get

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

Any workaround??
 
try putting the select distinct into a derived table, then do the order by outside it. Example
Code:
select * from 
(select distinct a+b as field1, c+d as Field2
from table1) myderivedtable
order by field1

"NOTHING is more important in a database than integrity." ESquared
 
Thank You That Worked! Have A star
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top