select unique row as per latest created_at time - mysql

this is my query:
SELECT br.employee_id AS rat_id,
br.rating_comment,
br.created_at,
br.id
FROM employee_followers
LEFT JOIN employee_ratings AS br ON employee_followers. employee_id = br.employee_id
WHERE employee_followers.employee_id IN
(SELECT `employee_id`
FROM employee_followers
WHERE user_id =32)
GROUP BY br.id
ORDER BY created_at DESC
what I am getting:
rat_id rating_comment created_at id
18 superb developer 2016-02-19 18:26:54 82
9 james to manoj-joshi 2016-02-19 16:42:17 84
50 james to prem 2016-02-19 13:05:30 83
50 pinal to prem 2016-02-19 12:20:00 73
50 prem to premal-joshi 2016-02-19 11:40:52 78
18 prem to mukund 2016-02-19 11:21:52 77
18 pinal to mukund 2016-02-18 11:37:00 76
9 mukund rate to mitesh 2016-02-15 22:34:14 51
9 Not a bad. 2016-02-10 00:16:31 45
9 sda 2016-02-08 01:36:33 39
what I want:
rat_id rating_comment created_at id
18 superb developer 2016-02-19 18:26:54 82
9 james to manoj-joshi 2016-02-19 16:42:17 84
50 james to prem 2016-02-19 13:05:30 83

Try This
SELECT rat_id,rating_comment, MAX(created_at) AS created, id FROM (
SELECT br.employee_id AS rat_id,
br.rating_comment,
br.created_at,
br.id
FROM employee_followers
LEFT JOIN employee_ratings AS br ON employee_followers. employee_id = br.employee_id
WHERE employee_followers.employee_id IN
(SELECT `employee_id`
FROM employee_followers
WHERE user_id =32)
GROUP BY br.id
ORDER BY created_at DESC) AS temp GROUP BY temp.rat_id

It seems that you want only the latest record per employee_id. You can use a derived table to get the maximum date, then join again to get all fields from employee_ratings table for this date:
SELECT er.employee_id AS rat_id,
er.rating_comment,
er.created_at,
er.id
FROM employee_followers AS ef
LEFT JOIN (
SELECT employee_id, MAX(created_at) AS max_date
FROM employee_ratings
GROUP BY employee_id
) AS emp_max ON ef.employee_id = emp_max.employee_id
LEFT JOIN employee_ratings AS er
ON emp_max.employee_id = er.employee_id AND
emp_max.max_date = er.max_date
WHERE user_id = 32
ORDER BY created_at DESC
I have also removed GROUP BY br.id that seems to be redundant in the context of the query. Also IN operator can be replaced by user_id = 32 since you are selecting from the same table.

Related

Need to solve a MYSQL select query

SELECT
points.location_id,
route_locations.route_id
FROM
points
LEFT JOIN route_locations ON route_locations.location_id = points.location_id
WHERE
points.id = 199 OR points.id = 205
after this query I am getting this result..
route_id location_id
12 69
12 75
14 75
now I need the common value for location_id 69 and location_id 75.. ( here route_id 12)
How can I get that by Query..
You can try below query -
SELECT
route_locations.route_id
FROM points
JOIN route_locations ON route_locations.location_id = points.location_id
WHERE
points.id in (199,205)
group by route_locations.route_id
having count(points.location_id)=2

SQL GROUP BY with two tables involved

