Populate Min/Max based on Issues and lead time - sql

I am looking to write an sql update query to populate the min/max fields based on the issues and the date issued. I am looking at 120 days delivery as my delivery date.
PN
QTY_ISSUED
ISSUED_DATE
12345
2
20-01-01
12345
1
20-02-01
12345
2
20-03-01
12345
5
20-04-01
12345
1
20-04-20
12345
3
20-06-01
I would like to return and update to 11 and not return the 3 in June.

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

How to find number of days between each status change

I have the following table named Application in Postgres:
ID JA_ID TO_STATUS FROM_STATUS DATE
1 100 Matched NULL 2019-05-06
2 100 INterview Matched 2019-05-30
3 100 Extended INterview 2019-05-31
4 200 New_Applicant NULL 2020-04-01
5 200 INterview New_applicant 2020-04-05
6 200 Rejected interview 2020-05-10
Now I need to calculate # of days between each status change for every JA_ID from this table.I have sorted the status change based JA_ID and DAte(in asc). My O/P 'DAYS' column should be like this:
ID JA_ID TO_STATUS FROM_STATUS DATE DAYS
1 100 Matched NULL 2019-05-06 NULL
2 100 Interview Matched 2019-05-30 24
3 100 Extended INterview 2019-05-31. 1
4 200 New_Applicant NULL 2020-04-01. NULL
5 200 Interview New_applicant 2020-04-05. 4
6 200 Rejected interview 2020-05-10. 5
Assuming "date" is defined with the datatype date (as it should be), you can use lag() to get the previous date and subtract the values:
select id, ja_id, to_status, from_status, "date",
"date" - lag("date") over (partition by ja_id order by "date") as days
from application;

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

Max date among records and across tables - SQL Server

I tried max to provide in table format but it seem not good in StackOver, so attaching snapshot of the 2 tables. Apologize about the formatting.
SQL Server 2012
**MS Table**
**mId tdId name dueDate**
1 1 **forecastedDate** 1/1/2015
2 1 **hypercareDate** 11/30/2016
3 1 LOE 1 7/4/2016
4 1 LOE 2 7/4/2016
5 1 demo for yy test 10/15/2016
6 1 Implementation – testing 7/4/2016
7 1 Phased Rollout – final 7/4/2016
8 2 forecastedDate 1/7/2016
9 2 hypercareDate 11/12/2016
10 2 domain - Forte NULL
11 2 Fortis completion 1/1/2016
12 2 Certification NULL
13 2 Implementation 7/4/2016
-----------------------------------------------
**MSRevised**
**mId revisedDate**
1 1/5/2015
1 1/8/2015
3 3/25/2017
2 2/1/2016
2 12/30/2016
3 4/28/2016
4 4/28/2016
5 10/1/2016
6 7/28/2016
7 7/28/2016
8 4/28/2016
9 8/4/2016
9 5/28/2016
11 10/4/2016
11 10/5/2016
13 11/1/2016
----------------------------------------
The required output is
1. Will be passing the 'tId' number, for instance 1, lets call it tid (1)
2. Want to compare tId (1)'s all milestones (except hypercareDate) with tid(1)'s forecastedDate milestone
3. return if any of the milestone date (other than hypercareDate) is greater than the forecastedDate
The above 3 steps are simple, but I have to first compare the milestones date with its corresponding revised dates, if any, from the revised table, and pick the max date among all that needs to be compared with the forecastedDate
I managed to solve this. Posting the answer, hope it helps aomebody.
//Insert the result into temp table
INSERT INTO #mstab
SELECT [mId]
, [tId]
, [msDate]
FROM [dbo].[MS]
WHERE ([msName] NOT LIKE 'forecastedDate' AND [msName] NOT LIKE 'hypercareDate'))
// this scalar function will get max date between forecasted duedate and forecasted revised date
SELECT #maxForecastedDate = [dbo].[fnGetMaxDate] ( 'forecastedDate');
// this will get the max date from temp table and compare it with forecasatedDate/
SET #maxmilestoneDate = (SELECT MAX(maxDate)
FROM ( SELECT ms.msDueDate AS dueDate
, mr.msRevisedDate AS revDate
FROM #mstab as ms
LEFT JOIN [MSRev] as mr on ms.msId = mr.msId
) maxDate
UNPIVOT (maxDate FOR DateCols IN (dueDate, revDate))up );

Qlik Set Analysis and aggregating across date ranges

I am trying to use Qlik set analysis and range sum to compute the total number of records that were active at a some point in the month
For example, if these are the records:
id state created_date modified_date
1 expired 01/12/2014 10/12/2014
2 expired 01/12/2014 10/03/2015
3 active 01/12/2014 01/12/2014
4 expired 10/01/2015 12/01/2015
5 expired 10/01/2015 11/03/2015
6 active 10/02/2015 10/02/2015
7 expired 10/03/2015 11/03/2015
The expected o/p is
Dec-14 3
Jan-15 4
Feb-15 4
Mar-15 5
E.g: For Jan-2015 the result should be 4 -
1 active record from Dec 2014 + 2 created in Jan + 1 from Dec which actually expired in March 2015
modified_date is updated when a record is expired
So I tried this:
rangesum(above(Count({$<[state] = {'active'} >} id), 0,12))
+ Count({1<[state] ={'expired'}, modified_date.Calendar.Month ={">=$(created_date.Calendar.Month)"}>} id)
the second count statement is wrong. How can I aggregate the records which are currently not active but were active at some point during the month.Is it possible to achieve this without using a Master calendar ?
Thanks in advance!
A better approach to this would be to amend your data model to include an active date like below.
ActiveMonthsRaw:
Load ID
,date(monthstart(created_date),''MMM-YY) as ActiveMonthFormat
resident SourceTable
where state ='active';
ActiveMonthsRaw:
Load ID
,date(monthstart(modified_date),''MMM-YY) as ActiveMonthFormat
resident SourceTable
where state ='expired';
ActiveMonths:
Load distinct ID
,ActiveMonthFormat as ActiveMonth;
drop table ActiveMonthsRaw;
It would then be a case of doing a distinct count of ID by the Active Month. No Set analysis in sight!