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;
Related
i have table structure like this
id
user_id
parent_id
club
1
1
club1
2
2
1
club1
3
3
1
club1
4
4
2
club1
5
5
2
club1
6
6
3
club1
7
7
3
club1
8
8
4
club1
9
9
4
club1
10
10
5
club1
11
11
5
club1
12
12
6
club1
13
13
6
club1
14
14
7
club1
15
15
7
club1
i want to select user_id whose child is less then 2.
refer to above table user 1,2,3,4,5,6 and 7 two child complete so the query should return 8,9,10,11,12,13,14,15
Refer to above table here user_id 1 have two child 2 and 3 , user_id 2 have child 4 and 5 and so on.
i need to select user_id whose child is less than 2
You can do it as follows :
SELECT user_id
FROM club_tree
WHERE user_id NOT IN (
SELECT parent_id
from club_tree
where club = 'club1'
group by parent_id
HAVING count(1) >= 2
)
and club = 'club1';
We Select users that are not in the list of users with more than two childs.
This is to get users with more than two childs :
SELECT parent_id
from club_tree
group by parent_id
HAVING count(1) >= 2
Here you can test it : http://sqlfiddle.com/#!9/eb2c70/3
Can you try this query using left join :
SELECT c.user_id
from club_tree c
left join
(
select parent_id
from club_tree
where club = 'club1'
group by parent_id
HAVING count(1) >= 2
) as c2 on c.user_id = c2.parent_id
where c.club = 'club1' and c2.parent_id is null;
Try it here : http://sqlfiddle.com/#!9/eb2c70/17
I have 2 tables.
MARKET TABLE
ID main_key sub_key name created_at
------------------------------------------------------
1 1 1 bottle 1606636000
2 2 1 flask 1606642546
3 2 2 flask 1606650045
4 3 1 can 1606650445
5 3 2 can 1606651546
6 4 1 glass 1606652545
MARKET_UPDATES TABLE
ID main_key sub_key price update
------------------------------------------------------
1 1 1 100 1606665555
2 2 1 120 1606665555
3 2 2 150 1606665555
4 3 1 500 1606665555
5 3 2 550 1606665555
6 4 1 25 1606665555
7 1 1 110 1606665666
8 2 1 135 1606665666
9 2 2 145 1606665666
10 3 1 490 1606665666
11 3 2 440 1606665666
12 4 1 29 1606665666
I've tried this.
SELECT *
FROM market m
JOIN (
SELECT MAX(id) max_id, fk_main_key
FROM market_update
GROUP BY fk_main_key, sub_key
) m_max ON (m_max.fk_main_key = m.main_key)
JOIN market_update mu ON (mu.id = m_max.max_id)
But it multiples it a lot of times and I end up with a lot of the same lines. I think it multiples main_key amount and sub_key amount.
I am trying to JOIN MARKET_UPDATES into MARKET so I can get the latest prices from MARKET_UPDATES, but my issue is that I have 2 id's that I have to check on, main_key & sub_key.
So I have to merge the MARKET table with id(7-12) in MARKET_UPDATES.
here is one way based on your query :
SELECT *
FROM market m
JOIN (
SELECT MAX(id) max_id , fk_main_key, sub_key
FROM market_update
GROUP BY fk_main_key, sub_key
) m_max
ON (m_max.fk_main_key = m.main_key
and m_max.sub_key = m.sub_key)
JOIN market_update mu ON (mu.id = m_max.max_id)
and here is another way using window function RANK() :
SELECT *
FROM market m
JOIN (
SELECT id
, fk_main_key
, sub_key
, rank() over (partition by fk_main_key, sub_key order by id desc ) rnk
FROM market_updat
) mu
ON mu.fk_main_key = m.main_key
and mu.sub_key = m.sub_key
and rnk = 1
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) ;
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