Hi Guys I wonder if someone maybe able to help me out with a little SQL.
I am trying to create a Derived Table in my universe which looks at a table that contains multiple values of a single Call the table Im looking at is Called Call_Events Each event has different event statuses. I want to create a Derived Table which gives me the unique call reference once, followed by all the call headings in separate columns. If a call has had an event with that heading give me a true or false value in return.
Im not an SQL expert so this is where im asking for a little help. Originally I had something along the lines off;
Select
Distinct (Call_Events.Link_to_Call) as Call_Ref,
Case When Call_Events.Event_Code = ‘A’ Then ‘Yes’ Else ‘No’ end ‘Allocated’,
Case When Call_Events.Event_Code = ‘AK’ Then ‘Yes’ Else ‘No’ end ‘Acknowledged’,
Case When Call_Events.Event_Code = ‘BU’ Then ‘Yes’ Else ‘No’ end ‘Bud Upflt Reqsd’,
Case When Call_Events.Event_Code = ‘C’ Then ‘Yes’ Else ‘No’ end ‘Complete’
From
Call_Events
That doesnt give me one line with everything on it. I think I need to insert a loop and let it keep looking. But im not 100% sure on what the correct code would be? Can anyone point me in the right direction please?..
Maybe the SQL Server syntax differs from Oracle one for the columns naming but…
Try this:
Select
Distinct (Call_Events.Link_to_Call) as Call_Ref,
Case When Call_Events.Event_Code = 'A' Then 'Yes' Else 'No' end as Allocated,
Case When Call_Events.Event_Code = 'AK' Then 'Yes' Else 'No' end as Acknowledged,
Case When Call_Events.Event_Code = 'BU' Then 'Yes' Else 'No' end as Bud_Upflt_Reqsd,
Case When Call_Events.Event_Code = 'C' Then 'Yes' Else 'No' end as Complete
From
Call_Events
SELECT Key,
CASE when first_YES_NO = 1 Then 'YES' Else 'NO' END f_YES_NO,
CASE when second_YES_NO = 1 Then 'YES' Else 'NO' END s_YES_NO,
CASE when third_YES_NO = 1 Then 'YES' Else 'NO' END t_YES_NO
FROM
(
SELECT KEY,
MAX(CASE when code = 'value1' Then 1 Else 0 END) first_YES_NO
MAX(CASE when code = 'value2' Then 1 Else 0 END) second_YES_NO
MAX(CASE when code = 'value3' Then 1 Else 0 END) third_YES_NO
FROM
YOURTABLE
GROUP BY KEY
) A
Let us know your result.
Obviously you can reduce the query size by incorporating both max and yes/no together
select subquery.Call_Ref,
case when subquery.flg_allocated = 0 then 'No' else 'Yes' end as Allocated,
case when subquery.flg_acknowledged = 0 then 'No' else 'Yes' end as Acknowledged,
case when subquery.flg_bud_Upflt_Reqsd = 0 then 'No' else 'Yes' end as Bud_Upflt_Reqsd,
case when subquery.flg_complete = 0 then 'No' else 'Yes' end as Complete
from
(Select
Distinct (Call_Events.Link_to_Call) as Call_Ref,
sum(case when Call_Events.Event_Code = 'A' Then 1 Else 0 end) as flg_allocated,
sum(case When Call_Events.Event_Code = 'AK' Then 1 Else 0 end) as flg_acknowledged,
sum(case When Call_Events.Event_Code = 'BU' Then 1 Else 0 end) as flg_bud_Upflt_Reqsd,
sum(case When Call_Events.Event_Code = 'C' Then 1 Else 0 end) end as flg_complete
From
Call_Events
group by Call_Events.Link_to_Call) subquery