I'm trying to change the header of a column based on a variable
Currently I have
SELECT
(CASE
WHEN GROUPING(CASE @@Role
WHEN 2 THEN Processor
WHEN 3 THEN Reviewer
END) = 1
THEN 'Total'
ELSE (CASE @@Role
WHEN 2 THEN Processor
WHEN 3 THEN Reviewer
END)
END) AS 'User',
COUNT(EntityId) AS 'Tickets Processed'
FROM
table
WHERE
conditions
GROUP BY
CASE @@Role
WHEN 2 THEN Processor
WHEN 3 THEN Reviewer
END WITH ROLLUP
Right now this returns the data I need for the correct role, however is there a way to change the second column's header based on the variable to something like
COUNT(EntityId) AS CASE @@Role
WHEN 2 THEN 'Tickets Processed'
WHEN 3 THEN 'Tickets Reviewed'
END
EDIT:
Sample of current result:
@@Role = 2 or @@Role = 3
Both return:
User Tickets Processed
-----------------------------
Steve 1
Gerald 3
John 1
Paul 2
Peter 5
Total 12
Desired result:
@@Role = 2
User Tickets Processed
-----------------------------
Steve 1
Gerald 3
John 1
Paul 2
Peter 5
Total 12
@@Role = 3
User Tickets Reviewed
-----------------------------
Steve 1
Gerald 3
John 1
Paul 2
Peter 5
Total 12
Sample data
EntityID Processor Reviewer
----------------------------------
1 Peter Bob
2 Peter Paul
3 Peter Bob
4 John Paul
5 Peter Bob
6 Peter Bob
...
You can either use dynamic sql, or you can split the logic based on the @@role
variable:
IF @@Role = 2 THEN {do Query A}
ELSE {do Query B}
But you definitely cannot base the column alias on the value of a variable in the context of a non-dynamic query.
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.