KQL regex row extraction - kql

I have a column called AdditionalExtensions that contains the following start=Aug 13 2022 20:42:17;logver700060366.ad
I want to add a regex search to this to only extract and display the date and time as following: Aug 13 2022 20:42:17
I dont care about the rest of the information in that column.

Using the parse operator (option 1) would probebly be the cleanest way to go here.
I added some more options for educational purposes.
print txt = "start=Aug 13 2022 20:42:17;logver700060366.ad"
| parse txt with * "=" dt1 ";" * // option 1
| extend dt2 = extract("=(.*?);", 1, txt) // option 2
| extend dt3 = split(split(txt, ";")[0], "=")[1] // option 3
| extend dt4 = trim("^.*?=|;.*$", txt) // option 4
| extend dt5 = substring(txt, indexof(txt, "=") + 1, indexof(txt, ";") - indexof(txt, "=") - 1) // option 5
txt
dt1
dt2
dt3
dt4
dt5
start=Aug 13 2022 20:42:17;logver700060366.ad
Aug 13 2022 20:42:17
Aug 13 2022 20:42:17
Aug 13 2022 20:42:17
Aug 13 2022 20:42:17
Aug 13 2022 20:42:17
Fiddle

Related

Select row with most recent date per location and increment recent date by 1 for each row by location using MariaDB

I have a table of location which has 'Date column'. I have to find recent date by each group of locationID for e.g. locationID 1 has most recent date '31 May 2022'. After finding recent date from the group of locationID I have to add 14 days in that recent date and store it in NewDate column. and add + 1 in that new date for other row for that group of locationID.
My table is:
id locationID Date NewDate
1 1 31 May 2022
2 1 16 May 2022
3 1 28 Apr 2021
4 2 29 Mar 2022
5 2 22 Feb 2022
6 3 14 Jun 2022
7 3 27 Oct 2021
8 4 01 Feb 2022
9 4 04 May 2022
10 4 14 Jun 2021
11 5 01 Jun 2022
12 5 29 May 2022
13 5 20 Sep 2022
14 5 11 Aug 2022
15 5 03 Aug 2022
Answer should be as below:
For e.g. for locationID = 1
id locationID Date NewDate
1 1 31 May 2022 14 Jun 2022 // Recent Date + 14 Days - 31 May + 14 Days
2 1 16 May 2022 15 Jun 2022 // Recent Date + 15 Days - 31 May + 15 Days
3 1 28 Apr 2021 16 Jun 2022 // Recent Date + 16 Days - 31 May + 16 Days
I have come across few similar post and found recent date like this:
SELECT L.*
FROM Locations L
INNER JOIN
(SELECT locationID, MAX(Date) AS MAXdate
FROM Locations
GROUP BY locationID) groupedL
ON L.locationID = groupedL.locationID
AND L.Date = groupedL.MAXdate
using above code I am able to find recent date per location but how do I add and increment required days and store it to NewDate column ? I am new to MariaDB, please suggest similar post link, any reference documents or blogs. Should I make some function to perform this logic and call the function to store required dates in NewDate column? I am not sure please suggest. Thank you.
RESULT SHOULD LOOK LIKE BELOW:
id locationID Date NewDate
1 1 31 May 2022 14 Jun 2022 // Recent Date for locationid 1 + 14 Days - 31 May + 14 Days
2 1 16 May 2022 15 Jun 2022 // Recent Date for locationid 1 + 15 Days - 31 May + 15 Days
3 1 28 Apr 2021 16 Jun 2022 // Recent Date for locationid 1 + 16 Days - 31 May + 16 Days
4 2 29 Mar 2022 12 APR 2022 // Recent Date for locationid 2 + 14 Days
5 2 22 Feb 2022 13 APR 2022 // Recent Date for locationid 2 + 15 Days
6 3 14 Jun 2022 28 JUN 2022 // Recent Date for locationid 3 + 14 Days
7 3 27 Oct 2021 29 JUN 2022 // Recent Date for locationid 3 + 15 Days
8 4 01 Feb 2022 18 MAY 2022 // Recent Date for locationid 4 + 14 Days
9 4 04 May 2022 19 MAY 2022 // Recent Date for locationid 4 + 15 Days
10 4 14 Jun 2021 20 MAY 2022 // Recent Date for locationid 4 + 16 Days
11 5 01 Jun 2022 04 OCT 2022 // Recent Date for locationid 5 + 14 Days
12 5 29 May 2022 05 OCT 2022 // Recent Date for locationid 5 + 15 Days
13 5 20 Sep 2022 06 OCT 2022 // Recent Date for locationid 5 + 16 Days
14 5 11 Aug 2022 07 OCT 2022 // Recent Date for locationid 5 + 17 Days
15 5 03 Aug 2022 08 OCT 2022 // Recent Date for locationid 5 + 18 Days
You can use a cte:
with cte as (
select l1.*, l2.m, (select sum(l4.id < l1.id and l4.locationid = l1.locationid) from locations l4) inc from locations l1
join (select l3.locationid, max(l3.dt) m from locations l3 group by l3.locationid) l2 on l1.locationid = l2.locationid
)
select c.id, c.locationid, c.dt, c.m + interval 14 + c.inc day from cte c
You could use analytic window functions and update the original table by joining to a sub-query (works for MariaDB):
update t
join (
select Id,
Date_Add(First_Value(date) over(partition by locationId order by date desc),
interval (13 + row_number() over(partition by locationId order by date desc)) day
) NewDate
from t
)nd on t.id = nd.id
set t.Newdate = nd.NewDate;
See DB<>Fiddle example

