Calculate Employee Experience from Multiple Jobs - mysql

I have a table named employee experience with id, userId, startDate, endDate columns.
I want to calculate employee experience. Can someone please help with mysql query or JPA specification code?
For example in case of following data:
id
userID
startDate
endDate
1
1
2021-01-01
2022-01-01
2
2
2019-01-01
2020-01-01
3
2
2020-01-02
2021-01-01
4
3
2021-01-01
2022-01-01
the output should be:
userID
experience
1
1
2
2
3
1

Successfully did this with the following:
SELECT SUM(TIMESTAMPDIFF(YEAR, START_DATE, END_DATE)) AS experience,
SOCIAL_PROFILE_ID
FROM tableName
GROUP BY SOCIAL_PROFILE_ID

Related

Counting club memberships in SQL

EDIT: I have added the primary key, following the comment by #Strawberry
The aim is to return the number of current members, and also the number of past memberships, on any particular date/time.
For example, suppose we have
msid id start cancelled
1 1 2020-01-01 09:00:00 null
2 2 2020-01-01 09:00:00 2020-12-31 09:00:00
3 2 2021-01-01 09:00:00 null
4 3 2020-01-01 09:00:00 2020-06-30 09:00:00
5 3 2020-02-01 09:00:00 2020-06-30 09:00:00
6 3 2020-07-01 09:00:00 null
and we want to calculate the number of members at various times, which should return as follows
Datetime Current Past <Notes - not to be returned by the query>
2020-01-01 12:00:00 3 0 -- all 3 IDs have joined earlier on this date
2020-02-01 12:00:00 3 0 -- new membership for existing member (ID 3) is not counted
2020-06-30 12:00:00 2 1 -- ID 3 has cancelled earlier on this day
2020-07-01 12:00:00 3 0 -- ID 3 has re-joined earlier on this day
2020-12-31 12:00:00 2 1 -- ID 2 has cancelled earlier on this day
2021-01-01 12:00:00 3 0 -- ID 2 has re-joined earlier on this day
An ID may either be current or past, but never both. That is, if a past member re-joins, as in the case of ID 2 and 3 above, they become current members, and are no longer past members.
Also, a member may have multiple current memberships, but they can only be counted as a current member once, as in the case of ID 3 above.
How can this be achieved in MySQL ?
Here is a db<>fiddle with the above data
Test this:
WITH
cte1 AS ( SELECT start `timestamp` FROM dt
UNION
SELECT cancelled FROM dt WHERE cancelled IS NOT NULL ),
cte2 AS ( SELECT DISTINCT id
FROM dt )
SELECT cte1.`timestamp`, COUNT(DISTINCT dt.id) current, SUM(dt.id IS NULL) past
FROM cte1
CROSS JOIN cte2
LEFT JOIN dt ON cte1.`timestamp` >= dt.start
AND (cte1.`timestamp` < dt.cancelled OR dt.cancelled IS NULL)
AND cte2.id = dt.id
GROUP BY cte1.`timestamp`
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=942e4c97951ed0929e178134ef67ce69

SQL Insert end of day for user last activity

I have this table
user_id activity dt
1 login 2020-01-01 08:00:00
1 logout 2020-01-01 20:00:00
2 home 2020-01-01 19:00:00
1 profile 2020-01-02 08:00:00
I need to insert last day for every user and the event of the last day is the last activity of that user. for example if user A logout on 2020-01-01 20:00:00 then you insert user A logout on 2020-01-02 00:00:00. the example is like in the last 3 rows of this table:
user_id activity dt
1 login 2020-01-01 08:00:00
1 logout 2020-01-01 20:00:00
2 home 2020-01-01 19:00:00
1 profile 2020-01-02 08:00:00
1 logout 2020-01-02 00:00:00
2 home 2020-01-02 00:00:00
1 profile 2020-01-03 00:00:00
on 2020-01-01 there is 2 user that have activity, so you have to input last activity of user 1 and 2 and the time is 2020-01-02 00:00:00.
I already search this solution on internet but can't find out the way to do it. All I have done is insert it manual one by one
In modern MySQL since version 8.0 you can use next approach:
insert into tbl
select distinct
user_id,
last_value(activity) over (partition by user_id, date(dt)),
date_add(date(dt), interval 1 day) dt
from tbl
order by dt
;
share SQL query
Hmmm . . . To get the last activity on each date with the appropriate new dt column:
select user_id, activity,
date(dt) + interval 1 day
from (select t.*,
row_number() over (partition by user_id, date(dt) order by dt desc) as seqnum
from t
) t
where seqnum = 1;
For a result set, you can union this to the existing table. If you want to actually modify the table, then insert these rows into the table.