Could you guys help me to make SELECT query for my simple case:
Table A:
UserID UserName
10 John
11 Mike
12 Matt
Table B:
SessionID UserID SessionTime
124 10 20
123 10 122
42 10 30
324 11 55
534 11 42
344 12 19
653 12 32
I need this result:
Result Table:
UserName UserID TotalTime
John 10 172
Mike 11 97
Matt 12 51
For one Table B this works:
SELECT UserID, SUM(SessionTime) AS "Total Time"
FROM TableB
GROUP BY UserID;
but I need to attach UserName to the final result
thank you
You can do that by using join and group by:
select a.UserId, a.UserName, sum(b.SessionTime) as TotalTime
from tableA a
left join tableB b on a.UserId = b.UserId
group by a.UserId, a.UserName;
Note: This would work for 1-to-many relations as in your case.
SELECT TableA.Username, TableA.User_ID, SUM(SessionTime) INNER JOIN
TableB ON TableA.User_ID = TableB.User_ID GROUP BY TableA.Username,
TableA.User_ID
SELECT a.UserName as "UserName"
,a.UserID as "UserID"
,sum(b.SessionTime) as "TotalTime"
FROM a LEFT JOIN b
ON a.UserID = b.UserID GROUP BY a.UserID
Here. I used TABLE a and Table b

rank on mysql with join

i need to display rank with my sql on 2 joined table, there's my mysql query to display
SELECT a.UserID, b.Nama,a.Matematika,a.IPA,a.IPS,a.BIND,a.BING,a.Rata,
FIND_IN_SET( a.Rata, (SELECT GROUP_CONCAT( a.Rata ORDER BY a.Rata DESC ) FROM datanilaiujian )) AS rank
from datanilaiujian as a JOIN
datauser as b
ON a.UserID=b.UserID
ORDER BY a.Rata DESC
but when i execute this command, mysql return Error Code: 1242. Subquery returns more than 1 row
list field on table datanilaiujian
UserID Matematika IPA IPS BIND BING Rata
1000 90 76 78.9 78 65 77.58
1001 78.9 87 67 56 78 73.38
1002 80 78.9 67 55 65.9 69.36
1003 78.9 56 77 88 90 77.97999999999999
list field on table datauser
UserID Pass Nama Alamat NoTelepon AsalSekolah Tanggal Masuk NilaiUN
1000 1000 Habib Jl.sesama 232323232323 23dsdsdsfsdfsdfsdfsdff 2017-01-13 19:35:22 Sudah
1001 1001 wisnu jl sesama 085600336706 SMA 2 Purwokerto 2017-01-28 17:35:32 Sudah
1002 1002 Arif Jl Sungkio 085600336706 SMA BINTEK 2017-01-28 19:30:56 Sudah
1003 1003 Akbar Jl sesama 085600133558 SMPN 1 Purwokerto 2017-02-02 18:59:47 Sudah
my expected result :
Nama Matematika IPA IPS BIND BING Rata Rank
I see the problem. Your subquery is saying a.Rata. This refers to the outer table. I suspect that the GROUP_CONCAT() is then confusing MySQL, so the subquery is not interpreted as an aggregation query.
However, you should move the subquery to the FROM clause:
SELECT a.*,
FIND_IN_SET( a.Rata, l.list) AS rank
from datanilaiujian a JOIN
datauser b
ON a.UserID = b.UserID CROSS JOIN
(SELECT GROUP_CONCAT( a2.Rata ORDER BY a2.Rata DESC ) as list
FROM datanilaiujian a2
) l
ORDER BY a.Rata DESC;
This generally helps the optimizer choose the best execution plan.

sql grouping finding average

