Say I have a table with 3 fields: TransferID, Department, and Program (ignore the fact that I don’t have a unique primary key in this example). For a given TransferID there are several records, each record containing a value for Department, and some containing a value for Program, some being null. Say that I want to return all records in the table, but I want a column that returns the applicable Program value associated with a particular department, for each TransferID.
Example:
1, Sales, 223
1, Martketing, null
1, Maintenance, 228
2, Sales, 334
2, Management, 556
2, Engineering, null
etc…
I want my query to return for each record, the associated Program for the ‘Sales’ department for the specific TransferID. So I would want the result to be:
1, Sales, 223
1, Marketing, 223
1, Maintenance, 223
2, Sales, 334
2, Management, 334
2, Engineering, 334
My query does not work though, it doesn’t return the correct program value as per my example above, and here is what I am doing:
Select t1.TransferID, t1.Department,
(CASE WHEN t1.Program is not null THEN t1.Program where Department = ‘Sales’ else (select t1.Program where t1.Department = 'Sales') end) as ‘Program’
Any ideas?
Thank you
Example:
1, Sales, 223
1, Martketing, null
1, Maintenance, 228
2, Sales, 334
2, Management, 556
2, Engineering, null
etc…
I want my query to return for each record, the associated Program for the ‘Sales’ department for the specific TransferID. So I would want the result to be:
1, Sales, 223
1, Marketing, 223
1, Maintenance, 223
2, Sales, 334
2, Management, 334
2, Engineering, 334
My query does not work though, it doesn’t return the correct program value as per my example above, and here is what I am doing:
Select t1.TransferID, t1.Department,
(CASE WHEN t1.Program is not null THEN t1.Program where Department = ‘Sales’ else (select t1.Program where t1.Department = 'Sales') end) as ‘Program’
Any ideas?
Thank you