Link to home
Start Free TrialLog in
Avatar of Johan Boog
Johan Boog

asked on

Sorting a SQL script

I start a query :

select  DEPARTMENT ,
sum(WORKLOAD) Number
from  group by DEPARTMENT

The result is a table like this :
DEPARTMENT    NUMBER
1      23
2      12
3      3
5      90

I want to rename some values  :
department no1 : sales
department no2 and no5 : ict
department no3 : management

De query result needs to be :
sales 23
ict 102
management 3
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>I want to rename some values  :
A vastly better idea would be to create a new table named Department with two columns:  DepartmentID (int) and DepartmentName (varchar(50)).
Then you can store the int values in tables, and not the varchar names.  Department names tend the change, and varchar takes more disk memory and perform slower in queries then int values.

But if you're just looking for a quick code fix then use a CASE block like this:

SELECT CASE DEPARTMENT
   WHEN 1 THEN 'Sales'
   WHEN 2 THEN 'Ict'
   WHEN 3 THEN 'Managment'
   WHEN 5 THEN 'Ict' END as DepartmentName, 
   Number
FROM YourTableName

Open in new window

Avatar of Johan Boog
Johan Boog

ASKER

I only have the rights to make a query , I cannot make a new table in the database.
This select case is working fine, but is there a way to combine this with the rest of the query?
Who's taking care of the database?
For use he or she knows about the database model and can provide you the name of the table that has the descriptions you need. I wouldn't be surprised if that table name is DEPARTMENT.
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.
This worke :-)