Retain value till a certain date in teradata - teradata

Below is the link of my previous quetsion. It worked as suggested by one of the community members #Doneth.
But when I use '2018-05-31' in coalesce , i'm getting an error saying 'datatype mismatch in if/then else statement'.
Query used:
with cte
{
SELECT customer_id, bal, st_ts,
-- return the next row's date
Coalesce(Min(st_ts)
Over (PARTITION BY customer_id
ORDER BY st_ts
ROWS BETWEEN 1 Following AND 1 Following)
,'2018-05-31') AS next_Txn_dt
FROM BAL_DET;
}
SELECT customer_id, bal
,Last(pd) -- last day of the period
FROM cTE
-- make a period of the current and next row's date
-- and return one row per day
EXPAND ON PERIOD(ST_TS, next_Txn_dt) AS pd;
Link to my question:
Retain values till there is a change in value in Teradata

Related

sqlite3 - Filtering out the very first and last record that have same identifier

id,date,source,target,identifier
1,2020-10-10,internal,external,abc-123
2,2020-10-10,internal,internal,xyz-123
3,2020-10-11,external,external,abc-123
4,2020-10-12,external,external,abc-123
There are three entries for the same record (abc-123) and I would like to filter out the oldest and the newest record. For all the records, if there are duplicates then I would like to get the oldest and newest record.
I have no idea how to construct such a query. Any help will be greatly appreciated.
You could use analytic functions here:
WITH cte AS (
SELECT *, MIN(date) OVER (PARTITION BY identifier) min_date,
MAX(date) OVER (PARTITION BY identifier) max_date
FROM yourTable
)
SELECT id, date, source, target, identifier
FROM cte
WHERE date IN (min_date, max_date);
The CTE above adds to your table two new columns for the min and max date per each identifier. The outer query then restricts to only records having those min or max dates.

Teradata - Get the 2nd most current date, from a table, and reference it

Lets say we have a table with entries and run dates. It might get updated on weekends or holidays, or it might just run M-F. And this check could run before all loads are done for the day. For this reason, I want to find the entry before the max date.
Run_Date Entry
2020-03-09 z
2020-03-06 x
2020-03-05 y
In this instance, I want to return 3/6/20. I would use this in a CTE or subquery.
This code returns the top two dates, and we see the 2nd date 3/6, but how do I single it out?
SELECT TOP 2
RUN_DATE
FROM DATABSE1.TABLEA
GROUP BY RUN_DATE
ORDER BY RUN_DATE DESC
SELECT
RUN_DATE
FROM DATABSE1.TABLEA
QALIFY
ROW_NUMBER()
OVER (ORDER BY RUN_DATE DESC) = 2 -- 2nd highest date
This assumes RUN_DATE is unique, otherwise switch to DENSE_RANK plus DISTINCT

Selecting Rows with based on Count (SQLite)

Hoping for some help with this:
1) I am trying to select all calls (rows) for CustomerIDs that show up 6 or more times within a 30 day rolling period, so if the CustomerID is within the file 6 or more times within 30 days, then it would provide me with all records for that CustomerID.
2) I also need to select all calls for CustomerIDs that show up 2 or more times within a 30 day rolling period but ONLY if two certain columns also match (CallType1 and CallType2). Very similar to the query with the 6 calls but we need to consider that the call types are exactly the same too.
SELECT * FROM tablename
WHERE CustomerID IN (SELECT CustomerID FROM tablename
WHERE "CustomerID"
IN ('MyProgram'));
The query above selects all of the CustomerIDs which reach my program. I need to add the logic to count >=6 CustomerIDs (item 1 above) and then a second query to get the >=2 with the same CallTypes.
The innermost subquery computes how many calls there are in the window beginning at First.
The middle subquery checks this value for every possible window in the table. (This is inefficient, but SQLite has no window functions.)
SELECT *
FROM TableName
WHERE CustomerID IN (SELECT CustomerID
FROM TableName AS First
WHERE (SELECT COUNT(*)
FROM TableName
WHERE Date BETWEEN First.Date
AND date(First.Date, '+30 days')
AND CustomerID = First.CustomerID
) >= 6)
This assumes that there is a column Date using the default date format (yyyy-mm-dd).

How to substitute part of date string with table.field value in sqlite3?

I have the following 2 tables:
CREATE TABLE count (nbr int not null);
and
CREATE TABLE day (day int not null);
Table count has 3 records with field values:
1
2
3
Now I want to insert a calculated date in table day based on the current date and a value from table count with the following statement:
insert into day values (date('now', '+'(select nbr from count where nbr=1) 'day'));
No matter what I change and (re)try in the statement I keep getting 'Syntax error' messages or the message that day.day may not be NULL.
Is it possible to use the select statement in this case anyway (with correct syntax of course) and if so, what am I doing wrong?
Found the solution, it was only a matter of correct concatenation.
This works:
insert into day select (date('now', '+'||(select nbr from count where nbr=1)||' day'));

how to get a average of days with two different tables

There are two tables 1)HR_OrderRequest (column to be considered is HRdate) other columns are HRUID,UID
2)HR_Supplydetails(colmn to be considered is HRUID) other columns are createddttm,UID
by considering the date from HR_Supplydetails we should find the average days taken for that particular UID time taken to release of HRdate .I have a problem getting a average of days
Please do the need .
You can do a datediff:
SELECT AVG(DATEDIFF(day, createddttm,HRdate))
And then do your join on the two tables.
so the steps would be first getting the number of days for a order so you have like these tables:
order (id, date) # this date is the date the order was placed
supply (id, date) # this date is the date the order was filled
these might not be right but i think they are close
select avg(days)
from (select (supply.date-order.date) as days
from order join supply on order.id=supply.id)
but the sub query isn't needed as it can be written as:
select avg(supply.date-order.date)
from order join supply on order.id=supply.id;
now to map this to your tables:
HR_OrderRequest (HRUID, UID, HRdate)
HR_Supplydetails (HRUID, UID, createddttm)
select avg(HR_OrderRequest.HRdate-HR_Supplydetails.createddttm)
from HR_OrderRequest join HR_Supplydetails on HR_OrderRequest.HRUID=HR_Supplydetails.HRUID
where UID=?
clearly if you have date functions use them

Resources