How can I use an Alias in a Mysql Calculation - mysql

I created the alias NumDrums from the count operation. I would like to use this alias again to create another alias UnitTotal, a calculated column. When running the query I get the following error:
Error executing query:
#42S22Reference 'NumDrums' not supported (reference to group function)
Select
customers.CustCompanyName,
products.ProdUnNum,
customers.CustGenerator,
products.ProdID,
products.ProdSize,
products.ProdUnitName,
COUNT(*) as NumDrums,
(SELECT NumDrums) * products.ProdSize as UnitTotal
From
drums Inner Join
orders On drums.OrderID = orders.OrderID Inner Join
manifests On orders.ManifestID = manifests.ManifestID Inner Join
customers On drums.CustID = customers.CustID
And orders.CustID = customers.CustID Inner Join
products On drums.ProdID = products.ProdID
Group By
customers.CustCompanyName,products.ProdUnNum
Order by
customers.CustCompanyName

Just use COUNT(*) * products.ProdSize, thanks to user Thallius

Related

SQL - Where have i gone wrong

QUESTION: I need to display the customers name and the total cost of the products they have ordered when they exceed over £10.
I cannot tell if it is because I am trying to use an inner join for three tables?
My attempt:
SELECT
DISTINCT Customer.custName,
SUM(Orders.quantity * Product.cost)
FROM
Customer,
Product
INNER JOIN Orders ON Product.productNo = Orders.productNo
WHERE
(Product.productNo = Orders.productNo)
AND (Orders.quantity * Product.cost > 10);
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. Plus, use GROUP BY. And your JOIN conditions are not complete. So:
SELECT c.custName, SUM(o.quantity * p.cost)
FROM Customer c JOIN
Orders o
ON o.customerNo = c.customerNo JOIN
Product p
ON p.productNo = o.productNo
GROUP BY c.custName
HAVING SUM(o.quantity * p.cost) > 10;

How to make Group By With multi inner join

i try to make filttering for selected data using inner join
i have 3 tables i should join it
this is my query
SELECT DISTINCT *
FROM products_products
INNER JOIN products_products_translations on products_products.id = products_products_translations.entry_id
INNER JOIN products_products_related on products_products.id = products_products_related.entry_id
INNER JOIN upload_files on products_products_related.related_id = upload_files.related_id
WHERE products_products_translations.locale = 'tr'
GROUP BY products_products_related.entry_id;
And this is the error that i got
Query 1 ERROR: Expression #35 of SELECT list is not in GROUP BY clause and contains nonaggregated column
'shop.products_products_translations.id' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by
Remove the distinct, and add products_products_related.entry_id, COUNT(*)
SELECT products_products_related.entry_id, COUNT(*)
FROM products_products
INNER JOIN products_products_translations on products_products.id = products_products_translations.entry_id
INNER JOIN products_products_related on products_products.id = products_products_related.entry_id
INNER JOIN upload_files on products_products_related.related_id = upload_files.related_id
WHERE products_products_translations.locale = 'tr'
GROUP BY products_products_related.entry_id;
You can't directly select the columns that are not coming after the group by clause.
The reason is when you group by any column, entry_id for example, you'll have single row for each entry_id. But, each entry_id has many rows in the original table with many different values in the other columns.
Therefore, you need to tell the query how to aggregate the columns which are not the columns you decided to group by.
You can aggregate them by COUNT(...) or SUM(...), or many other functions.
The solution would be something like the following:
SELECT products_products_related.entry_id, MAX(Column1), MIN(Column2) /*etc...*/
FROM products_products
INNER JOIN products_products_translations on products_products.id = products_products_translations.entry_id
INNER JOIN products_products_related on products_products.id = products_products_related.entry_id
INNER JOIN upload_files on products_products_related.related_id = upload_files.related_id
WHERE products_products_translations.locale = 'tr'
GROUP BY products_products_related.entry_id;

How can I grab all rows from one table, but filter the second table?

