SQLite query select between 2 records on same condition - sqlite

i am trying to count (in minutes) between two records on 1 table and WHERE clause is the same condition.
_id | venue_id | act_time | status |
1 | 1 | 13:30 | 0 |
2 | 1 | 15:40 | 1 |
3 | 2 | 13:03 | 0 |
4 | 2 | 16:06 | 1 |
when i exec query like this :
SELECT _id, venue_id, status, (julianday(act_time IN (SELECT act_time FROM reports WHERE venue_id='1' AND status='1')) - julianday(act_time))*1440 AS duration FROM reports WHERE venue_id='1' AND status='0'
but, the result show the wrong calculation
Please help me what is the correct query for this problem?
so, if i count the duration between 15:40 - 13:30 (at venue_id='1') = 130 minutes.
thank you.

The IN operator checks whether the value on the left side is contained in the set of values on the right side, and returns a boolean result (0 or 1).
You just want to use the act_time value directly; drop act_time IN.

Related

Hierarchical query in Teradata

I have hierarchical data as follows.
|Serial No | Primary Flag |Prev SerialNo|
| 101 | 1 | 56 |
| 56 | 0 | NULL |
| 505 | 0 | NULL |
| 223 | 1 | 156 |
| 156 | 0 | 93 |
| 93 | 0 | 42 |
42 | 0 | NULL |
First two rows are related by Previous serial number when primary flag, so total counts in their hierarchy is 2
Third row is not related to any thing since Previous serial number is NULL., so total count is 0.
Fourth row is related to below 3 records, so total count is 4.
I need a query to find the total related counts for rows when Primary flag is 1.How can I achieve this in Teradata?
Assuming you don't want to return a row for Serial_No 505, you can use a recursive query and aggregation:
WITH RECURSIVE r AS (
SELECT h.Serial_No AS Primary_SerialNo, h.Serial_No, h.Prev_SerialNo, 1 (INTEGER) as Level
FROM hierTbl h WHERE h.Primary_Flag=1
UNION ALL
SELECT r.Primary_SerialNo, h.Serial_No, h.Prev_SerialNo, r.Level+1
FROM hierTbl h JOIN r ON r.Prev_SerialNo = h.Serial_No
)
Select Primary_SerialNo, MAX(Level) as Related
FROM r GROUP BY 1;

How to use MERGE keyword in pl/sql?

I am updating a table, but I keep getting follwing error
ERROR: syntax error at or near "MERGE"
LINE 3: MERGE into
when i try to use a merge statement. I don't see anything obvious wrong with the syntax. can someone point out the obivous
MERGE into Table2 t2
using (select name, max(id) max_id from Table1 t1 group by name ) t1
on (t2.project_name=t1.name)
when matched then update set projectid=max_id where status='ongoing' ;
Table1
1 | alpha | 2021 |
2 | groundwork | 2020 |
3 | NETOS | 2021 |
5 | WebOPD | 2019 |
Table2
id | name | year | status | project name | projectID
1 | john | 2021 | ongoing | alpha | 1
2 | linda | 2021 | completed | NETOS | 3
3 | pat | 2021 | WebOPD | completed | 5
4 | tom | 2021 | ongoing | alpha | 1
version : PostgreSQL 13.6
The last line in your message says you use PostgreSQL. Tag you used (plsql) means Oracle. Which one is it, after all? I presume former, but - syntax you used is Oracle.
MERGE documentation for PostgreSQL says that
INTO can't be used
no parenthesis for ON clause
WHERE clause can't be used
See if something like this helps:
MERGE Table2 t2
using (select t1.name,
max(t1.id) max_id
from Table1 t1 join table2 t2 on t2.project_name = t1.name
where t2.status = 'ongoing'
group by name
) x
on t2.project_name = x.name
when matched then update set
t2.projectid = x.max_id ;

Kusto query to calculate number of users who experienced X number of crashes in Y days since release

