Filter results based on another filter's date range - sql

Background:
I have the following user's registration funnel, where user creates account and then goes through prompts with the goal of registering:
id date create_account_date user_creates_account registration_date user_registers
1 12/30/2017 12/30/2017 1 12/30/2017 1
2 12/30/2017 12/30/2017 1 1/2/2018 0
2 1/2/2018 12/30/2017 0 1/2/2018 1
3 12/31/2017 12/31/2017 1 12/31/2017 1
4 1/1/2018 1/1/2018 1 1/3/2018 0
4 1/3/2018 1/1/2018 0 1/3/2018 1
5 1/1/2018 1/1/2018 1 1/1/2018 1
6 1/2/2018 1/2/2018 1 1/3/2018 0
6 1/3/2018 1/2/2018 0 1/3/2018 1
7 1/3/2018 1/3/2018 1 1/3/2018 1
8 1/4/2018 1/4/2018 1 1/4/2018 1
In aggregate:
12/30 12/31 1/1 1/2 1/3 1/4 Total Total 1/2-1/4
User Creates Account 2 1 2 1 1 1 8 3
User Registers 1 1 1 1 3 1 8 5
Issue:
I am trying to add a date filter, where I can pick the date range of the data I want to see.
I added create_account_date as filter, and picked Jan 2 to Jan 4. However, that will only force min(registration date)='1/2/18', while max(registration date) can happen after Jan 4.
I also tried forcing create_account_date = registration_date, but that understates those who registered on a day different from create_account_date, but still within the filtered date range.
Ask:
I would like to be able to filter the output by the date range filter/parameter.
So create_account_date and registration date per user are >= min(date) and create_account_date and registration date per user are <= max(date). Here create_account_date >= registration date
So with filter implementation I would have:
1/2/2018 1/3/2018 1/4/2018 Total
User Creates Account 1 1 1 3
User Registers 0 1 1 2
Thank you in advance.

You are trying in a wrong way don't try to manipulate create account and registration date instead add a filter only for normal date field and place the fields in sheet and see the result.
If you really want to create a filter then you can't add the condition just using normal filter instead you need to create a two parameters.
One parameter for start date
One parameter for end date
For both the parameters use date field to display the list of data
Now create two calcualted fields
If create account date >=[start date parameter] and create account date < [end date parameter]
then
your field
end
Similarly
If registration date >=[start date parameter] and registration date < [end date parameter]
then
your field
end
USe both the fileds in rows and place date in column of sheet

