Why does my SQL query give this error? - mysql

I can't figure out why I keep getting error #1054 - Unknown column 'Filename' in 'field list'
SELECT 'R' AS Type, er.ID, er.RecipeID AS RecipeID,
RecipeName AS Name,er.Quantity,UnitID,
SUBSTRING_INDEX(PFCC,',',1) AS Protein,
SUBSTRING_INDEX(SUBSTRING_INDEX(PFCC,',',2),',',-1) AS Fat,
SUBSTRING_INDEX(SUBSTRING_INDEX(PFCC,',',-2),',',1) AS Carbs,
SUBSTRING_INDEX(PFCC,',',-1) AS Calories,
Rating, AvgRating, AvgCount, 0 AS SpecificID,
COALESCE(rimg.Filename,'0.png') AS DefaultImage,
s.Price, s.MaxQuantity
FROM `event-recipe` er
LEFT JOIN(
SELECT r.ID,RecipeName,
SUM(Quantity) AS Quantity,
IFNULL(Rating,0) AS Rating
FROM recipe r
LEFT JOIN(
SELECT RecipeID, Filename
FROM recipe_image
WHERE IsDefault) rimg
ON r.ID=rimg.RecipeID
LEFT JOIN `recipe-ingredient` ri
ON r.ID=ri.RecipeID
LEFT JOIN(
SELECT RecipeID,Rating
FROM `recipe-rating`
WHERE CustomerID=2) rr
on r.ID=rr.RecipeID
GROUP BY r.ID) r ON er.RecipeID=r.ID
LEFT JOIN(
SELECT RecipeID, GROUP_CONCAT(Value ORDER BY Nutr_No) AS PFCC
FROM nutrient_view_ingredient nv
WHERE Nutr_No IN (203,204,205,208)
GROUP BY RecipeID) n
ON r.ID=n.RecipeID
LEFT JOIN sell_recipe s ON (er.EventID=s.EventID AND s.RecipeID=er.RecipeID)
LEFT JOIN(
SELECT RecipeID, COUNT(RecipeID) AS AvgCount, ROUND(AVG(Rating),1) AS AvgRating
FROM `recipe-rating`
GROUP BY RecipeID) avgrate
ON r.ID=avgrate.RecipeID
WHERE er.EventID=53

The issue is in how you've used your parentheses to group your table, as well as the fact that you never select filename in the inner join.
The first big join, which you've named r is the table that has reference to filename. No where in that table do you select rimg.fileName so in the outer query, you can't even call r.fileName. To fix it, change the line:
SELECT r.ID, RecipeName
To:
SELECT r.ID, RecipeName, rimg.FileName
Then, in your outer query, you can select r.FileName.

Related

Convert Subquery with ORDER BY in 'ON' clause to Join for optimization

I have this query:
SELECT prod.ProductID, prod.Name, prod.ProdExtID, ls.ProdServiceID
FROM Products prod
LEFT JOIN ProductServices ls ON ls.ProdServiceID=(SELECT ProdServiceID FROM
ProductServices WHERE ProductID=prod.ProductID ORDER BY Modified DESC LIMIT
1) ;
This query returns 175 rows
I want to convert this to JOIN.
I used below query:
SELECT prod.ProductID, prod.Name, prod.ProdExtID, ls1.ProductServicesID
FROM Products prod
inner join ProductServices ls on ls.ProductID=prod.ProductID
inner JOIN (SELECT ProductServicesID, ProductID, max(Modified) as Modified
FROM
ProductServices group by Modified) as ls1 ON ls.ProductServicesID =
ls1.ProductServicesID and ls.Modified = ls1.Modified and ls.ProductID =
ls1.ProductID;
which doesn't return correct result. Can I get some direction on this.
The purpose is to optimize the query. Would it be good idea to use join in place of original query.
Thanks!
Try this:
SELECT ProductID
, Name
, ProdExtID
, ProdServiceID
FROM
(
SELECT prod.ProductID
, prod.Name
, prod.ProdExtID
, ls.ProdServiceID
, RANK() OVER(PARTITION BY ls.ProdServiceID ORDER BY ls.Modified DESC) AS rnk
FROM Products prod
LEFT JOIN ProductServices ls ON ls.ProductID=prod.ProductID
) x
WHERE rnk = 1
So basically: your join needs to be on ProductID but you only want the most recent ProdServiceID, right?
try this select query please.
Here you get the serviceid which corresponds to productid and the last modiied
SELECT prod.ProductID, prod.Name, prod.ProdExtID, ls.ProdServiceID
FROM Products prod
LEft join ProductServices ls on ls.ProductID=prod.ProductID
inner JOIN (SELECT ProductID, max(Modified) as Modified
FROM
ProductServices group by ProductID) as ls1 ON ls.ProductID =
ls1.ProductID and ls.Modified = ls1.Modified;

