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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Order by -Case- Select Distinct

Author  Topic 

kivancimer
Starting Member

2 Posts

Posted - 2009-01-04 : 10:35:39
Hello
I want to make a stored procedure but i am having some errors

Query is as below
select distinct  destination ,sum(answered) as answered,
sum(siezed) as siezed,sum(holdtime) as holdtime,
sum(totalduration) as totalduration,avg(NER) as NER from report_test(nolock)
group by destination order by sum(holdtime) desc


and Stored Procedure is as below
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Report_Destination]
@InAccount varchar(50),
@InTG varchar(50),
@OutAcc varchar(50),
@OutTG varchar(50),
@Country varchar(50),
@Destination varchar(50),
@Calltype nchar(1)='%',
@RouteClass varchar(20)='%',
@Begindate nchar(10),
@EndDate nchar(10),
@Sort varchar(20),
@TOP int
AS
SET ROWCOUNT @TOP
select distinct destination ,sum(answered) as answered,
sum(siezed) as siezed,sum(holdtime) as holdtime,
sum(totalduration) as totalduration,avg(NER) as NER from report_test(nolock)
where IngressAccount like @InAccount+'%' and IngressTG like @InTG+'%'
and EgressAccount like @OutAcc+'%' and EgressTG like @OutTG+'%'
and Country like @Country+'%' and Destination like @Destination+'%'
and Date>= @Begindate and Date <=@EndDate group by destination
order by case @Sort
when 'None' then destination
when 'Answered' then sum(answered)
when 'Seized' then sum(siezed)
when 'HoldTime' then sum(holdtime)
when 'Minutes' then sum(totalduration)
when 'NER' then avg(NER)
end


I am having this error
Msg 145, Level 15, State 1, Procedure Report_Destination, Line 16
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

I would be grateful if anybody can help..
thanks in advance
Kivanc

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-01-04 : 10:42:47
quote:
Originally posted by kivancimer

Hello
I want to make a stored procedure but i am having some errors

Query is as below
select destination ,sum(answered) as answered,
sum(siezed) as siezed,sum(holdtime) as holdtime,
sum(totalduration) as totalduration,avg(NER) as NER from report_test(nolock)
group by destination
order by answered desc



and Stored Procedure is as below
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Report_Destination]
@InAccount varchar(50),
@InTG varchar(50),
@OutAcc varchar(50),
@OutTG varchar(50),
@Country varchar(50),
@Destination varchar(50),
@Calltype nchar(1)='%',
@RouteClass varchar(20)='%',
@Begindate nchar(10),
@EndDate nchar(10),
@Sort varchar(20),
@TOP int
AS
SET ROWCOUNT @TOP
select distinct destination ,sum(answered) as answered,
sum(siezed) as siezed,sum(holdtime) as holdtime,
sum(totalduration) as totalduration,avg(NER) as NER from report_test(nolock)
where IngressAccount like @InAccount+'%' and IngressTG like @InTG+'%'
and EgressAccount like @OutAcc+'%' and EgressTG like @OutTG+'%'
and Country like @Country+'%' and Destination like @Destination+'%'
and Date>= @Begindate and Date <=@EndDate group by destination
order by case @Sort
when 'None' then destination
when 'Answered' then sum(answered)
when 'Seized' then sum(siezed)
when 'HoldTime' then sum(holdtime)
when 'Minutes' then sum(totalduration)
when 'NER' then avg(NER)
end


I am having this error
Msg 145, Level 15, State 1, Procedure Report_Destination, Line 16
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

I would be grateful if anybody can help..
thanks in advance
Kivanc

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-04 : 12:39:06
whats the purpose of distinct here? you're already grouping by destination so you will get only 1 record per destination along with aggregated values for other fields. Then why use distinct? can you explain?
Go to Top of Page

kivancimer
Starting Member

2 Posts

Posted - 2009-01-04 : 14:44:32
Heelo visakh16
Yeah you are totally right. I thought I have to use distinct when normal sql query generated no errors.But It is working without Distinct keyword.
Thanx a alot for your help
Go to Top of Page

shijobaby
Starting Member

44 Posts

Posted - 2009-08-19 : 05:40:06
Hi

The reasons and ways to avoid this error have discussed in this

site with good examples. By making small changes in the query

http://sqlerror104.blogspot.com/2009/08/order-by-items-must-appear-in-select_19.html
Go to Top of Page
   

- Advertisement -