My kusto data table records an event any time someone using the product experiences a crash. I want to calculate how many users experienced at least "X" number of crashes in "Y" number of days since the product was released.
So if the product was released on January 1st, and the table of crash events looks like:
| Date | User Id | Days Since Release |
| 1/1 | A | 0 |
| 1/1 | A | 0 |
| 1/1 | B | 0 |
| 1/2 | A | 1 |
| 1/3 | B | 2 |
| 1/4 | C | 3 |
Then the results would be:
| Days Since Release | Number of Crashes | Number of users |
| 0 | 1 | 2 | // Users A and B experienced 1 crash on 0th day
| 0 | 2 | 1 | // User A experienced 2 crashes on 0th day
| 1 | 1 | 2 | // Users A and B experienced at least 1 crash on 1st day
| 1 | 2 | 1 | // User A experienced at least 2 crashes on 1st day
| 1 | 3 | 1 | // User A experienced at least 3 crashes on 1st day
| 2 | 1 | 2 | // Users A and B experienced at least 1 crash on 2nd day
| 2 | 2 | 2 | // Users A and B experienced at least 2 crashes on 2nd day
| 2 | 3 | 1 | // User A experienced at least 3 crashes on 2nd day
| 3 | 1 | 3 | // Users A, B and C experienced at least 1 crash on 3rd day
| 3 | 2 | 2 | // Users A and B experienced at least 2 crashes on 3rd day
| 3 | 3 | 1 | // User A experienced at least 3 crashes on 3rd day
I tried to do this using the activity_engagement function but have not been able to perform the aggregations needed on number of crashes "X" and days since release "Y".
Please see the query below, I believe this is what you're looking for.
The idea is to expand the counts for each user from the day of the crash and until the total DaysSinceRelease (since if a crash happened on day #1, it should also be considered in all days following #1). Then, we also expand range(1, NumCrashes, 1) since if a user had 3 crashes, these should also be counted in the at-least-1 and at-least-2 bins.
let totalDaysSinceRelease = 3;
datatable(Date:datetime, UserId:string, DaysSinceRelease:long)
[
datetime(2020-01-01), "A", 0,
datetime(2020-01-01), "A", 0,
datetime(2020-01-01), "B", 0,
datetime(2020-01-02), "A", 1,
datetime(2020-01-03), "B", 2,
datetime(2020-01-04), "C", 3
]
| summarize NumCrashes = count() by UserId, DaysSinceRelease
| order by UserId asc, DaysSinceRelease asc
| extend NumCrashes = row_cumsum(NumCrashes, UserId != prev(UserId))
| extend DaysSinceRelease = range(DaysSinceRelease, totalDaysSinceRelease, 1)
| mv-expand DaysSinceRelease to typeof(long)
| summarize NumCrashes=max(NumCrashes) by UserId, DaysSinceRelease
| extend NumCrashes = range(1, NumCrashes, 1) | mv-expand NumCrashes to typeof(long)
| summarize dcount(UserId) by DaysSinceRelease, NumCrashes
| order by DaysSinceRelease asc, NumCrashes asc

sqlite difference between rows

In SQLite I have a collection of records and I want to only show the records with specific differences.
The table has something like the following values:
file | idx | values
------|-------|----------------------
1 | 101 | 1,3,7,11,23,11
2 | 101 | 1,3,7,11,23,11
3 | 101 | 0,4,8,60,20,11
1 | 211 | 12,11,23
2 | 211 | 12,0,23
3 | 211 | 12,0,23
1 | 300 | 1
2 | 300 | 0
3 | 300 | 0
I want to be able to select two different fileIDs, and compare them.
I mean, I want to examine only records with (file = 1 AND file = 2)
What I cant to get back as a result is a collection of records that are not the same:
file | idx | values
------|-------|----------------------
1 | 211 | 12,11,23
2 | 211 | 12,0,23
1 | 300 | 1
2 | 300 | 0
So you do not want rows for which another row with the same idx and values values exists:
SELECT *
FROM MyTable
WHERE file IN (1, 2)
AND NOT EXISTS (SELECT *
FROM MyTable AS T2
WHERE file IN (1, 2)
AND file <> MyTable.file
AND idx = MyTable.idx
AND values = MyTable.values);
I just recieved an answer in another forum. This seems to work:
select * from thetable a, thetable b
where a.file <> b.file and a.idx = b.idx and a.values <> b.values and
a.file in (1, 2) and b.file in (1, 2);
Of course I change certain values as variables in a prepared statement. But it did the trick

Query Calculation at lower granularity

So I have a report that shows. Product, account type, account tier, balance, # of deposits.
My problem is that I need the tier to be applied to the individual accounts based off of their balance. So if your account is less than 99 then 'TIER 1', 100-199 then 'TIER 2' and > 200 then 'TIER 3'. The issue with this is the tier needs to be summarized. So the logic needs to be at account granularity but then just show the different combinations of product, account type and account tier. The balance also needs to be the total of everyone within that group.
example:
PRODUCT | TYPE | TIER | BALANCE | # OF DEPOSITS
A | 1 | T1 | $95,000 | 4
A | 1 | T2 | $80,000 | 10
A | 1 | T3 |$100,000 | 2
A | 2 | T1 | $50,000 | 45
A | 2 | T2 | $70,000 | 13
A | 2 | T3 |$250,000 | 100
B | 1 | T1 | $65,000 | 45
B | 1 | T2 | $15,000 | 25
etc...
I hope this at least shows what I am trying to accomplish with this report.
Please feel free to ask questions for more clarification.
Thank you in advance-
EDIT: This is the output I am currently getting.
PRODUCT | TYPE | TIER | BALANCE | # OF DEPOSITS
A | 1 | T1 | $1MIL | 100
A | 2 | T1 | $1.5MIL | 520
A | 3 | T6 | $2MIL | 650
I assuming the column 'Tier' is generated by you and not from the data source.
[Tier]
CAST
WHEN [Balance] < 99 THEN 'Tier1'
WHEN [Balance] between 100 and 199 THEN 'Tier2'
WHEN [Balance] > 200 THEN 'Tier3'
END
[Total Balance]
total([Balance] for [Product],[Type],[Tier])
Set the [Total Balance] Aggregate Function property to 'Calculated'.
If I understand your question correctly, a crosstab to aggregate the three criteria might be the best solution.
or
A clunky but effective way might be to create a two queries (I assume you are using report studio). The first to assign each account a tier, and in the next query you can add the total aggregate.

Resources