i am looking to find the avg cost for the total cost of each order but my grouping function is invalid
SELECT AVG(SUM(quantityOrdered*priceEach)) AS total
FROM orderdetails od
GROUP BY orderNumber
below is a snippet of my database
orderNumber productCode quantityOrdered priceEach orderLineNumber
10100 S24_3969 49 35.29 1
10101 S18_2325 25 108.06 4
10101 S18_2795 26 167.06 1
10101 S24_1937 45 32.53 3
10101 S24_2022 46 44.35 2
10102 S18_1342 39 95.55 2
10102 S18_1367 41 43.13 1
10103 S10_1949 26 214.3 11
10103 S10_4962 42 119.67 4
10103 S12_1666 27 121.64 8
10103 S18_1097 35 94.5 10
10103 S18_2432 22 58.34 2
10103 S18_2949 27 92.19 12
10103 S18_2957 35 61.84 14
10103 S18_3136 25 86.92 13
10103 S18_3320 46 86.31 16
It's invalid use cannot use two aggregate functions together
SELECT SUM(quantityOrdered*priceEach) AS total
FROM orderdetails od
GROUP BY orderNumber
Having SUM(quantityOrdered*priceEach)>(SELECT AVG(quantityOrdered*priceEach) FROM orderdetails)
Just calculate the average using sum() divided by a number:
SELECT SUM(quantityOrdered*priceEach) / COUNT(DISTINCT orderNumber) AS total
FROM orderdetails od ;
You do it in two steps.
SQL Fiddle Demo
Calculate the Total of each order.
SELECT orderNumber, SUM(quantityOrdered*priceEach) AS total
FROM orderdetails od
GROUP BY orderNumber
Calculate the Average between all the totals
SELECT AVG(total)
FROM ( SELECT orderNumber, SUM(quantityOrdered*priceEach) AS total
FROM orderdetails od
GROUP BY orderNumber
) T
EDIT: I miss the last step after checking Ritesh answer.
SELECT o.*, t.global_avg
FROM (SELECT orderNumber, SUM(quantityOrdered*priceEach) AS order_total
FROM orderdetails od
GROUP BY orderNumber) o
CROSS JOIN
(SELECT AVG(total) global_avg
FROM ( SELECT orderNumber, SUM(quantityOrdered*priceEach) AS total
FROM orderdetails od
GROUP BY orderNumber
) t
) t
WHERE o.order_total > t.global_avg;
OUTPUT:

MySQL Query suggestion with access id

I have a MySQL table like this
ownerlisting_access_id property_id mainaccess_id subaccess_id access_value
62 2 35 41 Yes
64 2 35 36 Yes
123 4 35 41 Yes
125 4 35 36 Yes
306 7 35 41 Yes
307 7 35 42 Yes
308 7 35 36 Yes
I want a query that will give me this output using subaccess_id(41,42,36) and mainaccess_id(35) -
ownerlisting_access_id property_id mainaccess_id subaccess_id access_value
306 7 35 41 Yes
307 7 35 42 Yes
308 7 35 36 Yes
I need to get the property_id as 7 using sub access id with 41, 42, 36
The fastest way to get an answer to your question is to describe the PROBLEM not just show results you need. It's not clear what is the logic behind your desired output. I guess you need the rows with the highest property_id for each group subaccess_id. If so here is the query:
select * from t
join (select subaccess_id, max(property_id) MAX_property_id
from t
where mainaccess_id=35
and
subaccess_id in (41,42,36)
group by subaccess_id
) t1
on t.subaccess_id=t1.subaccess_id
and
t.property_id=t1.MAX_property_id
SQLFiddle demo
Also here is a query that outputs results you needed :) But I guess it doesn't solve your PROBLEM:
select * from t where property_id=7
Try this:
SELECT table1.* FROM (
select property_id, group_concat(DISTINCT subaccess_id ORDER BY subaccess_id) as list
from table1 as t1 group by property_id
) a, table1
WHERE a.property_id = table1.property_id
AND a.list = '36,41,42'
Working query: http://sqlfiddle.com/#!2/4744ea/2
SELECT MAX(DISTINCT property_ID) AS property_ID, mainaccess_id,
MAX(DISTINCT subaccess_id) AS subaccess_id, MAX(DISTINCT access_value)
FROM tableName GROUP BY mainaccess_id ORDER BY mainaccess_id
SELECT property_ID, mainaccess_id, subaccess_id, access_value
FROM tableName t1
WHERE t1.mainaccess_id = 35
AND (t1.subaccess_id = 41 OR t1.subaccess_id = 42 OR t1.subaccess_id = 36)
AND t1.property_ID = (SELECT MAX(t2.property_ID)
FROM tableName t2
WHERE t2.mainaccess_id = 35
AND (t2.subaccess_id = 41 OR t2.subaccess_id = 42 OR t2.subaccess_id = 36))