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 |
|
shubhada
Posting Yak Master
117 Posts |
Posted - 2005-09-28 : 02:55:54
|
| I have created one procedure with one select, which used the Order By clause with case structure.I will pass the column to order by clause on which i want sorting.My Procedure is as below:create proc test(@sortfield char(30)=NULL)AsBegindeclare @orderby numericif(ltrim(rtrim(@sortfield))= 'PayerPricingName')select @orderby =1else if (ltrim(rtrim(@sortfield))= 'RuleName')select @orderby =2else if (ltrim(rtrim(@sortfield))= 'Effective')select @orderby =3else if (ltrim(rtrim(@sortfield))= 'Termination')select @orderby =4select distinct PayerPricing.PayerPricingKey,PayerPricing.PayerPricingName,PayerPricing.RuleName,PayerPricing.Effective,PayerPricing.Termination from PayerPricing PayerPricing ,TermRule,ContractTerms,LookCCntrct,PayerContracts,PayerProfilewhere PayerPricing.Effective = (select max(PayerPricing1.Effective)from PayerPricing PayerPricing1where PayerPricing1.PayerPricingName = PayerPricing.PayerPricingName) andPayerPricing.RuleName = TermRule.RuleName andTermRule.TermKey = ContractTerms.TermKey andContractTerms.ContractID= LookCCntrct.ContractID andPayerContracts.ContractID = LookCCntrct.ContractID andPayerContracts.PUID = PayerProfile.PUID and ORDER BY CASE WHEN @orderby= 1 THEN PayerPricingName WHEN @orderby= 2 THEN RuleName WHEN @orderby = 3 THEN convert(varchar,Effective,120) WHEN @orderby = 4 THEN convert(varchar,Termination,120) else PayerPricingName endreturn endBut this above Procedure gives me one error like :ORDER BY items must appear in the select list if SELECT DISTINCT is specified.Can anyone help me out for the query? Thank you.shubhada |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-28 : 03:07:34
|
| If you use Distinct and Order by Clause then all the columns that appear in Order by Should be selected. In your select query include convert(varchar,Effective,120) and convert(varchar,Termination,120) and see if that worksMadhivananFailing to plan is Planning to fail |
 |
|
|
shubhada
Posting Yak Master
117 Posts |
Posted - 2005-09-28 : 04:48:48
|
| I have tried order by clause without convert function.select distinct PayerPricing.PayerPricingKey,PayerPricing.PayerPricingName,PayerPricing.RuleName,PayerPricing.Effective,PayerPricing.Termination from PayerPricing PayerPricing ,TermRule,ContractTerms,LookCCntrct,PayerContracts,PayerProfilewhere PayerPricing.Effective = (select max(PayerPricing1.Effective)from PayerPricing PayerPricing1where PayerPricing1.PayerPricingName = PayerPricing.PayerPricingName) andPayerPricing.RuleName = TermRule.RuleName andTermRule.TermKey = ContractTerms.TermKey andContractTerms.ContractID= LookCCntrct.ContractID andPayerContracts.ContractID = LookCCntrct.ContractID andPayerContracts.PUID = PayerProfile.PUID and ORDER BY CASE WHEN @orderby= 1 THEN PayerPricingName WHEN @orderby= 2 THEN RuleName WHEN @orderby = 3 THEN EffectiveWHEN @orderby = 4 THEN Terminationelse PayerPricingName endbut still i got same error.Server: Msg 145, Level 15, State 1, Procedure test, Line 81ORDER BY items must appear in the select list if SELECT DISTINCT is specified.shubhada |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2005-09-28 : 05:17:31
|
| you're actually joining tables, be sure to specify the source of the fields in your group byHTH--------------------keeping it simple... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-28 : 05:55:05
|
Try thisSelect * from ( select distinct PayerPricing.PayerPricingKey, PayerPricing.PayerPricingName, PayerPricing.RuleName, PayerPricing.Effective, PayerPricing.Termination from PayerPricing PayerPricing ,TermRule, ContractTerms,LookCCntrct, PayerContracts,PayerProfile where PayerPricing.Effective = (select max(PayerPricing1.Effective) from PayerPricing PayerPricing1 where PayerPricing1.PayerPricingName = PayerPricing.PayerPricingName) and PayerPricing.RuleName = TermRule.RuleName and TermRule.TermKey = ContractTerms.TermKey and ContractTerms.ContractID= LookCCntrct.ContractID and PayerContracts.ContractID = LookCCntrct.ContractID and PayerContracts.PUID = PayerProfile.PUID ) TORDER BY CASE WHEN @orderby= 1 THEN PayerPricingName WHEN @orderby= 2 THEN RuleName WHEN @orderby = 3 THEN EffectiveWHEN @orderby = 4 THEN Terminationelse PayerPricingName end MadhivananFailing to plan is Planning to fail |
 |
|
|
shubhada
Posting Yak Master
117 Posts |
Posted - 2005-09-30 : 07:26:37
|
| hi Madhivanan it is working fine...thanks a lot.shubhada |
 |
|
|
|
|
|
|
|