Count of Occurrences from the transaction DateTime + 1 Hour in SQL - sql

I have the Dataset Like
Transaction Time A B C Expected Output
3/17/2020 14:42 India 1 10 2
3/17/2020 15:09 India 1 10 0
3/17/2020 15:48 India 1 10 4
3/17/2020 15:59 India 1 10 0
3/17/2020 16:13 India 1 10 0
3/17/2020 16:36 India 1 10 0
3/17/2020 17:02 India 1 10 1
3/17/2020 18:42 India 1 10 5
3/17/2020 18:55 India 1 10 0
3/17/2020 19:05 India 1 10 0
3/17/2020 19:24 India 1 10 0
3/17/2020 19:30 India 1 10 0
3/17/2020 20:01 India 1 10 1
3/17/2020 21:24 India 1 10 3
3/17/2020 21:26 India 1 10 0
3/17/2020 21:48 India 1 10 0
The Dataset is based on the transaction Datetime with some of the dimensions associated to it.
The first transaction occured on: 3/17/2020 14:42 , I want to know how many such transactions are available between the first transaction date and next 1 hour interval. So In the data we can see the first and second record satisfies this criteria so the last column "Expected Output" as 2 in the first row and 0 in the 2nd row.
again the counter will start from the third records : 3/17/2020 15:48 so for this record we can see there are total of 4 such transactions which falls between and current datetime + 1 hour interval so the 3 row is set as 4 and the row 4,5,6 is set as 0 and so on.
Requesting your help on the same.

Related

MS-Access - Merge data between two tables base on conditions

I am using MS Access and I am trying to create a query between two tables and merge same rows base on:
cust_id = cust_id and
a_date = f_date and
price = paid
and have the desire output.
My data now:
tblapp
app_id cust_id a_date price a_memo
------------------------------------------
1 1 10/10/20 20 hello
2 1 11/10/20 10 bye
3 2 12/10/20 30 hi
4 2 12/10/20 30 text
5 2 12/10/20 30 lol
6 2 12/10/20 30 ciao
7 3 14/10/20 25 peace
tblfin
fin_id cust_id f_date paid
----------------------------------
1 1 10/10/20 20
2 1 11/10/20 10
3 1 11/10/20 10
4 2 12/10/20 30
5 3 14/10/20 25
As you can see,
cust_id = 1 on 10/10/20 with bill 20 and paid 20
cust_id = 1 on 11/10/20 with bill 10 and paid 10 + 10
cust_id = 2 on 12/10/20 with bill 30 + 30 + 30 + 30 and paid 30
cust_id = 3 on 14/10/20 with bill 25 and paid 25
Derire query output:
app_id cust_id a_date price a_memo fin_id cust_id f_date paid
----------------------------------------------------------------------
1 1 10/10/20 20 hello 1 1 10/10/20 20
2 1 11/10/20 10 bye 2 1 11/10/20 10
3 2 12/10/20 30 hi 4 2 12/10/20 30
7 3 14/10/20 25 peace 5 3 14/10/20 25
Tried the following sql but i am getting duplicates(like cust_id 1 and 2 where data rows are not the same in two tables):
SELECT f.fin_id,
f.cust_id,
f.f_date,
f.paid,
a.app_id,
a.cust_id,
a.a_date,
a.price,
a.a_memo
FROM tblfin AS f
LEFT JOIN tblapp AS a ON (f.cust_id=a.cust_id)
AND (f.f_date=a.a_date)
AND (f.paid=a.price);
Solution using MySQL is welcome. Thank you.

How retrieve all parent and child rows population in Oracle sql?

