简体   繁体   中英

How do I sort an SSRS report on an aggregate column?

I'm trying to sort a report on a column that is the count of total problem codes. I didn't create the original report and I've never used these before, so I'm trying to figure out how to modify the report without starting from scratch. I tried using a sub-query to give me a column I could use to sort, but I keep running into the same problem whenever I try and sort on the aggregate.

SELECT call_id,
   start_time,
   end_time,
   city,
   CASE
     WHEN state_id IS NULL THEN 999
     ELSE state_id
   END,
   zip_code,
   sex_id,
   call_type_id,
   market_category_id,
   CASE
     WHEN market_code_id IS NULL THEN 999
     ELSE market_code_id
   END,
   problem_category_id,
   gate_id,
   problem_code_id,
   u.username,
   h.user_id,
   p.cat_code,
   p.text
   AS problem_text,
   cat.name
   AS category_name,
   cat.item_code
   AS category_code,
   gat.name
   AS gate_name,
   gat.item_code
   AS gate_code,
   sta.name
   AS state_name,
   Round(Extract(epoch FROM ( end_time - start_time ) / 60) :: NUMERIC, 1)
   AS
   calltimemin,
   Extract(epoch FROM ( end_time - start_time )) :: NUMERIC
   AS calltime,
   (SELECT Count(*) AS category_name_sort
    FROM   hotline_calls h
           left outer join users u
                        ON h.user_id = u.id
           left outer join problem_codes p
                        ON h.problem_code_id = p.id
           left outer join l_list_items cat
                        ON h.call_type_id = cat.id
                           AND cat.list_code = 'cc'
           left outer join l_list_items gat
                        ON h.gate_id = gat.id
                           AND gat.list_code = 'ga'
           left outer join l_list_items sta
                        ON h.state_id = sta.id
                           AND sta.list_code = 'sa')
FROM   hotline_calls h
       left outer join users u
                    ON h.user_id = u.id
       left outer join problem_codes p
                    ON h.problem_code_id = p.id
       left outer join l_list_items cat
                    ON h.call_type_id = cat.id
                       AND cat.list_code = 'cc'
       left outer join l_list_items gat
                    ON h.gate_id = gat.id
                       AND gat.list_code = 'ga'
       left outer join l_list_items sta
                    ON h.state_id = sta.id
                       AND sta.list_code = 'sa'
WHERE  start_time :: DATE BETWEEN ? AND ? 

Report to sort on Total Column

You'll need to name the column. Then you can sort by it.

For example:

select
  ...
  (SELECT Count(*) AS category_name_sort -- this name is irrelevant
    FROM   hotline_calls h
  ...
  ) as my_new_column -- name it here!
from ...
order by my_new_column

You can SORT your table's GROUP in SSRS by the same value as your expression:

=COUNT(Fields!CallID.Value)

This should be on the GROUP's properties and NOT the table's properties.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM