First - how do i format my code sample? I have tried ALT+F10 and nothing happened. Tried manually adding the code in the backtick ` symbol but it did not respect the CRLF and the code was a mess.
Below is a working example of what i am trying to accomplish. I am asking for help on the query that has the two UNIONs. It is querying the data from the master client table and the two 1t o Many tables - Client Notes and Client Contact - into a single table. Is there a better/fast way to code this query. These tables are not very wide, but they have hundred of millions of rows.
Background - I am building a view for use in Tableau. The data from three tables needs to be presented to tableau as a single datasource. I created a generic Client Activity table. The tableau designers are happy with the design but i am wondering the best want to populate the client activity table.
IF OBJECT_ID('client', 'U') IS NOT NULL drop table client
IF OBJECT_ID('client_notes', 'U') IS NOT NULL drop table client_notes
IF OBJECT_ID('client_contact', 'U') IS NOT NULL drop table client_contact
IF OBJECT_ID('client_activity', 'U') IS NOT NULL drop table client_activity
create table client (client_id varchar(10), client_open_dt datetime)
create table client_notes (client_id varchar(10), note_dt datetime, notes varchar(156))
create table client_contact (client_id varchar(10), contact_dt datetime, contact_code varchar(5), contact_action varchar(20))
create table client_activity (client_id varchar(10), client_open_dt datetime, client_activity varchar(50), client_activity_dt datetime, client_activity_1 varchar (50), client_activity_2 varchar (50))
insert into client
values
('123', '2021-01-01 11:25:51.720'),
('456', '2021-02-01 11:25:51.720'),
('789', '2021-03-01 11:25:51.720'),
('012', '2021-03-01 11:25:51.720')
/* 1 to M on client. no association with table client_contact */
insert into client_notes
values
('123', '2021-01-02 11:25:51.720','customer was contacted'),
('123', '2021-01-03 11:25:51.720','spoke to customer'),
('123', '2021-01-04 11:25:51.720','custom called back'),
('456', '2021-02-02 11:25:51.720','incorrect contact information'),
('456', '2021-02-03 11:25:51.720','email was returned')
/* 1 to M on client. no association with table client */
insert into client_contact
values
('123', '2021-01-02 15:25:51.720','a','active'),
('123', '2021-01-04 15:25:51.720','e','escalated'),
('789', '2021-02-02 15:25:51.720','nf', 'not fraud')
insert into client_activity(client_id, client_open_dt, client_activity, client_activity_dt, client_activity_1, client_activity_2)
select fc.client_id, fc.client_open_dt, 'client' as ca, NULL as cadt, '' as ca1, '' as ca2
from client fc
left join client_notes cn
on fc.client_id = cn.client_id
left join client_contact cs
on fc.client_id = cs.client_id
where cn.client_id is null and cs.client_id is null
union
select fc.client_id, fc.client_open_dt, 'client notes', cn.note_dt, cn.notes, ''
from client fc
inner join client_notes cn
on fc.client_id = cn.client_id
union
select fc.client_id, fc.client_open_dt, 'client contact', cs.contact_dt, cs.contact_code, cs.contact_action
from client fc
inner join client_contact cs
on fc.client_id = cs.client_id
select client_id, client_open_dt,
client_activity,
case when client_activity = 'client notes' then 'Note Date: ' when client_activity = 'client contact' then 'contact Date: ' else '' end as client_activity_dt_label,
client_activity_dt,
case when client_activity = 'client notes' then 'Note Text: ' when client_activity = 'client contact' then 'contact Code: ' else '' end as client_activity_1_label,
client_activity_1,
case when client_activity = 'client notes' then '' when client_activity = 'client contact' then 'contact Action: ' else '' end as client_activity_2_label,
client_activity_2
from client_activity
order by client_open_dt, client_id, client_activity_dt, client_activity