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.
| Author |
Topic |
|
kivancimer
Starting Member
2 Posts |
Posted - 2009-01-04 : 10:35:39
|
HelloI want to make a stored procedure but i am having some errorsQuery is as belowselect 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 belowset ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER 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 intASSET ROWCOUNT @TOPselect 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 destinationorder by case @Sortwhen 'None' then destinationwhen '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 errorMsg 145, Level 15, State 1, Procedure Report_Destination, Line 16ORDER BY items must appear in the select list if SELECT DISTINCT is specified.I would be grateful if anybody can help..thanks in advanceKivanc |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-04 : 10:42:47
|
quote: Originally posted by kivancimer HelloI want to make a stored procedure but i am having some errorsQuery is as belowselect 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 belowset ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER 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 intASSET ROWCOUNT @TOPselect 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 destinationorder by case @Sortwhen 'None' then destinationwhen '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 errorMsg 145, Level 15, State 1, Procedure Report_Destination, Line 16ORDER BY items must appear in the select list if SELECT DISTINCT is specified.I would be grateful if anybody can help..thanks in advanceKivanc
|
 |
|
|
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? |
 |
|
|
kivancimer
Starting Member
2 Posts |
Posted - 2009-01-04 : 14:44:32
|
| Heelo visakh16Yeah 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 |
 |
|
|
shijobaby
Starting Member
44 Posts |
Posted - 2009-08-19 : 05:40:06
|
| HiThe reasons and ways to avoid this error have discussed in this site with good examples. By making small changes in the queryhttp://sqlerror104.blogspot.com/2009/08/order-by-items-must-appear-in-select_19.html |
 |
|
|
|
|
|
|
|