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"
Related
I have a scenario in below:
Name
LoginDate
Peter
2020-01-01
Peter
2020-01-02
Mary
2020-01-01
Peter
2020-02-02
Mary
2020-02-05
Chris
2020-02-07
How to write the SQL which can be re-organized in below
Name
Jan
Feb
Peter
2
1
Mary
1
1
Chris
0
1
Appreciated your help!
We can use Month() to do it
create table login_record(
name varchar(100),
login_date date
);
insert into login_record(name,login_date) values
('Peter','2020-01-01'),
('Peter','2020-01-02'),
('Mary','2020-01-01'),
('Peter','2020-02-02'),
('Mary','2020-02-05'),
('Chris','2020-02-07');
SELECT
name,
SUM(IF(month(login_date) = 1,1,0)) AS `Jan`,
SUM(IF(month(login_date) = 2,1,0)) AS `Feb`,
SUM(IF(month(login_date) = 3,1,0)) AS `Mar`
-- sum other month
FROM login_record
group by name
order by name
DB Fiddle Demo
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
I am trying to get the sum of costs by date, if the date lies between the start and end date which is coming in from a different table.
To explain:
Table 1
id
date
cost
1
2020-02-02
$10
2
2020-03-05
$100
1
2020-03-10
$200
3
2020-07-16
$200
1
2019-01-01
$50
1
2019-02-10
$50
3
2012-10-01
$500
Table 2
id
start_date
end_date
1
2020-01-01
2020-12-31
3
2020-01-01
2020-11-15
2
2020-01-01
2021-01-31
I just want to aggregate the costs by month only if the date in table 1 lies within the start and end date of table 2. So here I want the output to look like:
date
cost
2020-01-31
$10
2020-02-29
$10
2020-03-31
$300
2020-04-30
-
2020-05-31
-
2020-06-30
-
2020-07-31
$200
2020-08-31
-
2019-09-30
-
2019-10-31
$500
2019-11-30
-
2019-12-31
-
What I have tried so far:
select sum(cost), last_day(date)
from table1 inner join table2
on table1.id=table2.id
and table1.date>=table2.start_date and table1.date<=table2.end_date
group by last_day(date)
select year(date) as year,month(date) AS month, sum(cost) AS cost
from table1 inner join table2
on table1.id=table2.id
and table1.date>=table2.start_date and table1.date<=table2.end_date
group by table1.id, year(date), month(date)
order by last_day(date)
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
Mysql query I have following tables
Company table:-
id company_name company_createddate
1 ABC 2015-01-01 12:45:23
2 LMN 2015-01-11 09:45:23
3 PQR 2015-02-16 23:45:23
User table:-
id username
1 John
2 Mary
Order table:-
id company_id user_id order_name order_createddate
1 1 1 Sales 2015-02-16 09:45:23
2 1 1 Sales3 2015-02-20 09:45:23
3 1 1 Marketing 2015-02-24 09:45:23
4 2 1 Sales2 2015-02-17 09:45:23
5 1 2 M1 2015-02-16 09:45:23
6 2 2 M2 2015-02-23 09:45:23
7 2 2 P1 2015-02-26 09:45:23
Output required:-
Week day user name Pipeline Orders
16 Feb - 22 Feb John 2 (ABC), 1(LMN)
23 Feb - 01 Mar John 3 (ABC), 1(LMN)
16 Feb - 22 Feb Mary 1 (ABC)
23 Feb - 01 Mar Mary 1 (ABC), 2(LMN)
Week days is order_createddate
In 16Feb-22Feb Week, 2 orders created for company ABC
And in 23Feb-01Mar Week, 1 order created for company ABC, I want to show total number of order generated till 01 Mar Week, So i need output of 3.
Please help!
select concat(STR_TO_DATE(concat('2015 ',wk,' 1'), '%Y %u %w'), ' to ',
STR_TO_DATE(concat('2015 ',wk,' 0'), '%Y %u %w')),
User.username, GROUP_CONCAT(som) from (
select DATE_FORMAT(order_createddate,'%u') wk,user_id,
concat(Company.company_name ,'(',CONVERT(count(1),CHAR),')') som from `Order`
join Company on `Order`.company_id = Company.id
group by wk,user_id,company_name ) t
join User on t.user_id = User.id
group by wk, User.username
the time limit should be in sub select clause t.
if you dont like the week format, change them by modify the first line's date format
sql fiddle here