I am running a query on a PHP page that will pull all records from one table, INNER JOIN with two other tables and then list all of the results. However on the second table I only want the most recent record.
Here is my query
SELECT * FROM wn_trailer
INNER JOIN (
SELECT id, trailer_id, trailer_status, trailer_assigned, MAX(last_update), trailer_lat, trailer_long
FROM wn_trailer_history
) AS th ON wn_trailer.id = th.trailer_id
INNER JOIN wn_trailer_status ON wn_trailer_status.id = th.trailer_status
INNER JOIN wn_users ON wn_users.id = th.trailer_assigned
ORDER BY trailer_number ASC
The query runs but returns only the first record.
You want an additional JOIN to bring in the data on the last update date. Also, your subquery needs a GROUP BY:
SELECT *
FROM wn_trailer t INNER JOIN
(SELECT trailer_id, MAX(last_update) as max_last_update
FROM wn_trailer_history
GROUP BY trailer_id
) tht
ON t.id = tht.trailer_id INNER JOIN
wn_trailer_history th
ON th.trailer_id = tht.trailer_id AND
th.last_update = tht.max_last_update INNER JOIN
wn_trailer_status ts
ON ts.id = th.trailer_status INNER JOIN
wn_users u
ON u.id = th.trailer_assigned
ORDER BY trailer_number ASC;
I also added table aliases so the query is easier to write and to read.

How to access parent column from a subquery within a join

I'm trying to left join the second table useri_ban based on the users' ids, with the extra condition: useri_ban.start_ban = max_start.
In order for me to calculate max_start, I have to run the following subquery:
(SELECT MAX(ub.start_ban) AS max_start, user_id FROM useri_ban ub WHERE ub.user_id = useri.id)
Furthermore, in order to add max_start to every row, I need to inner join this subquery's result into the main result. However, it seems that once I apply that join, the subquery is no longer able to access useri.id.
What am I doing wrong?
SELECT
useri.id as id,
useri.email as email,
useri_ban.warning_type_id as warning_type_id,
useri_ban.type as type,
useri.created_at AS created_at
FROM `useri`
inner join
(SELECT MAX(ub.start_ban) AS max_start, user_id FROM useri_ban ub WHERE ub.user_id = useri.id) `temp`
on `useri`.`id` = `temp`.`user_id`
left join `useri_ban` on `useri_ban`.`user_id` = `useri`.`id` and `useri_ban`.`start_ban` = `max_start`
Does this solve your problem? You need GROUP BY in the inner query instead of another join.
SELECT useri.id, useri.email, maxQuery.maxStartBan
FROM useri
INNER JOIN
(
SELECT useri_ban.user_id ubid, MAX(useri_ban.startban) maxStartBan
FROM useri_ban
GROUP BY useri_ban.user_id
) AS maxQuery
ON maxQuery.ubid = useri.id;

How Can I use combination of inner join and order by

How Can I use combination of inner join and order by.
My query is running but my output is not giving me the correct answer, im having problem with this line, how can I sort 1 column or the column cm_status only:
INNER JOIN dbfeedersystem.tblrepair on
tblpm.control_number_pm=tblrepair.control_number
SELECT tblfeeder.control_number AS Control_No,tblfeeder.serial_number AS
Serial_No,tblfeeder.model AS Model,
DATE_FORMAT(date_updated_pm,'%d-%b-%Y') AS PM_Date,
DATE_FORMAT(DATE_SUB(due_date,INTERVAL 1 WEEK),'%d-%b-%Y') AS Issuance_of_Recall,
DATE_FORMAT(due_date,'%d-%b-%Y') AS Due_Date, pm_status, cm_status
FROM dbfeedersystem.tblpm INNER JOIN dbfeedersystem.tblrepair on
tblpm.control_number_pm=tblrepair.control_number
INNER JOIN
dbfeedersystem.tblfeeder on
tblpm.control_number_pm=tblfeeder.control_number
INNER JOIN
(SELECT control_number_pm,max(date_updated_pm) AS date_updated_pm FROM
dbfeedersystem.tblpm GROUP BY control_number_pm)
AS MAX USING (control_number_pm,date_updated_pm)
ORDER BY date_updated_pm,control_number_pm;