I have 3 SQL Server tables
- INC - Incident Tickets
- INT - Interaction tickets
- KB - Knowledge article views
All 3 tables have columns for user ID, ticket number, timestamp. I am trying to develop reporting that will identify when one or more rows exist in KB that have the same user ID and date as a row in either INC or INT. Ideally, my output would be a union of INC and INT with a new column that would list the KB Ticket number for each matching line in a comma separated field. For example given the following lines in each table:
INC Ticket Number | INC user ID | INC Date |
---|---|---|
INC1234 | id123 | 12/22/22 |
INC2345 | id123 | 12/22/22 |
KB Ticket Number | KB user ID | KB Date |
---|---|---|
KB1234 | id123 | 12/22/22 |
KB2345 | id123 | 12/22/22 |
I would get this output:
INC Ticket Number | INC user ID | INC Date | KB Tickets |
---|---|---|---|
INC1234 | id123 | 12/22/22 | KB1234,KB2345 |
INC2345 | id123 | 12/22/22 | KB1234,KB2345 |
The eventual destination for the output is going to be PowerBI. I initially tried to solve the issue in power query, but while I created a formula that successfully generated my desired output it was incredibly time and resource intensive as each table will have 1,000,000 or more lines and it took over 48 hours and never completed. I am trying to handle the comparison in the SQL query, but I am fairly new to SQL and can't seem to figure it out.
I got to the query below which will combine the 3 tables successfully, but only outputs a single match for each row:
select
inc.TicketNumber, inc.OpenTime, inc.Contact,
kb.KBTicketNumber, kb.UpdateTime, kb.ViewedMMID
from
MMITMetrics.dbo.INC_IncidentTickets inc
full join
MMITMetrics.dbo.KB_Use kb on inc.Contact = kb.ViewedMMID
and cast(inc.OpenTime as date) = cast(kb.UpdateTime as date)
where
inc.OpenTime > '2021-01-01 12:00:00.000'
or kb.UpdateTime > '2021-01-01 12:00:00.000'
union
select
int.TicketNumber, int.OpenTime,int.Contact,
kb.KBTicketNumber, kb.UpdateTime, kb.ViewedMMID
from
MMITMetrics.dbo.INT_InteractionTickets int
full join
MMITMetrics.dbo.KB_Use kb on int.Contact = kb.ViewedMMID
and cast(int.OpenTime as date) = cast(kb.UpdateTime as date)
where
int.OpenTime > '2021-01-01 12:00:00.000'
or kb.UpdateTime > '2021-01-01 12:00:00.000'
I am using Microsoft SQL Server Management Studio 18, so I think I need to use the string_agg
function but can't get it to work properly.