MySQL nested select statement issues - mysql

I'm struggling to get my nested SQL statement to work.
It tells me I have a syntax error near the second SELECT.
Can anyone advise?
SELECT date, Expense_Date, cost1
RIGHT JOIN t ON dates.date = t.Expense_Date
FROM (
SELECT Expense_Date, IFNULL(Sum(Total_Cost),0) as cost1
FROM Expenses
RIGHT JOIN membership_userrecords ON Expenses.id = membership_userrecords.pkValue
where membership_userrecords.memberID = 'kieran' AND membership_userrecords.tableName='Expenses'
GROUP BY Expense_Date
) AS t
GROUP BY date ;

Forget about second SELECT ... You are missing a FROM clause in your first SELECT itself as seen below
SELECT date, Expense_Date, cost1 <-- here
RIGHT JOIN t ON dates.date = t.Expense_Date

Try this
SELECT date, Expense_Date, cost1
FROM (
SELECT Expense_Date, IFNULL(Sum(Total_Cost),0) as cost1
FROM Expenses
RIGHT JOIN membership_userrecords ON Expenses.id = membership_userrecords.pkValue
where membership_userrecords.memberID = 'kieran' AND membership_userrecords.tableName='Expenses'
GROUP BY Expense_Date
) AS t
LEFT JOIN t ON dates.date = t.Expense_Date
GROUP BY date ;

Related

Mysql Select query with double JOIN statement

Hy guys. I need to select the customer_id who is on 'base' table, where the created date is the current date and where this customer dont have a budget_item executed on the current date. My query brings up an incorrect result. It show the customer_id of all budget_id that are not on that date..
What is wrong in my stetement?
PS.: I cannot consolidate the tables.
SELECT base.customer_id
FROM base
LEFT JOIN budget ON base.customer_id = budget.customer_id
LEFT JOIN budget_item ON budget.budget_id = budget_item.budget_id
WHERE
CAST(base.created as Date) = CURDATE()
AND budget_item.execution_date <> CURDATE();
Left join budget/budget_item specifically looking for items with the current date and then exclude them in WHERE by checking that some non-nullable column of budget_item is NULL (indicating no record was joined):
SELECT base.customer_id
FROM base
LEFT JOIN budget ON base.customer_id = budget.customer_id
LEFT JOIN budget_item ON budget.budget_id = budget_item.budget_id AND budget_item.execution_date = CURDATE()
WHERE
CAST(base.created as Date) = CURDATE()
AND budget_item.budget_id IS NULL;
Some prefer using NOT EXISTS for this, but the result and efficiency should be the same:
SELECT base.customer_id
FROM base
WHERE
CAST(base.created as Date) = CURDATE()
AND NOT EXISTS (
SELECT 1
FROM budget
JOIN budget_item ON budget.budget_id = budget_item.budget_id AND budget_item.execution_date = CURDATE()
WHERE budget.customer_id = base.customer_id
);
fiddle
Use aggregation:
SELECT base.customer_id
FROM base LEFT JOIN
budget bu
ON base.customer_id = bu.customer_id LEFT JOIN
budget_item bi
ON bu.budget_id = bi.budget_id
WHERE CAST(base.created as Date) = CURDATE()
GROUP BY base.customer_id
HAVING SUM(bi.execution_date = CURDATE()) = 0;

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;

Return count 0 with mysql group by and where by date

I want to create a single query to result data like this:
but when I insert where clause, column name just result a few value from column m_ccode:
Q: how keep all name column values if I insert where clause?
Here the working fiddle:
http://sqlfiddle.com/#!9/687776/10
use MONTH(t.date) = '10' AND YEAR(t.date) = '2018' condition in on cluase like below
demo
SELECT ccode.name,IFNULL(count(DISTINCT u.username),0) as total_active
,(count(DISTINCT u.username) - count(DISTINCT t.username)) as total_non_active
from m_ccode as ccode
left join m_user as u on u.ccode = ccode.id
left join t_safety_act t on t.username = u.username
and MONTH(t.date) = '10' AND YEAR(t.date) = '2018' group by ccode.id;

Mysql Select unique record based on multiple columns and display only group and sum amount