I have a table "TB_Population" with some records about the population from all over the world.
at this time I want to calculate each title's population in particular row
and demonstrate each level in that table.
I have this table with the following data:
ID TITLE PARENT_ID POPULATION
1 WORLD 10
2 AFRICA 1 5
3 ASIA 1 10
4 EUROPE 1 4
5 GERMANY 4 6
6 FRANCE 4 10
7 ITALY 4 4
8 JAPAN 3 6
9 MORROCO 2 1
10 SPAIN 4 9
11 INDIA 3 8
12 PORTUGAL 4 2
13 USA 14 10
14 AMERICA 1 10
15 NEWYORK 13 5
The expected output table should be as below
ID TITLE POPULATION LEVEL
1 WORLD 100 1
2 AFRICA 6 2
3 ASIA 24 2
4 EUROPE 35 2
5 GERMANY 6 3
6 FRANCE 10 3
7 ITALY 4 3
8 JAPAN 6 3
9 MORROCO 1 3
10 SPAIN 9 3
11 INDIA 8 3
12 PORTUGAL 2 3
13 USA 15 3
14 AMERICA 25 2
15 NEWYORK 5 4
Thanks and best regards
The tricky part which I see here is you want the LEVEL of title from "BOTTOM TO TOP" and POPULATION from "TOP TO BOTTOM". For example, AMERICA's level has to be 2 which means the LEVEL has to be measured from AMERICA -> WORLD, but AMERICA's population has to be 25 which is the sum of population measured from AMERICA -> NEWYORK. So, I tried this:
SELECT TOP_TO_BOTTOM.TITLE_ALIAS, TOP_TO_BOTTOM.TOTAL_POPULATION, BOTTOM_TO_TOP.MAX_LEVEL FROM
(SELECT TITLE_ALIAS, SUM(POPULATION) AS "TOTAL_POPULATION" FROM
(SELECT CONNECT_BY_ROOT TITLE AS "TITLE_ALIAS", POPULATION
FROM TB_POPULATION
CONNECT BY PRIOR ID = PARENT_ID)
GROUP BY TITLE_ALIAS) "TOP_TO_BOTTOM"
INNER JOIN
(SELECT TITLE_ALIAS, MAX(LEV) AS "MAX_LEVEL" FROM
(SELECT CONNECT_BY_ROOT TITLE AS "TITLE_ALIAS", LEVEL AS "LEV"
FROM TB_POPULATION
CONNECT BY PRIOR PARENT_ID = ID)
GROUP BY TITLE_ALIAS) "BOTTOM_TO_TOP"
ON
BOTTOM_TO_TOP.TITLE_ALIAS = TOP_TO_BOTTOM.TITLE_ALIAS
ORDER BY BOTTOM_TO_TOP.MAX_LEVEL;
You can have a look at the simulation here: https://rextester.com/HFTIH47397.
Hope this helps you

Calculated column - SQL - Football Teams