Combine union and sum with diffrent column of view

I have View here
What I want to try is to make the v_total_project looks like that.
I have try this code
select o.id_project AS id_project,
sum((o.office_expense + m.misc_expense)) AS total_expense
from v_office_project o inner join v_misc_project m on o.id_project = m.id_project
group by o.id_project, m.id_project
I know my code will not print the v_misc_project.id_project. So I tried the Union to print both id_project, and got error different numbers of column.
select o.id_project AS id_project
from v_office_project o
union
select m.id_project as id_project,
sum((o.office_expense + m.misc_expense)) AS total_expense
from v_office_project o inner join v_misc_project m on o.id_project = m.id_project
group by id_project
So, is there another way to do that?
Thanks in advance,
SELECT
o.id_project AS id_project,
sum(
o.office_expense + ifnull(m.misc_expense, 0)
) AS total_expense
FROM
v_office_project o
LEFT JOIN v_misc_project m ON o.id_project = m.id_project
GROUP BY
id_project
UNION
SELECT
m.id_project AS id_project,
sum(
ifnull(o.office_expense, 0) + m.misc_expense
) AS total_expense
FROM
v_office_project o
RIGHT JOIN v_misc_project m ON o.id_project = m.id_project
GROUP BY
id_project
In the first select of your union you are missing the field for your expenses.
Also the join was missing, added it. Instead of INNER JOIN use a LEFT or RIGHT Join, otherwise you only get results for id_project's which are in both tables.
Added ifnull() in the calculation, because the LEFT / RIGHT joins can return null, which has to be treated as 0.

use Aggregate function with multiple table in Access

