Using JOINs and group by together in single sql query - mysql

I'm not able to use JOINS and group-by together:
I searched for this...but I didn't find the solution..Problem is as below:
I have two tables, first is main_table with fields eid, name, status
Second table is followups with fields fid, eid, last_date, remarks, and next_date
Second table is used to store followup details of clients (client details stored in main_table) and i want to get last followup record for each client with selected date period (from_date to to_date) and sort by next_date in descending order.
i used the below query but not working
SELECT *
FROM main_table as MT
JOIN followups as MF on MT.eid=MF.eid
WHERE MT.status='open' AND MF.NDate<='2012-12-07'
GROUP BY MF.eid
ORDER BY MF.next_date DESC
thanks in advance...guys

Try This
SELECT * FROM main_table AS MT
LEFT JOIN (SELECT * FROM (SELECT * FROM followups ORDER BY next_date DESC) AS A GROUP BY eid) AS MF ON MT.eid=MF.eid
WHERE MT.status='open'
AND MF.NDate<='2012-12-07';

You can try something like this:
select m.eid,
m.name,
m.status,
f1.last_date,
f1.remarks,
f1.next_date
from maintable m
left join
(
select max(last_date) maxLast, eid
from followups
where last_date between from_date and to_date
group by eid
) f
on m.eid = f.eid
left join followups f1
on f.maxLast = f1.last_date
and f.eid = f1.eid
where m.status='open'
and f1.next_date<='2012-12-07'
order by f1.next_date desc

Try this:
SELECT * FROM main_table AS MT
LEFT JOIN (SELECT * FROM (SELECT * FROM followups WHERE NDate<='2012-12-07' ORDER BY next_date DESC) AS A GROUP BY eid) AS MF ON MT.eid=MF.eid
WHERE MT.status='open';

Related

MySQL group by only on specific amount of records (query inside JOIN expression)

I have a History table that represents messages that are stored by some sort of chat program.
It has a user id, message & datetime. (specified on: http://sqlfiddle.com/#!9/49fcefb/5)
Now, what I want is to have a result which contains:
The max amount of messages typed by date
What user has the most messages and howmany messages this user typed)
I got it working, but I find the query rather slow, I think its because of the last GROUP BY (GROUP BY b.cnt) as this will group over all the records found.
Query:
SELECT b.cnt as dayCount, a.cnt as userCount, a.userid as userId, b.date
FROM (
select date_added as date, user_id as userid, count(*) as cnt from history group by userid, day(date_added),month(date_added),year(date_added)
) a
INNER JOIN (
select date_added as date, count(*) as cnt from history group by day(date_added),month(date_added),year(date_added)
) b ON year(a.date) = year(b.date)
AND month(a.date) = month(b.date)
AND day(a.date) = day(b.date)
GROUP BY b.cnt
ORDER BY dayCount desc, userCount desc limit 10;
Can someone advice me on how to solve this? Maybe with another sort of query?
Thanks in advance!
You can try using windows function with Subquery to get the same result.
select SQ.daycount,SQ.usercount,SQ.user_id, SQ.date1 from (
select sum(count(Message)) over (partition by date(date_added)) as daycount, --to get max amount of msg by date
max(count(Message)) over (partition by date(date_added)) as maxuser, -- to identify user with most msg
count(Message) as usercount,
date(date_added) as date1,user_id from history
group by date1,user_id ) SQ
where SQ.usercount=SQ.maxuser
Check the Fiddle here
seems you are joining with wrong criteria.
first table is getting the # of users, 2nd table is getting the #of days per user_id. see dbfiddle
SELECT b.cnt as dayCount, a.cnt as userCount, a.userid as userId, b.date
FROM (
select
user_id as userid
, count(*) as cnt
from history
group by userid
) a
LEFT JOIN (
select cast(date_added as date) as date
, count(*) as cnt
, user_id as userid
from history
group by cast(date_added as date)
) b ON a.userid = b.userid
ORDER BY dayCount desc, userCount desc limit 10;

Mysql Group By get latest record with Count