Calculate MRR Kpi without monthly invoices

I am trying to calculate mrr for each month.
The DB table 'boxes' looks like this:
project_id
product_id
payment_method_id
price
interval
booked_at
canceled_at
1
1
3
19.00
1
2020-12-01 00:00:00
NULL
1
2
3
39.00
1
2020-05-01 00:00:00
2020-11-05 19:10:27
4
1
3
39.00
12
2020-05-01 00:00:00
2020-11-05 19:10:27
Payment-Interval is in months. I need to show in KPI dashboard the mrr.
Currently I have this query:
SELECT DATE_FORMAT(booked_at, '%Y-%m') AS period, sum(price)
FROM
project_boxes
GROUP BY period;
The problem with the above query is that it doesn't show between months MRR and doesn't work with canceled boxes.
What am I missing? Any help is appreciated.

joining two database tables in mysql and laravel

I have these two tables.
as_tbl_company_holidays
id year company_id start_date, end_date, description
1 2020 1 2020-01-01 2020-01-01 New Year Holiday
2 2020 1 2020-02-14 2020-02-15 Valentine Holiday
3 2020 2 2020-01-01 2020-01-01 New Year Holiday
as_tbl_employee_holidays
id employee_id company_id year start_date end_date description
1 ASL100 1 2020-01-31 2020-01-31 Casual Holiday
2 ASL200 2 2020-04-01 2020-04-02 Easter Holiday
How can i join these two table to get a holiday data for a particular employee i.e.
id employee_id company_id year start_date end_date description
1 ASL100 1 2020 2020-01-01 2020-01-01 New Year Holiday
2 ASL100 1 2020 2020-02-14 2020-02-15 Valentine Holiday
3 ASL100 1 2020-01-31 2020-01-31 Casual Holiday
id employee_id company_id year start_date end_date description
1 ASL200 2 2020 2 2020-01-01 2020-01-01 New Year Holiday
2 ASL200 2 2020 2 2020-04-01 2020-04-02 Easter Holiday
Meanwhile, I wrote this SQL query and it's returning duplicate data. How can I combine the data from both tables into one.
select
emp.employee_id,
comp.company_id,
comp.year,
comp.start_date,
comp.end_date
from
as_tbl_company_holidays as comp
left join as_tbl_employee_holidays as emp on comp.company_id = emp.company_id
where
emp.employee_id = "ASL100" and
comp.company_id = 1
I guess you need a union query instead of join
select company_id,year, start_date, end_date,description
from as_tbl_company_holidays
where company_id = 1
union
select company_id,year, start_date, end_date ,description
from as_tbl_employee_holidays
where company_id = 1
and employee_id = "ASL100"

count of users who've bought same item on a given day 1,2,3,...n in MySql

I using MySql version 5.7
I have a table that looks like this:
user_id item_id date
1 2 2020-01-01
1 2 2020-01-01
1 2 2020-01-01
1 3 2020-01-01
1 4 2020-01-01
33 7 2020-02-02
33 7 2020-02-02
44 11 2020-02-02
44 11 2020-02-02
I want to count number of users who have bought same item on a given day.
Desired result:
date one two three
2020-01-01 1 0 1
2020-02-02 0 2 0
one column = number of users who've bought same item once in a given day
two column = number of users who've bought same item twice in a given day.
Let me know if anything is unclear.
Thanks in advance!
Hmmm . . . Two levels of aggregation:
select date,
sum(cnt = 1) as one,
sum(cnt = 2) as two,
sum(cnt = 3) as three
from (select date, user_id, item_id, count(*) as cnt
from t
group by uesr_id, item_id
) ui
group by date