Hi I am trying to query a table that conatains multiple duplicates on Code,Amount and Status How will I do this if I only one to get a result group according to the client_group name and get the sum of amount under that group
SELECT `client`.`client_group`
, FORMAT(SUM(`Data_result`.`Data_result_amount` ),2) as sum
FROM
`qwer`.`Data_result`
INNER JOIN `qwer`.`Data`
ON (`Data_result`.`Data_result_lead` = `Data`.`Data_id`)
INNER JOIN `qwer`.`Data_status`
ON (`Data_result`.`Data_result_status_id` = `Data_status`.`Data_status_id`)
INNER JOIN `qwer`.`client`
ON (`Data`.`Data_client_id` = `client`.`client_id`)
WHERE `Data_status`.`Data_status_name` IN ('PAID') AND MONTH(`Data_result`.`result_ts`) = MONTH(CURRENT_DATE())
AND YEAR(`Data_result`.`result_ts`) = YEAR(CURRENT_DATE())
GROUP BY `client`.`client_group`
Result of said query:
Table
Try to distinct before run the 'sum' check whether this solve your problem
SELECT `client_group` , FORMAT(SUM(`Data_result_amount` ),2) as sum from (
SELECT DISTINCT `client`.`client_group` , `Data_result`.`Data_result_amount`
FROM
`qwer`.`Data_result`
INNER JOIN `qwer`.`Data`
ON (`Data_result`.`Data_result_lead` = `Data`.`Data_id`)
INNER JOIN `qwer`.`Data_status`
ON (`Data_result`.`Data_result_status_id` = `Data_status`.`Data_status_id`)
INNER JOIN `qwer`.`client`
ON (`Data`.`Data_client_id` = `client`.`client_id`)
WHERE `Data_status`.`Data_status_name` IN ('PAID') AND MONTH(`Data_result`.`result_ts`) = MONTH(CURRENT_DATE())
AND YEAR(`Data_result`.`result_ts`) = YEAR(CURRENT_DATE())
) T
GROUP BY `client_group`
you can check the query here http://sqlfiddle.com/#!9/36a3f8/6

MySQL throwing error on second JOIN

I'm getting the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN product_catalog ON product_catalog.entity_id
As a result of the following query:
SELECT sales_order.created_at , order_item.order_id, sales_order.increment_id, SUM(order_item.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item
JOIN sales_order
ON sales_order.entity_id = order_item.order_id
WHERE sales_order.created_at > '2012-11-15 00:00:00'
JOIN product_catalog
ON product_catalog.entity_id = order_item.product_id
WHERE product_catalog.size = 14
GROUP BY order_item.order_id;
Variations on this query have worked for grouping different types of product by sales order in the past where I only needed to perform one JOIN to get all the info I needed. The problem I'm encountering is from the second JOIN. Clearly I'm missing something but I really am not sure what. :(
Please make sure that WHERE condition must be after all JOIN
SELECT sales_order.created_at , order_item.order_id, sales_order.increment_id, SUM(order_item.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item
JOIN sales_order
ON sales_order.entity_id = order_item.order_id
JOIN product_catalog
ON product_catalog.entity_id = order_item.product_id
WHERE product_catalog.size = 14
AND sales_order.created_at > '2012-11-15 00:00:00'
GROUP BY order_item.order_id;
First of all you have to JOIN your tables which you need. Then after WHERE clause come for conditions.
Your WHERE clauses are in the wrong spots. See the code below for proper JOIN syntax.
SELECT sales_order.created_at,
order_item.order_id,
sales_order.increment_id,
SUM(order_item.qty_ordered) AS qty_ordered,
COUNT( * )
FROM order_item
JOIN sales_order
ON sales_order.entity_id = order_item.order_id
AND sales_order.created_at > '2012-11-15 00:00:00'
JOIN product_catalog
ON product_catalog.entity_id = order_item.product_id
AND product_catalog.size = 14
GROUP BY order_item.order_id
JOIN...ON... clause it's also section to input condition so you don't need where clause, just add AND instead.
SELECT sales_order.created_at , order_item.order_id, sales_order.increment_id,
SUM(order_item.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item
JOIN sales_order ON sales_order.entity_id = order_item.order_id
and sales_order.created_at > '2012-11-15 00:00:00'
JOIN product_catalog ON product_catalog.entity_id = order_item.product_id
and product_catalog.size = 14
GROUP BY order_item.order_id;
Please consider below example I added aliases. It's good practice to use it because code is more readable.
SELECT SO.created_at , OI.order_id, SO.increment_id,
SUM(OI.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item OI
JOIN sales_order SO ON SO.entity_id = OI.order_id
and SO.created_at > '2012-11-15 00:00:00'
JOIN product_catalog PC ON PC.entity_id = OI.product_id
and PS.size = 14
GROUP BY OI.order_id;