How to join two columns from one table to a different table based matching criteria in SAS

I am trying to join the columns "Type2" and "Measurement2" from table "Update" to the table "Have". I want the columns to align where column "Subject1" in table "Have" matches column "Subject2" in table "update", and column "Procedure1" in table "Have" matches column "Procedure2" in table "Update".Thank you in advance.
data Have;
input Subject1 Type1 :$12. Date1 &:anydtdte. Procedure1 :$12. Measurement1;
format date yymmdd10.;
datalines;
500 Initial 15 AUG 2017 Invasive 20
500 Initial 15 AUG 2017 Surface 35
428 Initial 3 JUL 2017 Outer 10
765 Initial 20 JUL 2019 Other 19
610 Initial 17 Mar 2018 Invasive 17
;
data Update;
input Subject2 Type2 :$12. Date2 &:anydtdte. Procedure2 :$12. Measurement2;
format date yymmdd10.;
datalines;
500 Followup 15 AUG 2018 Invasive 54
428 Followup 15 AUG 2018 Outer 29
765 Seventh 3 AUG 2018 Other 13
500 Followup 3 JUL 2018 Surface 98
610 Third 20 AUG 2019 Invasive 66
;
Are you just looking for a join between two tables ??
Select distinct have.*, update.type2, update.measurement2
from have
left join update
on
have.subject1 = update.subject2
and have.procedure1 = update.procedure2
Combining two data sets based on a key (your subject and procedure) is performed using a MERGE according to the group variables named in a BY statement. Both data sets need the same BY variables.
Example code:
MERGE requires sorted data, so that will have to occur first.
Data set option rename= is used to create common names for the BY statement.
proc sort data=Have; by Subject1 Procedure1;
proc sort data=Updates; by Subject2 Procedure2;
data combined;
* trick: force these variables to be first two columns in output data set;
retain subject procedure;
merge
have (rename=(subject1=subject procedure1=procedure))
updates (rename=(subject2=subject procedure2=procedure))
;
by subject procedure;
run;
Example data:
data Have;
attrib
Subject1 length=8
Type1 length=$12
Date1 informat=anydtdte. format=yymmdd10.
Procedure1 length=$12
Measurement1 length=8
;
input
Subject1& Type1& Date1& Procedure1& Measurement1&; datalines;
500 Initial 15 AUG 2017 Invasive 20
500 Initial 15 AUG 2017 Surface 35
428 Initial 3 JUL 2017 Outer 10
765 Initial 20 JUL 2019 Other 19
610 Initial 17 Mar 2018 Invasive 17
;
data Updates;
attrib
Subject2 length=8
Type2 length=$12
Date2 informat=anydtdte. format=yymmdd10.
Procedure2 length=$12
Measurement2 length=8
;
input
Subject2& Type2& Date2& Procedure2& Measurement2&; datalines;
500 Followup 15 AUG 2018 Invasive 54
428 Followup 15 AUG 2018 Outer 29
765 Seventh 3 AUG 2018 Other 13
500 Followup 3 JUL 2018 Surface 98
610 Third 20 AUG 2019 Invasive 66
;

