Maybe I’m looking at this problem all wrong, but here goes.
We have a database that is the backend for some CRM software we use in the office. The boss wants a simple query to extract birthdays from clients and their spouses.
The table structure is as follows:
Client Details Table:
Client Name, Client Birthday, Spouse Name, Spouse Birthday, <etc>
I have a simple query with a case select that will determine if the client’s birthday or the spouse’s birthday is any time in the next month.
Query is compacted as follows:
SELECT a.*
FROM
(SELECT
(CASE WHEN <birthday calculation>
ELSE NULL
END) as WhoHasBDay,
ClientName,
SpouseName
FROM
ClientDetailsTable) as a
WHERE
a.WhoHasBDay IS NOT NULL
This will return any entries where the client or the spouse has a birthday in the next X days.
However, if a client AND their spouse has a birthday in the next X days, I would like the query to return the result once for each.
IE, here is the following data
CLIENT X: Birthday in 5 days
CLIENT Y: Spouse's Birthday in 8 days
CLIENT Z: Birthday in 3 days, Spouse's Birthday in 9 days.
The result set I want is
CLIENT X BDAY T+5
CLIENT Y SPOUSE BDAY T+8
CLIENT Z BDAY T+3
CLIENT Z SPOUSE BDAY T+9
Perhaps I’m approaching this problem the wrong way, in which case suggestion on a better method of attacking it would be appreciated. Otherwise if there’s a solution to this, please let me know.
I’d tackle this as a UNION of two separate queries: one for clients and one for their spouses.