This is my customer table.
I want to group by emp_id alongwith the count. But Group By gets the 'first' record and not the 'newest' one.
I have tried various queries, like this
SELECT id, emp_id, COUNT( * ) AS count, created_at
FROM customer c
WHERE created_at = (
SELECT MAX( created_at )
FROM customer c2
WHERE c2.emp_id = c.emp_id
)
GROUP BY emp_id
ORDER BY created_at DESC
LIMIT 0 , 30
But cannot get the count. Please help.
Edit: this answer doesn't help to obtain count
Try joining to a subquery:
SELECT c1.id, c1.emp_id, c1.created_at, c2.cnt
FROM customer c1
INNER JOIN
(
SELECT emp_id, MAX(created_at) AS max_created_at, COUNT(*) AS cnt
FROM customer
GROUP BY emp_id
) c2
ON c1.emp_id = c2.emp_id AND c1.created_at = c2.max_created_at;
please try this
SELECT cust1.id, cust1.emp_id, cust1.created_at, cust2.cnt
FROM customer cust1
INNER JOIN
(
SELECT emp_id, MAX(created_at) AS max_created_at, COUNT(*) AS count
FROM customer
GROUP BY emp_id
) cust2
ON cust1.emp_id = cust2.emp_id AND cust1.created_at = cust2.max_created_at;

Select element from table Where something in Select

I have this query
SELECT
equipe_id
FROM
user
GROUP BY
equipe_id
HAVING
COUNT(*) >= 5
and it returns 1 and 3.
I want to use this next query using the previous query
SELECT
name,
location
FROM
equipe
WHERE
equipe_id IN ??? previous query result (1,3) ???
I hope that makes sense. Thank you
SELECT name
, location
FROM equipe
WHERE equipe_id in (SELECT equipe_id FROM user GROUP BY equipe_id HAVING COUNT(*) >= 5);
We can also try doing this via joining to a subquery:
SELECT e1.name, e1.location
FROM equipe e1
INNER JOIN
(
SELECT equipe_id
FROM user
GROUP BY equipe_id
HAVING COUNT(*) >= 5
) e2
ON e1.equipe_id = e2.equipe_id;
As an alternative, this can be done also without the inner subquery, like this:
SELECT
e.name,
e.location
FROM
equipe AS e
INNER JOIN
user AS u ON u.equipe_id = e.equipe_id
GROUP BY
e.equipe_id, e.name, e.location
HAVING
COUNT(*) >= 5

(MySQL) Get last data in every group (SubQuery)

Table:
Table
Help me in. I'm suck in SubQuery. I wanna get the latest Name from every category group
Output:
Output
I already search some reference in google and still didn't understand. Hopefully this time I can understand
you can use join on max_date for each category
select * from
my_table m
inner join (
select category, max(date) max_date
from my_table
group by category) as t on m.date = t.max_date and m.category = t.category
Thx scaisEdge!!
I finally got it!
So this is my final query
select m.id, m.category, m.name, m.time
from my_table m
inner join (
select id, category, name, time
from my_table
order by id desc
) as t
on m.id = t.id
group by category

MAX aggregate function in sql server

I wrote the following query to return the the records with the latest date.
select fs.company_id, max(fs.create_dt) as latestcreatedate
from field_sale fs
group by fs.company_id
order by fs.company_id
The query all works fine. But I need to retrieve the record with all related columns attached to it. Such as, id, title, desc and etc.
How can I retrieve the records with its corresponding columns?
Couple ways of doing so :
-- 1.
SELECT a.*
FROM field_sale a
INNER JOIN
(
select fs.company_id, max(fs.create_dt) as latestcreatedate
from field_sale fs
group by fs.company_id
)b
ON b.company_id = a.company_id AND b.latestcreatedate = a.create_dt
order by a.company_id;
-- 2.
SELECT b.* FROM
(
SELECT a.* , ROW_NUMBER()
OVER (PARTITION BY a.company_id ORDER BY a.create_dt DESC)
AS rn
FROM field_sale a
)b WHERE b.rn = 1
ORDER BY company_id
WITH t AS (
SELECT fs.company_id,
fs.create_dt AS latestcreatedate,
id,
title,
etc,
ROW_NUMBER() OVER ( PARTITION BY fs.company_id ORDER BY fs.create_dt DESC ) AS rowNum
FROM field_sale fs
)
SELECT t.company_id,
t.latestcreatedate,
t.id,
t.title,
t.etc
FROM t
WHERE t.rowNum = 1
ORDER BY t.company_id