Selecting Rows with based on Count (SQLite) - 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).

Related

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

How do I pull an extra row when pulling data from between dates?

I'd like to pull another row with the nearest date before the beginning of the following query from a sqlite db:
select * from inv WHERE TIME BETWEEN date1 AND date2
Is there a simple addition to the query or do I need to pull in more data and do the transformation at the end?
Thank you
Use UNION for the row with the maximum TIME that is less than date1:
select * from inv WHERE TIME BETWEEN date1 AND date2
UNION
select * from inv WHERE TIME = (select MAX(TIME) from inv WHERE TIME < date1)
If there would be more than 1 additional rows and you want exactly 1, you can add to the above code:
LIMIT 1

Retain value till a certain date in 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

Update only the year in SQLite column

I have a SQLite3 database that has 366 rows in it, and a date for each day of the year. I need to make sure that the year is current, and if it is not, then I need to update the year to the current calendar year. What I would like to do is something like the following:
UPDATE daily SET date = DATE('%Y', 'now');
or
UPDATE daily SET date = strftime('%Y', 'now');
But those queries just make the date column NULL, and even if they did work as I expected them to, I doubt that it would retain the day and month that is there already.
In MySQL, I can do something like UPDATE daily SET date = ADDDATE(date, INTERVAL 1 YEAR) -- but firstly, it is not a valid SQLite query, and secondly, I need to update to the current year, not just step up one year.
Any help would be appreciated.
Try this:
create table t (id int, d text);
insert into t
select 1, date('2011-01-01') union
select 2, date('2012-03-11') union
select 3, date('2013-05-21') union
select 4, date('2014-07-01') union
select 5, date('2015-11-11');
select * from t;
update t set
d = date(strftime('%Y', date('now')) || strftime('-%m-%d', d));
select * from t;
It uses Date And Time Functions. Firstly it takes month and day from field (strftime('-%m-%d', d)) then add (concatenate) current year (strftime('%Y', date('now'))) and converts it to date.
SQL Fiddle live example.

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