SQL almost stumped me

For the last several workdays, I have been stumped by what should be a simple SQL query that has been giving me some grief. I have not had to create this particular type of query before, and I got to the last part of the query and could not get the numbers to add up. 

This hasn't happened to me in a while. I don't have anyone to reach out to who would be able to help. I also can't get answers on forums because of the unique nature of what I'm doing. I kept coming back to it, wracking my brain, until I finally had a breakthrough today. I didn't actually solve the problem, but I found a backdoor to the solution. 

Rather than query for the results directly, I queried for the subset of records that do NOT meet the condition, which is easier to do. Then I put those records in a temporary table and excluded them when I ran the main query. If the universe of records U includes mutually exclusive subsets A and B, I was having the hardest time finding B. So, I ran a query in U to get A, popped those records in a temporary table, and then ran a query in U that excluded all the records in A to get only the records in B. It's messy but it worked. 

It still bugs me that I did not successfully identify the B records directly. Maybe one day I'll have an epiphany and figure out why my method was not working. In the meantime, I have what I need to move forward with my tasks for the client.

It still bugs me that I did not successfully identify the B records directly.

I know what you mean. I hate it when i can't do something the proper way and have to resort to a hack.

2021-04-21 01:01:37