Count number of rows before date per id - sql

I'm not sure how else to explain it other than the title. I'm basically trying to get the number of rows per id before the date on that specific row. I've tried a bunch of things and scoured the internet to no avail. Please help!
Before
id date
1 3/3/2015
2 3/27/2015
2 4/15/2015
2 5/1/2015
3 3/7/2015
3 5/17/2015
3 7/9/2015
3 7/19/2015
After
id date count
1 3/3/2015 0
2 3/27/2015 0
2 4/15/2015 1
2 5/1/2015 2
3 3/7/2015 0
3 5/17/2015 1
3 7/9/2015 2
3 7/19/2015 3

-1 + row_number() over (partition by id order by date)

Related

Creating 2 additional columns based on past dates - PostgresSQL

Seeking some help after spending alot of time on searching but to no avail and decided to post this here as I'm rather new to SQL, so any help is greatly appreciated. I've tried a few functions but can't seem to get it right. e.g. GROUP BY, BETWEEN etc
On the PrestoSQL server, I have a table as shown below starting with columns Date, ID and COVID. Using GROUP BY ID, I would like to create a column EverCOVIDBefore which looks back at all past dates of the COVID column to see if there was ever COVID = 1 or not, as well as another column called COVID_last_2_mth which checks if there was ever COVID = 1 within the past 2 months
(Highlighted columns are my expected outcomes)
Link to dataset: https://drive.google.com/file/d/1Sc5Olrx9g2A36WnLcCFMU0YTQ3-qWROU/view?usp=sharing
You can do:
select *,
max(covid) over(partition by id order by date) as ever_covid_before,
max(covid) over(partition by id order by date
range between interval '2 month' preceding and current row)
as covid_last_two_months
from t
Result:
date id covid ever_covid_before covid_last_two_months
----------- --- ------ ------------------ ---------------------
2020-01-15 1 0 0 0
2020-02-15 1 0 0 0
2020-03-15 1 1 1 1
2020-04-15 1 0 1 1
2020-05-15 1 0 1 1
2020-06-15 1 0 1 0
2020-01-15 2 0 0 0
2020-02-15 2 1 1 1
2020-03-15 2 0 1 1
2020-04-15 2 0 1 1
2020-05-15 2 0 1 0
2020-06-15 2 1 1 1
See running example at db<>fiddle.

assign value to a group pandas based on a day difference between them

I have a dataframe with ID and date ( and calculated day difference between the rows for the same ID)
ID date day_difference
1 27/06/2019 0
1 28/06/2019 1
1 29/06/2019 1
1 01/07/2019 2
1 02/07/2019 1
1 03/07/2019 1
1 05/07/2019 2
2 27/06/2019 0
2 28/06/2019 1
2 29/06/2019 1
2 01/08/2019 33
2 02/08/2019 1
2 03/08/2019 1
2 04/08/2019 1
which i would like to group by ID and calculate total duration with a condition if day difference is bigger than 30 days re-use that ID again and create a new group starting counting duration from that day after a 30day gap.
Desired result
ID Duration
1 8
2 3
2 4
Thanks.
You can do:
(df.groupby(['ID', df.day_difference.gt(30).cumsum()])
.agg(ID=('ID','first'), Duration=('ID','count'))
.reset_index(drop=True)
)
Output:
ID Duration
0 1 7
1 2 3
2 2 4

Find the sum of previous count occurrences per unique ID in pandas

I have a history of customer IDs and purchase IDs where no customer has ever bought the same product. However, for each purchase ID (which is unique), how can I find out the number of times the customer has made a previous purchase
I have tried using groupby() and sort_values()
df = pd.DataFrame({'id_cust': [1,2,1,3,2,4,1],
'id_purchase': ['20A','143C','99B','78R','309D','90J','78J']})
df.sort_values(by='id_cust')
df.groupby('id_cust')['id_purchase'].cumcount()
This is what I expect:
id_cust id_purchase value
1 20A 1
2 143C 1
1 99B 2
3 78R 1
2 3097D 2
4 900J 1
1 78J 3
You can just use the cumcount() on the id_cust column since the id_purchase is unique:
df['value']=df.groupby('id_cust')['id_cust'].cumcount()+1
print(df)
id_cust id_purchase value
0 1 20A 1
1 2 143C 1
2 1 99B 2
3 3 78R 1
4 2 309D 2
5 4 90J 1
6 1 78J 3

Customers who bought and not bought some product in last 90 days

I need a dax measure which shows me which customers bought products B and C in last 90 days.
And another one which shows me those whose bought products B and C in last 90 days.
(based in my filter date context)
Below is like it should be:
Can someone help me?
Here is a sample data if needed:
FactSales
KeyDate KeyCustomer KeyProduct Total
1 1 1 12,9
1 2 2 13
1 3 1 156,4
1 4 1 564,8
2 1 1 894,8
2 2 1 56,5
3 1 2 564,85
3 2 3 564,8
4 1 1 1325,6
4 2 1 132,3
Customer
KeyCustomer Name
1 Jean
2 Mari
3 Lisa
4 Julian
5 Jhonny
Calendar
KeyDate Date
1 01/01/2018
2 02/01/2018
3 01/05/2018
4 01/08/2018
Product
KeyProduct Product
1 A
2 B
3 C
Try something along these lines:
IfBought = IF(
COUNTROWS(
FILTER(FactSales,
RELATED('Product'[Product]) IN {"B", "C"} &&
RELATED('Calendar'[Date]) > TODAY() - 90)
) > 0,
1, 0)
Note that May 1st is longer than 90 days ago as of today though, so you won't get the result you asked for unless you change 90 to 114 or greater.

DB Query matching ids and sum data on columns

Here is the info i have on my tables, what i need is to create a report based on certain dates and make a sum of every stock movement of the same id
Table one Table Two
Items Stocks
---------- ---------------------------
ID - NAME items_id - altas - bajas - created_at
1 White 4 5 0 8/10/2016
2 Black 2 1 5 8/10/2016
3 Red 3 3 2 8/11/2016
4 Blue 4 1 4 8/11/2016
2 10 2 8/12/2016
So based on a customer choice of dates (on this case lets say it selects all the data available on the table), i need to group them by items_id and then SUM all altas, and all bajas for that items_id, having the following at the end:
items_id altas bajas
1 0 0
2 11 7
3 3 2
4 6 4
Any help solving this?
Hope this will help:
Stock.select("sum(altas) as altas, sum(bajas) as bajas").group("item_id")