I have Four tables Items,Customer,Invoice_summary,Invoice_details. Here I want to join these four tables and to get Sum(Invoice_details.Item_quntity) and Sum(Invoice_details.Price) for a specific item_code and for a specific date range . Main Columns are as follows:
Invoice_summary :Inv_num,Inv_date,Cus_id,Total
Items :Item_code,Item_name,Unit_price
Invoice_details :Inv_num,Item_code,Item_qty,Price
Customers :Cus_id,Cus_name,Route
Here is what I currently have.this return more than a row(whole itemsnames) i need only for a specific item code.Could someone explain where I am going wrong.
SELECT Invoice_Table.Item_Code, Items.Item_Name,
(Select sum(Invoice_Table.Item_Quntity) from (Invoice_Table INNER JOIN Invoice ON Invoice_Table.Inv_Num = Invoice.Inv_Num) where ((Invoice_Table.Item_Code=[?]) And Invoice.inv_date Between #3/4/2013# And #6/4/2013#) group BY Invoice_Table.Item_Code) AS Quntity,
(Select sum(Invoice_Table.Price) from (Invoice_Table INNER JOIN Invoice ON Invoice_Table.Inv_Num = Invoice.Inv_Num) where ((Invoice_Table.Item_Code=[?]) And Invoice.inv_date Between #3/4/2013# And #6/4/2013#) group BY Invoice_Table.Item_Code) AS Price
FROM Invoice_Table
INNER JOIN Items ON Invoice_Table.Item_Code = Items.Item_Code
GROUP BY Invoice_Table.Item_Code, Items.Item_Name;
The sums you're interested in should be given by this query.
select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
It looks like the date range should be found in the invoice summary table. It looks like Invoice_summary and Invoice_details should be joinable on Inv_num.
select inv_s.Inv_num, inv_s.Inv_date, inv_s.Cus_id, inv_s.Total,
inv_t.item_code_qty, inv_t.item_code_price
from Invoice_summary inv_s
inner join (select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
) inv_t
on inv_s.Inv_num = inv_t.Inv_num
where inv_s.Inv_date between ? and ?;
And to round that out, join the other two tables on their keys, and add some of their columns to the SELECT clause.
select inv_s.Inv_num, inv_s.Inv_date, inv_s.Cus_id, cus.Cus_name, inv_s.Total,
inv_t.item_code_qty, inv_t.item_code_price,
items.name
from Invoice_summary inv_s
inner join (select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
) inv_t
on inv_s.Inv_num = inv_t.Inv_num
inner join items on items.Item_code = inv_t.Item_code
inner join Customers cus on cus.Cus_id = inv_s.Cus_id
where inv_s.Inv_date between ? and ?;
Try this..
Select ID.Item_qty,ID.Price,I.Item_code from Invoice_summary IS
INNER JOIN Invoice_details ID ON ID.Inv_num=IS.Inv_num
INNER JOIN Customers C ON C.Cus_id=IS.Inv_num
INNER JOIN Items I ON I.Item_code=ID.Item_code

MySQL Lowercase Returned Value Or Entire Result

I have the below SQL query and it will return a group_name along with a list of departments for that group. I was able to lowercase the departments returned, but I can't figure out how to lowercase the group name as well.
Also, instead of lowercasing each returned column is there perhaps a way to lowercase the entire result in one swoop?
SELECT sg.group_name,A.dept_name
FROM `sys_groups` `sg`
INNER JOIN (SELECT gda.group_id,
GROUP_CONCAT(LOWER(sd.dept_name) ORDER BY `dept_name`
SEPARATOR '|'
) `dept_name`
FROM `group_dept_access` `gda`
INNER JOIN `sys_department` `sd`
ON gda.dept_id = sd.dept_id
GROUP BY gda.group_id) AS `A`
ON sg.group_id = A.group_id
Thank you in advance!
Try this:
SELECT LOWER(sg.group_name) group_name, LOWER(A.dept_name) dept_name
FROM sys_groups sg
INNER JOIN (SELECT gda.group_id,
GROUP_CONCAT(sd.dept_name ORDER BY dept_name SEPARATOR '|') dept_name
FROM group_dept_access gda
INNER JOIN sys_department sd ON gda.dept_id = sd.dept_id
GROUP BY gda.group_id
) AS A ON sg.group_id = A.group_id

How can I fix MySQL query to solve "Unknown column...in On clause" error?

Can anyone help fix this query? I tried just about everything I could think of with no luck.
I have the below MySQL query and it spits out the below error
ERROR:
Unknown column 'SR.sales_rec_id' in 'on clause'
QUERY:
SELECT
SYSKEY.cri_sr_id,
SR.sales_rec_id,
SYSKEY.adnum,
charge,
SYSKEY.cancelsale,
paymentdue,
merchant,
paymentnum,
authorization as authcode,
CONCAT(lastname,', ',firstname,' ',middleinitial) as customer,
CONCAT(replname,', ',repfname) as repname,
FROM CRI_SR_KEYS as SYSKEY
INNER JOIN SALES_PENDING as SPEND on SPEND.sales_rec_id=SR.sales_rec_id
INNER JOIN SALES_RECORD as SR on SR.cri_sr_id=SYSKEY.cri_sr_id
LEFT JOIN CLIENT_INFO as CI on CI.client_id = SYSKEY.clientid
LEFT JOIN SALES_REPS as REP on SR.rep_id=REP.repid
WHERE SPEND.authorization NOT LIKE '000000' AND paymentdue LIKE CURDATE()
ORDER BY adnum ASC LIMIT 0,5000
Rearrange the order of your joins so that SR is aliased before you try to use it:
SELECT
SYSKEY.cri_sr_id,SR.sales_rec_id,
SYSKEY.adnum,charge,
SYSKEY.cancelsale,paymentdue,merchant,paymentnum,authorization as authcode,
CONCAT(lastname,', ',firstname,' ',middleinitial) as customer,
CONCAT(replname,', ',repfname) as repname
FROM CRI_SR_KEYS as SYSKEY
INNER JOIN SALES_RECORD as SR on SR.cri_sr_id=SYSKEY.cri_sr_id
INNER JOIN SALES_PENDING as SPEND on SPEND.sales_rec_id=SR.sales_rec_id
LEFT JOIN CLIENT_INFO as CI on CI.client_id = SYSKEY.clientid
LEFT JOIN SALES_REPS as REP on SR.rep_id=REP.repid
WHERE SPEND.authorization NOT LIKE '000000' AND paymentdue LIKE CURDATE()
ORDER BY adnum ASC LIMIT 0,5000
There's also a stray comma after repname in the SELECT clause but that could just be a typo.
If the column SALES_RECORD.sales_rec_id does actually exist, reorder your JOIN statements:
FROM CRI_SR_KEYS as SYSKEY
/* Move this above SALES_PENDING to SR */
INNER JOIN SALES_RECORD as SR on SR.cri_sr_id=SYSKEY.cri_sr_id
INNER JOIN SALES_PENDING as SPEND on SPEND.sales_rec_id=SR.sales_rec_id
LEFT JOIN CLIENT_INFO as CI on CI.client_id = SYSKEY.clientid
LEFT JOIN SALES_REPS as REP on SR.rep_id=REP.repid