ORDERBYDESC on Two columns is not working on Service Portal Widget

Vijay1212
Tera Contributor

I am gliding the most viewed and most rated kb articles in service portal widgets using the query below. But the sorting is not applying properly. Only sorting is applying on single column . Please suggest.

 

gr.addEncodedQuery('GOTO123TEXTQUERY321='+ data.q+'^ORDERBYDESCrating^ORDERBYDESCsys_view_count');

 

Thanks,

Vijay

4 REPLIES 4

Sai Shravan
Mega Sage

Hi @Vijay1212 ,

 

the sorting is not applying as expected because you are using two separate ORDER BY clauses. You can try combining them into a single ORDER BY clause using comma-separated values like this:

 

gr.addEncodedQuery('GOTO123TEXTQUERY321='+ data.q+'^ORDERBYDESC rating, DESC sys_view_count');

 

This should sort the results first by rating in descending order, and then by sys_view_count in descending order as well.

 

Alternatively, if you want to sort by sys_view_count first and then by rating, you can change the order of the columns in the ORDER BY clause like this:

 

gr.addEncodedQuery('GOTO123TEXTQUERY321='+ data.q+'^ORDERBYDESC sys_view_count, DESC rating');

 

This will sort the results first by sys_view_count in descending order, and then by rating in descending order as well.

 

Regards,

Shravan

If my answer solved your issue, please mark my answer as Correct & Helpful

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi @Sai Shravan ,

 

Thanks for your response. I have updated the query as below as you mentioned.

resultGR.addEncodedQuery('GOTO123TEXTQUERY321=' + data.q + '^ORDERBYDESCrating,DESCsys_view_count');

 

with this query, I am getting the records as below order.

NumberView CountRating
KB0028930 75
KB0027001 205
KB0027084 25
KB0027430 05
KB0023716 05
KB0030787 805
KB0030442 05
KB0026826 265
KB0026750 05
KB0026252 1765
KB0030230 15
KB0010676 05
KB0027306 05
KB0029371 05
KB0029351 85

 but i am expecting the result should be as below.

Vijay1212_0-1678459656039.png

Thanks,

Vijay

Hi @Vijay1212 , 

 

To sort the records in descending order by both fields at the same time, try the below code 

resultGR.addEncodedQuery('GOTO123TEXTQUERY321=' + data.q + '^ORDERBYDESCrating,ORDERBYDESCsys_view_count');
Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi @Sai Shravan ,

 

I have modified the query. but issue isnot solved. I am not getting what exactly the issue is.

I have run the query using background script..but no luck.

 

Thanks,

Vijay