I have an existing question, that I thought would lead me my answer, but now i'm left with another question, and i'm not sure how to solve it. My original quesiton is here: How to find Invoice detail counter column not in numeric order In my table I have the following information when I run the query
Select * from Invoice_detail where Description like 'Service%'
Result:
Detail_ID date_Created Invoice_id Description
1 1/1/18 12:02:03 1 Service 1
2 1/1/18 12:02:04 1 Service 2
3 1/1/18 12:02:05 1 Service 3
4 1/1/18 12:06:03 2 Service 1
5 1/1/18 12:06:04 2 Service 2
6 1/1/18 12:06:05 2 Service 3
7 1/1/18 12:08:03 3 Service 1
8 1/1/18 12:08:04 3 Service 2
9 1/1/18 12:08:05 3 Service 3
10 1/1/18 12:12:03 4 Service 1
12 1/1/18 12:12:05 4 Service 3
13 1/1/18 12:15:05 5 Service 1
15 1/1/18 12:15:05 5 Service 2
Select * from Invoice_Detail returns
:
Detail_ID date_Created Invoice_id Description
1 1/1/18 12:02:03 1 Service 1
2 1/1/18 12:02:04 1 Service 2
3 1/1/18 12:02:05 1 Service 3
4 1/1/18 12:06:03 2 Service 1
5 1/1/18 12:06:04 2 Service 2
6 1/1/18 12:06:05 2 Service 3
7 1/1/18 12:08:03 3 Service 1
8 1/1/18 12:08:04 3 Service 2
9 1/1/18 12:08:05 3 Service 3
10 1/1/18 12:12:03 4 Service 1
12 1/1/18 12:12:05 4 Service 3
13 1/1/18 12:15:05 5 Service 1
14 1/1/18 12:15:05 5 Test
15 1/1/18 12:15:05 5 Service 2
The query for my initial question - I'm running is:
select id.invoice_id
from invoice_detail id
where Description like ('Service%')
group by id.invoice_id
having count(*) <> max(id.detail_id) - min(id.detail_id) + 1;
This query, which was answered in my previoius question is doing exactly what I asked for - However, I found some special cases, where this issue pops up.
I'd only like to return invoice_ID
4 ONLY (above) In this Case - Detail_ID '11' does not exist and was presumably deleted. I do not want to see Invoice_id 5, since the detail_id
Exists, just not for that ticket.
I'm almost thinking I need something like this, too: SQL: find missing IDs in a table