Adding set lists of future dates to rows in a SQL query

So I am doing a cohort analysis for customers, where a cohort is a group of people who started using the product in the same month. I then keep track of each cohort's total use for every subsequent month up till present time.
For example, the first "cohort month" is January 2012, then I have "use months" January 12, Feb 12, March 12, ..., March 17(current month). One column is "cohort month", and another is "use month". This process repeats for every subsequent cohort month. The table looks like:
Jan 12 | Jan 12
Jan 12 | Feb 12
...
Jan 12 | Mar 17
Feb 12 | Feb 12
Feb 12 | Mar 12
...
Feb 12 | Mar 17
...
Feb 17 | Feb 17
Feb 17 | Mar 17
Mar 17 | Mar 17
The problem arises because I want to do forecasting for one year out for both existing and future cohorts.
That means for the Jan 12 cohort, I want to do prediction for April 17 to Mar 18.
I also want to do predictions for the April 17 cohort (which doesn't exist yet) from April 17 to Mar 18. And so on till predictions for the Mar 18 cohort in Mar 18.
I can handle the predictions, don't worry about that.
My issue is that I cannot figure out how to add in this list of (April 17 .. Mar 17) in the "use month" column before every cohort switches.
I also need to add in cohorts April 17 to Mar 18, and have the applicable parts of this list of (April 17 ... Mar 17) for each of these future cohorts.
So I want the table to look like:
Jan 12 | Jan 12
Jan 12 | Feb 12
...
Jan 12 | Mar 17
Jan 12 | Apr 17
..
Jan 12 | Mar 18
Feb 12 | Feb 12
Feb 12 | Mar 12
...
Feb 12 | Mar 17
Feb 12 | Apr 17
...
Feb 12 | Mar 18
...
...
Feb 17 | Feb 17
Feb 17 | Mar 17
...
Feb 17 | Mar 18
Mar 17 | Mar 17
...
Mar 17 | Mar 18
I know the first solution to come to mind is to do a create a list of all dates Jan 12 to Mar 18, cross join it to itself, and then left outer join to the current table I have (where cohort / use months range from Jan 12 to Mar 17). However, this is not scalable.
Is there a way I can just iteratively add in this list of the months of the next year?
I am using HP Vertica, could use Presto or Hive if absolutely necessary
I think you should use the query here below to create a temporary table out of nothing, and join it with the rest of your query. You can't do anything in a procedural manner in SQL, I'm afraid. You won't be able to get away without a CROSS JOIN. But here, you limit the CROSS JOIN to the generation of the first-of-month pairs that you need.
Here goes:
WITH
-- create a list of integers from 0 to 100 using the TIMESERIES clause
i(i) AS (
SELECT dt::DATE - '2000-01-01'::DATE
FROM (
SELECT '2000-01-01'::DATE + 0
UNION ALL SELECT '2000-01-01'::DATE + 100
) d(d)
TIMESERIES dt AS '1 day' OVER(ORDER BY d::TIMESTAMP)
)
,
-- limits are Jan-2012 to the first of the current month plus one year
month_limits(month_limit) AS (
SELECT '2012-01-01'::DATE
UNION ALL SELECT ADD_MONTHS(TRUNC(CURRENT_DATE,'MONTH'),12)
)
-- create the list of possible months as a CROSS JOIN of the i table
-- containing the integers and the month_limits table, using ADD_MONTHS()
-- and the smallest and greatest month of the month limits
,month_list AS (
SELECT
ADD_MONTHS(MIN(month_limit),i) AS month_first
FROM month_limits CROSS JOIN i
GROUP BY i
HAVING ADD_MONTHS(MIN(month_limit),i) <= (
SELECT MAX(month_limit) FROM month_limits
)
)
-- finally, CROSS JOIN the obtained month list with itself with the
-- filters needed.
SELECT
cohort.month_first AS cohort_month
, use.month_first AS use_month
FROM month_list AS cohort
CROSS JOIN month_list AS use
WHERE use.month_first >= cohort.month_first
ORDER BY 1,2
;

Converting Row Into a Column based on Month

I have following input:
JAN_OLD FEB_OLD MAR_OLD APR_OLD MAY_OLD JUNE_OLD JAN_NEW FEB_NEW MAR_NEW APR_NEW MAY_NEW JUNE_NEW
10 11 12 13 14 15 20 21 22 23 24 25
Disired result set is as below:
JAN New OLD
FEB 20 10
MAR 21 11
APR 22 12
MAY 23 13
JUN 24 14
Can someone suggest how to achieve this?
Multiple Union All or single Cross Apply
SELECT months,old,new
FROM Your_table
CROSS apply (VALUES(jan_old,jan_new,'Jan'),
(FEB_OLD,FEB_new,'Feb'),
(MAR_OLD,MAR_new,'Mar'),
(APR_OLD,APR_new,'Apr'),
(MAY_OLD,MAY_new,'may'),
(JUNE_OLD,JUNE_new,'Jun'))
cs (old, new, months)
If you are not sure about the no. of columns then you may have to use Dynamic sql

Select a string from a specific place - T-SQl [duplicate]

This question already has answers here:
T-SQL Find char string and take all char to right of expression
(3 answers)
Closed 8 years ago.
Let's say I have following records in my table.
4 - Mar 1 thru Apr 11
13 - Dec 01 thru Dec 31
I want to select the string so that the output would be,
Mar 1 thru Apr 11
Dec 01 thru Dec 31
How can I do that?
Thank you
I could use:
CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )
See for documentation: http://msdn.microsoft.com/en-us/library/ms186323.aspx
So, this will be:
SELECT SUBSTRING([*FieldName*], CHARINDEX('-', [*FieldName*])+2, 252) FROM [*Table*]
252 is the number of characters, and that's the default length of nvarchar fields. Increase it or decrease this number if need be.
I put +2 to strip out the - (dash and white space).
You have a lot of choices, I will use SELECT with SUBSTR and LEN clause, (see this page: http://oreilly.com/catalog/sqlnut/chapter/ch04.html)
Data:
4 - Mar 1 thru Apr 11
13 - Dec 01 thru Dec 31
SELECT SUBSTR(your-field,4,TRIM(LEN(your-field)-4)) FROM your-table;
OUTPUT:
Mar 1 thru Apr 11
Dec 01 thru Dec 31
NOTES
- SUBSTR: return part of argument
- LEN: return lenght of argument
- TRIM: return lenght without spaces
SELECT REPLACE((SUBSTRING('4 - Mar 1 thru Apr 11', CHARINDEX(' - ', '4 - Mar 1 thru Apr 11'), LEN('4 - Mar 1 thru Apr 11'))),' - ','')
This also works,
I will check out your option now.
Thank you for all your help guys.