I'd like to add a new second column to a 'teams' table which is representative of premier league (UK) football rankings. At the moment the table just contains the names of each football team.
The column will be called 'Played' and it will list the number of games each team has played. I'd like to calculate this number (integer data type) from a separate table called 'games', which records a historic log of games fixtures. This would probably include using SQL's native 'COUNT' function.
I have tried to use a function to help me do this, but currently it is inserting all values as '0'
CREATE FUNCTION [dbo].[GetPlayed](#Team VARCHAR)
RETURNS INT
BEGIN
RETURN(SELECT COUNT(*)
FROM games
WHERE games.Home = #Team OR games.Away = #Team);
END;
ALTER TABLE teams
ADD Played AS GetPlayed(teams.Team)
The tables:
teams:
```Team
Arsenal
Bournemouth
Burnley
Chelsea
Crystal Palace
Everton
Hull City
Leicester City
Liverpool
Manchester City
Manchester United
Middlesbrough
Southampton
Stoke City
Sunderland
Swansea City
Tottenham Hotspur
Wat"For"d
West Bromwich Albion
West Ham United
```
games:
gameID Home HomeScore Away AwayScore GameDate
4 Arsenal 2 Chelsea 0 2018-05-26
5 Arsenal 5 Bournemouth 0 2018-04-22
6 Arsenal 1 Leicester City 1 2018-03-15
7 Bournemouth 5 Liverpool 0 2018-04-22
8 Burnley 5 Bournemouth 0 2018-04-22
9 Burnley 1 Swansea City 2 2017-11-22
10 Stoke City 0 Burnley 0 2018-01-08
11 Chelsea 1 Middlesborough 2 2017-11-22
12 Southampton 0 Chelsea 0 2018-01-01
13 Crystal Palace 1 Everton 2 2018-03-26
14 Manchester United 4 Crystal Palace 0 2018-06-01
15 Crystal Palace 0 Southampton 1 2018-04-16
16 Everton 1 Hull City 2 2017-11-20
17 Manchester City 4 Everton 0 2017-11-20
18 Hull City 0 Burnley 0 2018-06-01
19 Sunderland 2 Hull City 0 2018-06-15
20 Leicester City 3 Tottenham Hotspur 1 2017-09-20
21 Swansea City 2 Leicester City 5 2018-02-15
22 Sunderland 0 Leicester City 1 2018-01-29
23 Liverpool 3 Tottenham Hotspur 0 2018-02-28
24 Stoke City 1 Liverpool 2 2017-09-19
25 Manchester City 2 Manchester United 4 2018-05-02
26 Middlesborough 1 Southampton 1 2018-02-08
27 Stoke City 2 Middlesborough 2 2017-08-19
28 Swansea City 0 Manchester United 5 2018-06-27
29 Sunderland 1 Tottenham Hotspur 2 2017-09-01
Any help would be much appreciated!
Thanks, Rob
VARCHAR without size defaults to 1 char, you need to change your function declaration
CREATE FUNCTION [dbo].[GetPlayed](#Team VARCHAR(32))
.....
Without size your parameter #Team will receive just the first letter of your passed team value and, of course, the WHERE statement is unable to find any result in your games table

Construct a grouping column in SQL Server 2012

I have created a table that looks something like this:
ID TSPPLY_DT NEXT_DT DAYS_BTWN TIME_TO_EVENT CENSORED ENDPOINT
-----------------------------------------------------------------------------
1 2014-01-01 2014-01-10 10 10 0 0
1 2014-01-10 2014-01-21 11 21 0 0
1 2014-01-21 NULL NULL 21 1 0
2 2015-04-01 2015-04-30 30 30 0 0
2 2015-04-30 2015-05-03 1 31 0 1
2 2015-05-03 2015-05-06 3 34(should be 3)0 0
2 2015-05-06 2015-05-16 10 44(shouldbe 13)1 0
The TIME_TO_EVENT column however is not adding up correctly with my code - The idea is to add up the days between until either ID changes, CENSORED = 1 or ENDPOINT = 1.
I think what I need is an addition column where I can sum based on an aggregate of ID and GROUPING... With an output as follows:
ID TSPPLY_DT NEXT_DT DAYS_BTWN TIME_TO_EVENT CENSORED ENDPOINT GROUPING
----------------------------------------------------------------------------------------
1 2014-01-01 2014-01-10 10 10 0 0 A
1 2014-01-10 2014-01-21 11 21 0 0 A
1 2014-01-21 NULL NULL 21 1 0 A
2 2015-04-01 2015-04-30 30 30 0 0 A
2 2015-04-30 2015-05-03 1 31 0 1 A
2 2015-05-03 2015-05-06 3 3 0 0 B
2 2015-05-06 2015-05-16 10 13 1 0 B
So any ideas on how to create the GROUPING column? It would be something like IF next rows ID is the same as current row, check CENSORED AND ENDPOINT. If either = 1, for the next row, change the grouping to a new value. Once a new ID is reached, reset the grouping to A (or whatever arbitrary value) and run the test again.
You need to use the DATEDIFF function, like this:
DATEDIFF(d, TSPPLY_DT, NEXT_DT) AS DAYS_BTWN
Now you don't need GROUP BY.

Merge two tables using common fields

I have two tables, which I need to get data from table 1 to table 2 by matching customer name & Sale date. In the first table, the name is in two columns but the other table its in one column.
> list(CustomerSales.CSV)
[[1]]
CustomerFirstName CustomerLastName SaleDate_Time InvoiceNo InvoiceValue
1 Hendricks Eric 30-09-2015 13:00 10 5000
2 Fier Marilyn 02-10-2015 15:30 15 18000
3 O'Brien Donna 03-10-2015 13:30 16 25000
4 Perez Barney 03-10-2015 16:10 17 20000
5 Fier Marilyn 04-10-2015 11:10 18 6000
6 Hendricks Eric 05-10-2015 14:00 19 8000
> list(ReturnSales.CSV)
[[1]]
CustomerName SaleDate_Time ReturnDate_Time ReturnNo ReturnValue
1 Hendricks Eric 05-10-2015 14:00 10-10-2015 14:00 1 1000
2 O'Brien Donna 03-10-2015 13:30 15-10-2015 13:30 2 2000
3 Perez Barney 03-10-2015 16:10 12-10-2015 16:10 3 1500
4 Fier Marilyn 02-10-2015 15:30 08-10-2015 15:30 4 2000
The result should be a table like this.
list(ReturnSales.CSV)
[[1]]
CustomerName SaleDate_Time InvoiceNo InvoiceValue ReturnDate_Time ReturnNo ReturnValue
1 Hendricks Eric 05-10-2015 14:00 19 8000 10-10-2015 14:00 1 1000
2 O'Brien Donna 03-10-2015 13:30 16 25000 15-10-2015 13:30 2 2000
3 Perez Barney 03-10-2015 16:10 17 20000 12-10-2015 16:10 3 1500
4 Fier Marilyn 02-10-2015 15:30 15 18000 08-10-2015 15:30 4 2000
Table 2 customer name & SaleDate_Time should be match with table 1 CustomerFirstName, CustomerLastName, & SaleDate_Time. Then combine from table 1, InvoiceNo & InvoiceValue to table 2.
Any suggestions?
Are you looking for SQL Query for the above scenario then you can something like below.
SELECT RS.CustomerName
,CS.SaleDate_Time
,CS.InvoiceNo
,CS.InvoiceValue
,RS.Return_DateTime
,RS.ReturnNo
,RS.ReturnValue
FROM CustomerSales CS
INNER JOIN ReturnSales RS
ON RS.CustomerName = CS.CustomerfirstName + ' ' + Cs.CustomerLastName
WHERE RS.SaleDate_Time = CS.SaleDate_Time