0
votes

Writing a query to select user_ids based on allocation date:

below are the parameters I am using.

  1. allocation start date

  2. allocation end date

  3. from date

  4. to date

The below query is failing to get a result

SELECT 
  DISTINCT user_id
FROM 
  table_name
WHERE
  from_date <= allocation_start_date 
  AND to_date>=allocation_end_date

example:

user_id    allocation start date       allocation end
1                2022-08-10               2022-10-10
2                2022-05-01               2022-06-30
3                2022-05-01               2022-09-10
4                2022-05-01               2022-05-11

if I give from date as 2022-05-01 and to date as 2022-07-30

I should get user ids 2 and 4.

please help, not able to figure out this.

Thank you in advance.

How are you doing the quoting? Date fields have to be quoted like strings. Otherwise, 2022-05-01 is just an integer expression with the value 2016. Show us your code. - Tim Roberts
Even better, illustrate you use case in a fiddle of your choice. For example, sqlfiddle.com - PM 77-1