Gravity Forms Aggregate Query [duplicate] - mysql

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.

Related

mysql checking if product ID also exist in other table [duplicate]

This question already has answers here:
Select rows which are not present in other table
(4 answers)
Closed 2 years ago.
I have not much experience with JOINS and the result I get with query below isn't correct.
I have a table called products and want to check if there are records in the table product_links.
I only want to get a list of items that doesn't have rows in product_links.
When I run the below query, I only get one line.
Anybody suggestions? Google couldn't help me or I'm searching with the wrong keywords.
SELECT a.id, a.SKU, a.title,
(SELECT COUNT(b.id) AS amount FROM product_links WHERE b.product=a.id) AS amount
FROM products AS a
LEFT JOIN product_links AS b ON b.product=a.id
I would recommend not exists:
select p.*
from products p
where not exists (select 1 from product_links pl where pl.product_id = p.id)
From your question i understand you need info of products which doesnt have any links.
Below is the query for that
SELECT * FROM products
WHERE id NOT IN (SELECT id FROM product_links);

How do I find the sku on the first order for a customer? [duplicate]

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!

computed column to use further in mysql query [duplicate]

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

MAX(date) Not Returning Latest Date [duplicate]

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

Select rows with highest value of multiple columns [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL how to find rows which have highest value of specific column
Here is my table schema
--transaction_details--
transaction_id primary_key auto inc
entity_name
entity_transaction
Now, one entity can have multiple transactions. I want to query last transaction made for every entity, which is unlike this post that looks up a specific column. This can be done by selecting the rows with last/max transaction_id for each entity. But I am unable to write this query. I tried using group by entity_name but its selecting any random row.
You can do something like this, if entity_transaction is always the same for the same entity_name:
select max(transaction_id), entity_name, entity_transaction
from transaction_details
group by entity_name, entity_transaction
If you can have different values for entity_transaction for the same entity_name, use this query:
select g.transaction_id, g.entity_name, d.entity_transaction
from
(
select max(transaction_id) transaction_id, entity_name
from transaction_details
group by entity_name
) g
inner join transaction_details d
on g.entity_name = d.entity_name and g.transaction_id = d.transaction_id
try this:
select *
from <table> t
join
( select entity_name,max(transaction_id) as transaction_id
from <table>
group by entity_name)a
on a.entity_name=t.entity_name
and a.transaction_id=t.transaction_id