I am trying to make a query that pulls out all Tickets for a particular company. Within that table will be a column named [Repeat]
What I need the query to do is check to see if there are any other rows that have a matching Circuit_ID within the last 30days of that ticket.
"SELECT [MAIN_TICKET_ID], [CompID], [ActMTTR], [ActOTR], [DtCr], [DtRFC],
CASE WHEN [PRIORITY] = 1 THEN '1'
WHEN [PRIORITY] = 2 THEN '2'
WHEN [PRIORITY] = 3 THEN '3' END AS [PRIORITY],
CASE WHEN ([PRIORITY] = '1' AND [ActMTTR] >= '4' AND ([ResCd7] = 'Equipment (XX)' OR [ResCd7] = 'Lec Facilities (LEC)'))
OR ([PRIORITY] = '1' AND [ActOTR] >= '14' AND ([ResCd7] = 'Equipment (XX)' OR [ResCd7] = 'Lec Facilities (LEC)'))
OR ([PRIORITY] = '2' AND [ActMTTR] >= '6' AND ([ResCd7] = 'Equipment (XX)' OR [ResCd7] = 'Lec Facilities (LEC)'))
OR ([PRIORITY] = '2' AND [ActOTR] >= '16' AND ([ResCd7] = 'Equipment (XX)' OR [ResCd7] = 'Lec Facilities (LEC)'))
OR (([Rpt5] = '1' OR [Rpt30] = '1' OR [Chronic] = '1') AND ([ResCd7] = 'Equipment (XX)' OR [ResCd7] = 'Lec Facilities (LEC)')) THEN 'Yes' ELSE 'No' END AS [Measured],
CASE WHEN [Reviewed] = 1 THEN 'Yes' ELSE 'No' END AS [Reviewed],
CASE WHEN [Rpt5] = 1 OR [Rpt30] = 1 THEN 'Yes' ELSE 'No' End As [Repeat],
CASE WHEN [Chronic] = 1 THEN 'Yes' ELSE 'No' END AS [Chronic],
CASE WHEN [ResCd7] = 'Equipment (XX)' THEN 'XX'
WHEN [ResCd7] = 'Isolated to Customer (ITC)' THEN 'ITC'
WHEN [ResCd7] = 'Information (INF)' THEN 'INF'
WHEN [ResCd7] = 'Test OK (TOK)' THEN 'TOK'
WHEN [ResCd7] = 'Lec Facilities (LEC)' THEN 'LEC'
WHEN [ResCD7] = 'Dispatched No Trouble Found (NTF)' THEN 'NTF'
WHEN [ResCD7] = 'Cleared While Testing (CWT)' THEN 'CWT' END AS [Resolution]
FROM [SNA_Ticket_Detail] WHERE ([CompID] = @CompID)"
Above is the current query which relies on a 0 or 1 flag within the table. Seen on line CASE WHEN [Rpt5] = 1 OR [Rpt30] = 1 THEN 'Yes' ELSE 'No' End As [Repeat],
What I want to do is replace that with something along the lines of
CASE WHEN (SELECT COUNT([XX_CIRCUIT_ID]) FROM SNA_Ticket_Detail WHERE (CONVERT(CHAR(10), [DtRFC], 101) BETWEEN ([DtRFC] - 6) AND ([DtRFC])) AND (XX_CIRCUIT_ID = XX_CIRCUIT_ID)) > '1' THEN 'Yes' ELSE 'No' End As [Repeat],
This doesn't work.. It basically counts all rows instead of just the rows that match the current rows circuit id and fall within the last month.
I don't know how to code this properly to get it to work. or even if it's possible within the query.