I'm not 100% clear on the goal, but it sounds like you want to find users where the account_create_date is between a start and end date AND the registration_date is between a start and end date. If that's so, you could filter with WHERE (create_account_date BETWEEN #start_date AND #end_date) AND (registration_date BETWEEN #start_date AND #end_date). My syntax assumes SQL Server, but you can make it work for other DBMS too.

Related

SQL Query - Identifying entries between payment dates greater than 6 years

I have this table (in reality it has more fields but for simplicity, it will demonstrate what I'm after)
Payment_Type
Person ID
Payment_date
Payment_Amount
Normal
1
2015-01-01
£1.00
Normal
1
2017-01-01
£2.00
Reversal
1
2022-01-09
£3.00
Normal
2
2016-12-29
£3.00
Reversal
2
2022-01-02
£4.00
I need 2 specific things from this:
I need all entries where there is over 6 years difference between any given payment dates (when its been greater than or equal to 6 years from the date of the latest payment date). I don't need to count them, I just need it to return all the entries that meet this criteria.
I also need it to specify where a normal payment hasn't been made for 6 years or more from todays date but a reversal has however occurred within the last 6 years. (This might need to be a separate query but will take suggestions)
I'm using Data Lake (Hue).
Thank you.
I've tried to run a sub query with join and union but I'm not getting the desired results so will need to start from scratch. Any advice/insight on this is greatly appreciated.
Ideally, query one will show:
Payment_Type
Person ID
Payment_date
Payment_Amount
Normal
1
2015-01-01
£1.00
Normal
1
2017-01-01
£2.00
Normal
2
2016-12-29
£3.00
Query 2 results should show:
Payment_Type
Person ID
Payment_date
Payment_Amount
Normal
1
2017-01-01
£2.00
Reversal
1
2022-01-09
£3.00
Normal
2
2016-12-29
£3.00
Reversal
2
2022-01-02
£4.00

Query repeating event in a given time range

I have a set of events (Time in MM/DD) that can be repeated by different users:
EventId Event Time User
1 Start 06/01/2012 10:05AM 1
1 End 06/05/2012 10:45AM 1
2 Start 07/07/2012 09:55AM 2
2 End 09/07/2012 11:05AM 2
3 Start 09/01/2012 11:05AM 2
3 End 09/03/2012 11:05AM 2
I want to get, using SQL, those events a user has done in a specified time range, for instance:
Given 06/06/2012 and 09/02/2012 I am expecting tho get:
EventId Event Time User
2 Start 07/07/2012 09:55AM 2
2 End 09/07/2012 11:05AM 2
3 Start 09/01/2012 11:05AM 1
Any idea on how to deal with this?
A basic range query should work here:
SELECT *
FROM yourTable
WHERE Time >= '2012-06-06'::date AND Time < '2012-09-03'::date;
This assumes you want records falling on June 6, 2012 to September 2 of the same year.

Combine 2 queries into 1 table with user entering the parameters twice for both queries

My project needs me to come up with a query which can compare any 2 months data side by side, by just keying in the dates of the 2 months.
I have done 2 separate queries that can only do single month data because I can only enter 1 date per query. I tried to combine this 2 separate query into 1 single query by selecting the columns from each table but it gives me blank data.
I will need some help in combining the 2 queries together, into 1 table as a view form and allowing the user to key in the 2 dates they want to get their data from. Below will be the 2 queries result I can achieve and also the end result I want to achieve from combining this 2 queries.
Conditions to merge the two table is that the company will be the same for both dates, and the item the company bought (if any). If the company did not buy the item on the month , data should be blank.
Query 1 : User will enter "First month" they want the data from
Inv Number Company Date Item Price Quantity Total
123 ABC 1/1/2018 Table 5 3 15
123 ABC 1/1/2018 Chair 2 4 8
345 XYZ 1/1/2018 Table 5 5 25
345 XYZ 1/1/2018 Chair 2 6 12
Query 2: User will enter "Second Month" they want the data from
Inv Number Company Date Item Price Quantity Total
999 ABC 1/2/2018 Table 4 3 12
999 ABC 1/2/2018 Chair 2 5 10
899 XYZ 1/2/2018 Table 4 3 12
End result : User will be allowed to key in both dates they want the data from
Inv Number Company Date Item Price Quantity Total Date Item Price Quantity Total Inv Number
123 ABC 1/1/2018 Table 5 3 15 1/2/2018 Table 4 3 12 999
123 ABC 1/1/2018 Chair 2 4 8 1/2/2018 Chair 2 5 10 999
345 XYZ 1/1/2018 Table 5 5 25 1/2/2018 Table 4 3 12 899
345 XYZ 1/1/2018 Chair 2 6 12

pick up date within some range

I have table like this. This is a record of consultant done activity for aftercare when some new candidate starts a job.I have built a report to compare if aftercare is done on time or not. in the report it start showing 3 days ago that aftercare is due on this date. But some consultants do aftercare before or after and also on date which is correct. I am fetching MIN aftercaredone date to check and compare with the aftercare due date and shows in the report if it is correct like below.
MIN(case when DESCRIPTION='Aftercare Pre Start' then DUEDATECONCAND end) DUEDATEPreStart hin
But my min date logic fails here. Actually I like to pick up all aftercaredone dates and compare with aftercareduedate . If any date falls within required period then should show report result correct otherwise fails. I mean if someone did aftercare within 3 days difference from AFtercareduedate then should show correct or otherwise in query it should show N(means not done). And also I need to exclude weekends.
ConsultantID CandiateID candidateNAME AfterCareDueDate AftercareDone
123 1 Bob 01/02/2019 20/02/2019
123 1 Bob 01/02/2019 01/02/2019
123 1 Bob 01/02/2019 07/02/2019
100 2 Rob 01/02/2019 01/02/2019
100 2 Rob 01/02/2019 10/02/2019
200 3 ABC 01/02/2019 20/01/2019

Getting a count by date based on the number of observations with encompassing date ranges

I am working with a table in Microsoft Access whereby I have 2 columns with a start and end date.
I want to get the count by date of the number of rows with date ranges that encompass the date in the output table.
Input Data
Start Date End Date
01/02/2017 03/02/2017
07/02/2017 19/02/2017
09/02/2017 19/02/2017
11/02/2017 12/02/2017
12/02/2017 17/02/2017
Desired Output
Date Count
01/02/2017 1
02/02/2017 1
03/02/2017 1
04/02/2017 0
05/02/2017 0
06/02/2017 0
07/02/2017 1
08/02/2017 1
09/02/2017 2
10/02/2017 2
11/02/2017 3
12/02/2017 4
13/02/2017 3
14/02/2017 3
15/02/2017 3
16/02/2017 3
17/02/2017 3
18/02/2017 2
19/02/2017 2
20/02/2017 0
For this project, I have to use Microsoft Access 2010, so a solution in either SQL code or design view input would be great.
Any help on this would be appreciated. Thanks!
Use the below query to get the required result. You can also change the column with respect to your requirements
SELECT END_DATE AS DATE, COUNT(*) AS COUNT FROM TABLE_NAME
GROUP BY END_DATE ORDER BY END_DATE;