Holmes Holmes - 3 months ago 8
SQL Question

Always Include Certain Records in Daterange

Sorry if this is too general a question, but I couldn't find much material on this.

I'm wondering if there is any way in SQL or Tableau to always include certain records despite changes in a date range?

For example, I have 200 records that range from 1940-2004 and want 2 or 3 of these records to always be returned in the query (which includes a date range statement) is there a known method?

I'd like to avoid altering the date attributes based on the date range statement itself is possible.

Initial data:

Person_ID | Group | Date
ID 1 2 1-1-2003
ID 2 1 1-1-1994
ID 3 1 1-1-1985
ID 4 1 1-1-1992
ID 5 2 1-1-1991
ID 6 2 1-1-2002
ID 7 1 1-1-2003
ID 8 2 1-1-2005
ID 9 2 1-1-1999
ID 10 1-1-2002
ID 11 1-1-1989


For my results, I want it to be possible so that no matter the daterange I select, ID 10 and ID 11 are included.

SELECT Person_ID
FROM table
WHERE DATE BETWEEN date1 AND date2


Will always yield ID 10 and ID 11 no matter the dates inputted.

Answer

I don't know much about tableau but you can try this...

SELECT Person_ID
FROM table
WHERE (DATE BETWEEN date1 AND date2) OR Person_ID = 10 OR Person_ID = 11

If you want help on figuring out queries try and say what you want as literal as possible using sql words. So in this case you could say "I want to select the person_id from table where the date is between date1 and date2 or if the person_id is 10 or if the person_id is 11". If you ever say but (ex: date between date1 and date2 but if their id is 10 then also do it) then most likely you can put an or there : ). So if I were to do it without the or it sound more normal (in my opinion at least -> "where the date is between date1 and date2 but if the person_id is 10 or 11 also include it"). Hope that helps!