there are two tables tbldoctor and tblschedule
tbldoctor
doc_id(PK) docMrId docfname
1 22 manish
2 23 rahul
3 22 ashish
4 24 ahemad
5 22 narendra
6 22 akshat
tblschedule
doctor_id(FK) mr_id schedule_date
1 22 2012-06-12
1 22 2012-06-13
3 22 2012-06-14
3 22 2012-06-14
4 24 2012-06-12
4 24 2012-06-12
5 22 2012-06-14
5 22 2012-06-15
5 22 2012-06-12
What i want is list of all doctorid and the count of particular doctor repeated in tblschedule table where schedule_date parameter and mr_id is provided here we are supposed mr_id =22 and schedule_date between 2012-06-01 to 2012-06-31
Output Should look like
doc_id docfname count
1 manish 2
3 rahul 2
5 narendra 3
6 akshat 0
select d.doc_id, docfname, count(mr_id) as count
from tbldoctor d
left outer join tblschedule s on s.doctor_id = d.doc_id
where mr_id = 22
and schedule_date between '2012-06-01' and '2012-06-31'
group by s.doctor_id
edit
select d.doc_id,
docfname,
sum(case when mr_id = 22
then 1
else 0
end) as count
from tbldoctor d
left outer join tblschedule s on s.doctor_id = d.doc_id
and schedule_date between '2012-06-01' and '2012-06-31'
group by s.doctor_id
SQLFiddle example
edit 2
how about
select d.doc_id, docfname, count(mr_id) as count
from tbldoctor d
left outer join tblschedule s on s.doctor_id = d.doc_id and mr_id = docmrid
where docmrid = 22
and (schedule_date between '2012-06-01' and '2012-06-31' or mr_id is null)
group by s.doctor_id
order by d.doc_id
SQLFiddle example
Not 100% sure I've understood you, but this will give you a list of all doctor IDs, along with the number of schedule items for each in between two given dates.
SELECT tbldoctor.doc_id, COUNT(*) cnt
FROM tbldoctor d INNER JOIN tblschedule s
ON d.doc_id = s.doctor_id
WHERE s.schedule_date BETWEEN '2012-06-12' AND '2012-06-15'
GROUP BY tbldoctor.doc_id
SELECT * FROM `tbldoctor` JOIN `tblschedule` ON `tblschedule`.doctor_id(FK) = `tbldoctor`.doc_id(PK)
SELECT tbldoctor.doc_id, COUNT(*) cnt FROM tbldoctor d INNER JOIN tblschedule s ON d.doc_id = s.doctor_id WHERE s.schedule_date BETWEEN '2012-06-12' AND '2012-06-15' GROUP BY tbldoctor.doc_id
Related
I have some problem in mysql, I have data like this
id
date
renewaltime
111
2020-01-01
1
111
2020-01-02
1
111
2020-01-03
1
111
2020-01-04
1
111
2020-01-05
1
but I wanna output be like this in mysql
id
date
renewaltime
renewal_by_date
111
2020-01-01
1
1
111
2020-01-02
1
2
111
2020-01-03
1
3
111
2020-01-04
1
4
111
2020-01-05
1
5
adding the value with value before in diffrent datetime
SELECT
h.id as ID,
i.date as DATE,
count(it.relid) as RENEWALTIME
FROM tblhosting h
INNER JOIN tblinvoiceitems it on h.id=it.relid
INNER JOIN tblinvoices i on it.invoiceid=i.id
WHERE h.id =305864
and i.status = 'Paid'
AND it.type = 'Hosting'
AND h.regdate <> it.duedate
GROUP BY i.date
please if you have same problem and share withme
thanks
On MySQL 8+, this is a cinch with analytic functions:
SELECT h.id, i.date,
SUM(COUNT(it.relid)) OVER (ORDER BY i.date) RENEWALTIME
FROM tblhosting h
INNER JOIN tblinvoiceitems it ON h.id = it.relid
INNER JOIN tblinvoices i ON it.invoiceid = i.id
WHERE
h.id = 305864 AND
i.status = 'Paid' AND
it.type = 'Hosting' AND
h.regdate <> it.duedate
GROUP BY
i.date
ORDER BY
i.date;
I have 4 tables in similar form. Structures of these tables are like:
id team_id position_id country_id
1 1 1 3
2 1 1 3
3 2 2 3
4 3 3 3
I can count rows of one table with:
SELECT count(position_id) as count1, position_id
FROM players1
where country_id = 3
group by position_id;
Getting result as:
position_id count1
1 54
2 41
3 39
4 32
I want join 4 tables and want to get a result like:
position_id count1 count2 count3 count4
1 54 42 51 61
2 41 40 49 59
3 39 29 44 50
4 32 21 37 47
Can you help me write this sql?
As I have understand you question. Execute this Mysql query.
SELECT
d1.position_id AS Positions_Id,
d1.count1 AS count1,
d2.count1 AS count2,
d3.count1 AS count3,
d4.count1 AS count4
FROM (
SELECT position_id, COUNT(position_id) AS count1
FROM players1
WHERE country_id=3
GROUP BY position_id) AS d1
LEFT JOIN (
SELECT position_id, COUNT(position_id) AS count1
FROM players2
WHERE country_id=3
GROUP BY position_id
) AS d2 ON d2.position_id = d1.position_id
LEFT JOIN (
SELECT position_id, COUNT(position_id) AS count1
FROM players3
WHERE country_id=3
GROUP BY position_id
) AS d3 ON d3.position_id = d1.position_id
LEFT JOIN (
SELECT position_id, COUNT(position_id) AS count1
FROM players4
WHERE country_id=3
GROUP BY position_id
) AS d4 ON d4.position_id = d1.position_id
I want to make SQL that will return users who not performed any points/redeem activity in last 12 months
user_data
userId email create_date
1 steve#gmail.com 2017-01-05 12:55:00
2 mark_nel#gmail.com 2019-05-15 12:13:00
3 les.born#gmail.com 2018-04-05 03:15:00
points_data
id user_id activity_id activity_points create_date
1 1 1 10 2017-01-05 11:09:00
2 2 1 15 2019-06-12 09:17:00
3 2 2 10 2019-08-05 02:55:00
4 3 1 10 2019-01-05 07:15:00
redeem_data
id user_id redeem_points create_date
1 2 20 2019-09-11 02:55:00
Result
email
steve#gmail.com
Use LEFT JOIN concept to include non matching row from table user_data and only select non matching row by removing matching rows using where id of both tables(points_data,redeem_data) is null.
SELECT u.email
FROM user_data u
LEFT JOIN points_data p ON u.userId = p.user_id AND p.create_date >= CURRENT_DATE - INTERVAL 1 YEAR
LEFT JOIN redeem_data r ON u.userId = r.user_id AND r.create_date >= CURRENT_DATE - INTERVAL 1 YEAR
WHERE p.id IS NULL AND r.id IS NULL;
I have two tables
table: a
---------------------
id amount status
---------------------
4031 3000 1
4032 4000 1
4033 5000 1
4034 6000 1
4035 7000 1
4036 8000 0
table: s
--------------
id a_id b_id
--------------
1 4031 20
2 4031 21
3 4032 23
4 4032 24
5 4033 25
6 4033 26
7 4034 21
8 4034 20
9 4035 25
10 4035 29
11 4036 21
12 4036 20
How do we get the sum of the a.amount where have ( b_id = 20 AND b_id = 21) AND a.status = 1?
The answer should be 9000.
SELECT SUM(amount) FROM (
JOIN s ON a.id = s.id
WHERE STATUS =1
AND (b_id = 20 OR b_id = 21) GROUP BY a.id
) AS amounts
total : 9000
In the case you can add several times the same amount, I guess this should work without join:
SELECT SUM(amount) AS total
FROM `a`, `s`
WHERE a_id = a.id AND (b_id = 20 OR b_id = 21) AND status = 1
total : 18000
Try this:
select sum(a.amount)
from a
join b on a.id = b.a_id
where b.b_id IN ( 20, 21 ) and a.status = 1
You can get the answer using a subquery:
SELECT SUM(a.amount)
FROM a
WHERE a.status=1 AND
EXISTS (SELECT 1
FROM s
WHERE s.a_id=a.id AND s.b_id in (20,21));
There is no need to group the data as we want the global sum of the amounts selected.
SELECT SUM(a.amount)
FROM a
WHERE a.status=1 AND
EXISTS (SELECT 1
FROM s
WHERE s.a_id=a.id AND s.b_id=20) AND
EXISTS (SELECT 1
FROM s
WHERE s.a_id=a.id AND s.b_id=21) ;
If I have table logs :
ID Date P_id TYPE
-------------------------------
1 2016-9-1 11 adClick
2 2016-9-1 22 adComplete
3 2016-9-1 11 adComplete
4 2016-9-3 22 adClick
5 2016-9-3 22 adClick
6 2016-9-1 44 adClick
7 2016-9-3 44 adComplete
8 2016-9-3 44 adClick
9 2016-9-3 11 adClick
-------------------------------
and another table report having the same Date & P_id as follows :
ID Date P_id clicks
--------------------------------
1 2016-9-1 11
2 2016-9-1 11
3 2016-9-1 22
4 2016-9-3 22
5 2016-9-1 11
6 2016-9-1 44
5 2016-9-1 44
6 2016-9-1 11
---------------------------------
I need MySQL query to fill clicks in report table and according to the key (Date & P_id) :
clicks =
count of rows having (Date & P_id) in Report table
divided by
count of rows having (Date & P_id) and Type is adClick
So the table will be :
ID Date P_id clicks
--------------------------------
1 2016-9-1 11 4 / 1
2 2016-9-1 11 4 / 1
3 2016-9-1 22 0
4 2016-9-3 22 2 / 2
5 2016-9-1 11 4 / 1
6 2016-9-1 44 2 / 1
5 2016-9-1 44 2 / 1
6 2016-9-1 11 4 / 1
---------------------------------
Sample, first row :
2016-9-1 11 4 / 1
4 rows (2016-9-1 11) in report table by
1 row (2016-9-1 11) in logs table with type=adClick
What I have tried so far :
UPDATE report AS r
INNER JOIN
(
SELECT
*, count(id) AS count_value
FROM
logs
WHERE
type= "adClick"
GROUP BY
date,p_id
) log
ON r.date=log.date AND r.p_id=log.p_id
SET r.clicks=(log.count_value / (SELECT COUNT(lof) from report) );
Thanks in advance
You can do this using conditional aggregation:
UPDATE report AS r INNER JOIN
(SELECT date, p_id, count(*) AS cnt,
SUM(type = 'adClick') as cnt_adclick
FROM logs
GROUP BY date,p_id
) l
ON r.date = l.date AND r.p_id = l.p_id
SET r.clicks = cnt_adclick / cnt;
You can also do this with avg() instead of division:
UPDATE report AS r INNER JOIN
(SELECT date, p_id,
AVG(type = 'adClick') as avg_clicks
FROM logs
GROUP BY date,p_id
) l
ON r.date = l.date AND r.p_id = l.p_id
SET r.clicks = avg_clicks;
Try using CONCAT()
UPDATE report AS r
INNER JOIN (
SELECT
date,
p_id,
count(*) AS count,
SUM(type = 'adClick') AS count_adclick
FROM
logs
GROUP BY
date,
p_id
) log ON r.date = log.date
AND r.p_id = log.p_id
SET r.clicks = CONCAT(log.count_adclick, ' / ', log.count);
Thanks all, the following did the job for me :
UPDATE report AS r
INNER JOIN
(
SELECT cnt_adclick,count(*) as cnt_report,date, report.P_id
FROM report
INNER JOIN
(
SELECT date as date, P_id, SUM(event_type = 'adClick') as cnt_adclick
FROM logs
GROUP BY date,P_id
) inner_log
ON report.date = inner_log.date AND report.P_id = inner_log.P_id
GROUP BY report.date, report.P_id
) l
ON r.date = l.date AND r.P_id= l.P_id
SET r.clicks = cnt_adclick / cnt_report;