This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 5 years ago.
I am joining three tables and I am trying to show the latest date but the dates that are being returned are not the latest.
SELECT c.id, c.companyname,c.displayname, c.address1, c.address2,
c.town, c.postcode,cd.contactname cd.contactphone, cd.contactemail,
MAX(q.coldate)
FROM companies c, query q, custd cd
WHERE c.id = q.id AND q.id = cd.compid
AND year(q.coldate) >= 2016
GROUP BY q.companyname;
How can I fix this please?
if you want to display the max(date) in your table (and therefore the same date) for every row of your result, try this before you run the query:
select cast(max(coldate) as char) from schema_name.table_name into
#max_coldate_for_query ;
when running your query change coldate to #max_coldate_for_query in the list of columns you are selecting
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
MySQL INNER JOIN select only one row from second table
(12 answers)
Closed 2 years ago.
I am trying to create an aggregate query of Gravity Forms submissions. My client has 8 different forms, each with 5 questions.
A user will fill out the same forms multiple times over the course of time.
My challenge is to to only query the latest submissions for each form_id and the underlying data for a given user.
So far, I have this:
SELECT e.id as entry_id, e.form_id as form_id, e.date_created as date_created, m.meta_key as meta_key, m.meta_value as meta_value
FROM wplh_gf_entry e
JOIN wplh_gf_entry_meta m on e.id = m.entry_id
WHERE e.created_by = 5 AND meta_key BETWEEN 1 AND 5 ( meta_key in this case hold the id of a given question for each form)
However, the query above does nothing to limit the results to only the latest entries per form_id.
If I put a max(e.date_created) and GROUP BY e.form_id then I only get the first entry for each form out of the meta table.
The relevant fields in each table are as follows:
wplh_gf_entry (id, form_id, date_created, created_by)
wplh_gf_entry_meta(form_id, entry_id, meta_key, meta_vaue)
I'm not quite sure what I need here. I tried a few different subqueries as well but couldn't get any workable results.
Thank you in advance.
This question already has answers here:
Group by minimum value in one field while selecting distinct rows
(10 answers)
MySQL - Select group by with minimum value [duplicate]
(1 answer)
Closed 2 years ago.
Help!
How do I find the first sku on the first order for a customer? Assume I have a table with the following columns and am using MySQL:
order_number
email
sku
For example, I know we can do the following to find the first order number:
select email, min(order_number)
from orders
group by email
If I run the following query, it returns a seemingly random sku from the customers order history:
select email, min(order_number), sku
from orders
group by email
I tried running a subquery, but no luck there either:
select email, min(order_number),
(select sku from orders o2
where o2.email = o1.email
and o2.order_number = o1.order_number)
from orders o1
group by email
How can I do this?
Thanks!
This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 3 years ago.
I'm coding a message system using mysql.
Everything works fine when I list users whom I'm conversing with, until I want to add date of the last or the start of conversation.
When I add a.date I get duplicate results when the date isnt the same.
Here is my sqlfiddle
Since, you were pulling only user_id then in both cases (send/recieve) it was giving you distinct record. But now with date it is no more distinct. you need to do something like:
SELECT temp.id_user, MAX(temp.date) as date
FROM
(
SELECT users.id_user,
a.date
FROM users
LEFT JOIN message AS a
ON users.id_user = a.id_user_recipient
LEFT JOIN message AS b
ON a.id_user_recipient = b.id_user_sender
WHERE a.id_user_sender = 1
UNION DISTINCT
SELECT users.id_user,
a.date
FROM users
LEFT JOIN message AS a
ON users.id_user = a.id_user_sender
LEFT JOIN message AS b
ON a.id_user_sender = b.id_user_recipient
WHERE a.id_user_recipient = 1
) as temp
GROUP BY temp.id_user;
Grabbing max(date) will ensure to return only one record as with group by
This question already has answers here:
Can I reuse a calculated field in a SELECT query?
(7 answers)
Closed 3 years ago.
I want to know how can i use two columns already computed in sql query to get one more result from their values.
my query is
SELECT s.date date
, p.id
, SUM(COALESCE(p.avgcost,0)) costofsale
, SUM( COALESCE(s.actual_payable, 0 ) ) total_sales
, (total_sales - costofsale) tots
FROM sales s
LEFT
JOIN sale_items si
on si.sale_id = s.id
LEFT
JOIN products p
on p.id = si.product_id
WHERE DATE(s.date) = DATE('2019-09-10 14:48:50')
I want to get the result from total_sales - costofsale that is already computed in query.
I don't want to alter or update my table.
I just need to use those two columns to give me the result by calculating them.
I have not found any solution over google.
MySQL 8 includes WITH statements which allows for creating calculated values without using variables.
WITH statement VS subquery
This question already has answers here:
Ordering a MySQL result set by a MAX() value of another table
(2 answers)
Closed 4 years ago.
I have two tables - groups and messages.
Messages has the following fields group_id and date_created. So a lot of messages can be added to a single group. I want to select all groups from table - most relevant on top, i.e. order by latest message date. I've tried something like this
SELECT g.*, MAX(m.date_created) AS mdt FROM groups g
LEFT JOIN messages m ON g.id = m.group_id
ORDER BY mdt DESC;
But this query returns only one row and max message date from the whole table.
You are missing group by:
SELECT g.*, MAX(m.date_created) AS mdt
FROM groups g LEFT JOIN
messages m
ON g.id = m.group_id
GROUP BY g.id
ORDER BY mdt DESC;