MySql query runs but it doesn't work in sp - mysql

Here is my sp query:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `USP_GetUserOrders`(UserId INT)
BEGIN
SELECT op.OrderId,
O.Number,
SUM(op.Price) Price,
(SELECT CONCAT(A.Detail, ' ',C.Name, ' / ', Ci.Name) FROM kobiakinlar.Address AS A
INNER JOIN County AS C ON C.CountyId = A.CountyId
INNER JOIN City AS Ci ON C.CityId = Ci.CityId
WHERE UserId = O.UserId) AS UserAddress,
( SELECT CASE WHEN O.Status =0 THEN 'Onay Bekliyor' WHEN O.Status =1 THEN 'Onaylandı' WHEN O.Status = 2 THEN 'Reddedildi' END) Status,
O.Creation,
( SELECT CASE WHEN O.IsDelivered =0 THEN 'Teslim Edilmedi' ELSE 'Teslim Edildi' END) IsDelivered,
group_concat(P.Name) Product
FROM
kobiakinlar.product P
JOIN
kobiakinlar.orderproduct op ON op.ProductId = P.productId
JOIN
kobiakinlar.order O ON O.orderId = op.OrderId
JOIN
kobiakinlar.address A ON A.addressId = O.AddressId
WHERE O.UserId = UserId
GROUP BY op.OrderId;
END
It returns Error Code: 1242. Subquery returns more than 1 row when I CALL USP_GetUserOrders(3)
But I run only sql in query tab, it runs and return what I want. You can see query's result in image:
Do you have any suggestion?

I'm pretty sure the reason is the confusion between UserId and o.UserId.
In the query context, it does not know that you mean the argument to the sp. Change the name of the arguemnt to something like "arg_UserId" and substitute that in the query where appropriate.
You can also simplify your query syntax. The SELECT outside the case statements is redundant. Also, assuming that the joins to County and City are always 1-1, you can rewrite the query as:
SELECT op.OrderId, O.Number, SUM(op.Price) Price,
CONCAT(A.Detail, ' ', C.Name, ' / ', Ci.Name) AS UserAddress,
(CASE WHEN O.Status =0 THEN 'Onay Bekliyor' WHEN O.Status =1 THEN 'Onaylandı' WHEN O.Status = 2 THEN 'Reddedildi' END) Status,
O.Creation,
(CASE WHEN O.IsDelivered =0 THEN 'Teslim Edilmedi' ELSE 'Teslim Edildi' END) IsDelivered,
group_concat(P.Name) as Product
FROM kobiakinlar.product P JOIN
kobiakinlar.orderproduct op
ON op.ProductId = P.productId JOIN
kobiakinlar.order O
ON O.orderId = op.OrderId JOIN
kobiakinlar.address A ON A.addressId = O.AddressId join
County C
ON C.CountyId = A.CountyId join
City AS Ci
ON C.CityId = Ci.CityId
WHERE O.UserId = arg_UserId
GROUP BY op.OrderId;

Related

Use of CTE (WITH) in a MYSQL stored procedure

I'm trying to use CTE for running the query below to avoid the "can't reopen the table xxxx' error in a MySQL stored procedure. MySQL Workbench claims a syntax error and doesn't allow to save the procedure. WITH is underlined in red and reads
"With" is not valid a this position for this server version (8.0.13), expecting : CACHE, CHECKSUM, COMMIT.....
ELSE
-- Board charges
WITH cte AS (SELECT id FROM billablehorses)
INSERT INTO billablehorses
(id, horse, agrid, dos, months, installment, totalamount, billed, payableid)
SELECT ah.id, h.name, a.id, ah.dos,
TIMESTAMPDIFF(MONTH, ah.dos, b.boardingdate) Months,
p.amount,
a.totalamount,
TRUE,
p.id
FROM horses h
INNER JOIN agreementhorses ah
ON h.id = ah.horseid
INNER JOIN agreements a
ON ah.agreementid = a.id
INNER JOIN payables p
ON ah.id = p.agreementhorseid
INNER JOIN boarding b
ON p.ticketid = b.id
WHERE p.typeid = 1
AND ah.id IN (SELECT id FROM cte)
AND EXISTS (SELECT id FROM billed WHERE id = ah.id);
SET msg = CONCAT(ROW_COUNT() , " ", 'board charges inserted');
END IF;

MySQL - I want my table JOIN to return a single row based on a condition

in the below code there are multiple entries in 'leads' table with the same 'account_id'. I want it to return a single row - the one with the minimal value of another field 'date_entered'. I cannot use 'group by' on account_id as I intend to use 'group by' on BU and get summation accordingly. Please help.
select uc.business_unit_dp_c,
FORMAT(SUM(CASE
WHEN lc.source_leads_c not in ('Discovery','Discovery SuperEmail','Self Generated','Partner','Channel_Partner') and k.id<>'' THEN k.order_value
WHEN lc.source_leads_c not in ('Discovery','Discovery SuperEmail','Self Generated','Partner','Channel_Partner') and s.id<>'' THEN s.sivr_aiv_inr
ELSE 0
END),0)
as Online,
FORMAT(SUM(CASE
WHEN lc.source_leads_c in ('Discovery', 'Discovery SuperEmail') and k.id<>'' THEN k.order_value
WHEN lc.source_leads_c in ('Discovery', 'Discovery SuperEmail') and s.id<>'' THEN s.sivr_aiv_inr
ELSE 0
END),0)
as Discovery,
FORMAT(SUM(CASE
WHEN lc.source_leads_c in ('Partner','Channel_Partner') and k.id<>'' THEN k.order_value
WHEN lc.source_leads_c in ('Partner','Channel_Partner') and s.id<>'' THEN s.sivr_aiv_inr
ELSE 0
END),0)
as Self_Generated_CP
from opportunities as o
left join opportunities_cstm as oc on o.id=oc.id_c
left join opportunities_knw_caf_1_c as ok on o.id=ok.opportunities_knw_caf_1opportunities_ida
left join knw_caf as k on ok.opportunities_knw_caf_1knw_caf_idb=k.id
left join opportunities_knw_sivr_caf_1_c as os on os.opportunities_knw_sivr_caf_1opportunities_ida=o.id
left join knw_sivr_caf as s on s.id=os.opportunities_knw_sivr_caf_1knw_sivr_caf_idb
left join accounts_opportunities as ao on ao.opportunity_id=o.id
left join leads as l on l.account_id=ao.account_id and l.account_id <> ''
left join leads_cstm as lc on lc.id_c=l.id
left join users_cstm as uc on uc.id_c=o.assigned_user_id
where o.sales_stage='clw' and
(k.id<>'' or s.id<>'') and o.jira_raise_date <> '' and
(o.tranjection_type in ('Fresh Plan / New Customer','Number Activation','Revival','Balance Amount') or o.transaction_sivr in ('Paid Project','Number Allocation','New Feature')) and
o.jira_raise_date between '2016-06-01' and curdate()
group by uc.business_unit_dp_c
Write SQL just as you described
Select *
from from opportunities o
left join opportunities_cstm oc
on o.id = oc.id_c
left join opportunities_knw_caf_1_c ok
on o.id = ok.opportunities_knw_caf_1opportunities_ida
left join knw_caf k
on ok.opportunities_knw_caf_1knw_caf_idb = k.id
left join opportunities_knw_sivr_caf_1_c os
on os.opportunities_knw_sivr_caf_1opportunities_ida=o.id
left join knw_sivr_caf s
on s.id = os.opportunities_knw_sivr_caf_1knw_sivr_caf_idb
left join accounts_opportunities ao
on ao.opportunity_id=o.id
left join leads l
on l.account_id=ao.account_id
and l.account_id <> ''
left join leads_cstm lc
on lc.id_c = l.id
left join users_cstm uc
on uc.id_c = o.assigned_user_id
where o.sales_stage = 'clw' and
and (k.id <> '' or s.id <> '')
and o.jira_raise_date <> ''
and (o.tranjection_type in
('Fresh Plan / New Customer',
'Number Activation','Revival','Balance Amount') or
o.transaction_sivr in
('Paid Project','Number Allocation','New Feature'))
and o.jira_raise_date between '2016-06-01' and curdate()
-- next, add this additional predicate to Where clause...
use table w/DateEntered column
and date_entered =
(Select Min(date_entered)
From accounts_opportunities os
join tableWithDateEntered dr -- Table w/DateEntered
on ????? -- proper join criteria here
Where os.account_id = l.account_id)
--- or as constructed by op ( and simplified by me, since both account_id and date_entered are in table leads, that's the only table that needs to be referenced in the subquery).....
and l.date_entered =
(select min(date_entered)
from leads
where account_id = l.account_id)
select min(C.date),C.Customer_Code from (
select InvoiceNo,month(InvoiceDate) as date,Customer_Code,Createddate from tbl_Invoices A Inner Join tbl_customer B on A.customer_Code=B.CustomerCode
where YEAR(InvoiceDate)='2017'
and CustomerCode not in (select CustomerCode from tbl_customer where year(createddate) in (year(getdate())))
and CustomerCode not in (select customer_Code from tbl_Invoices where year(InvoiceDate) in (year(getdate())-1))
and CustomerCode in (select customer_Code from tbl_Invoices where year(InvoiceDate) not in (year(getdate())))
--group by Customer_Code,Createddate,InvoiceNo
)C group by C.Customer_Code

MySQL, CROSS JOIN and unknown column

I wrote mysql query which use subquery. I can't understand why mysql shows me 'Unknown column 'T.TICKETID' in 'where clause'' in subquery. For MSSQL it works without problem.
SELECT #PERIODTYPE := 'Y';
SELECT
#PERIODTYPE,
CASE
WHEN #PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
WHEN #PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
WHEN #PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d')
END `Period`,
DATE_FORMAT(MIN(R.DATENEW),'%Y-%m-%d') min_date,
DATE_FORMAT(MAX(R.DATENEW),'%Y-%m-%d') max_date,
COUNT(*) Transactions,
SUM(SQ.`Sold Units`) `Sold Units`,
FORMAT(MAX(P.TOTAL), 2) `Largest Order`,
FORMAT(SUM(P.TOTAL), 2) `Total $ Sold`,
FORMAT(SUM(TXL.AMOUNT), 2) `Total Tax $ Collected`
FROM RECEIPTS R INNER JOIN TAXLINES TXL ON R.ID = TXL.RECEIPT
INNER JOIN TAXES TX ON TXL.TAXID = TX.ID
INNER JOIN TAXCATEGORIES TXC ON TX.CATEGORY = TXC.ID
INNER JOIN PAYMENTS P ON P.RECEIPT = R.ID
INNER JOIN TICKETS T ON R.ID = T.ID
CROSS JOIN (SELECT SUM(TL.UNITS) 'Sold Units' FROM TICKETLINES TL WHERE T.TICKETID = TL.TICKET) SQ
GROUP BY CASE
WHEN #PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
WHEN #PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
WHEN #PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d')
END
ORDER BY R.DATENEW
Update 1
I replaced table name (TICKETS) instead of alias (T). I got error. Please, see the screenshot.
SELECT #PERIODTYPE := 'D';
SELECT
#PERIODTYPE,
CASE
WHEN #PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
WHEN #PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
WHEN #PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d')
END `Period`,
DATE_FORMAT(MIN(R.DATENEW),'%Y-%m-%d') min_date,
DATE_FORMAT(MAX(R.DATENEW),'%Y-%m-%d') max_date,
COUNT(*) Transactions,
FORMAT(SUM(IFNULL(SQ.`Sold Units`,0)),2) `Sold Units`,
FORMAT(MAX(P.TOTAL), 2) `Largest Order`,
FORMAT(SUM(P.TOTAL), 2) `Total $ Sold`,
FORMAT(SUM(TXL.AMOUNT), 2) `Total Tax $ Collected`
FROM RECEIPTS R INNER JOIN TAXLINES TXL ON R.ID = TXL.RECEIPT
INNER JOIN TAXES TX ON TXL.TAXID = TX.ID
INNER JOIN TAXCATEGORIES TXC ON TX.CATEGORY = TXC.ID
INNER JOIN PAYMENTS P ON P.RECEIPT = R.ID
INNER JOIN TICKETS ON R.ID = TICKETS.ID
-- LEFT JOIN (SELECT TL.TICKET, SUM(TL.UNITS) 'Sold Units' FROM TICKETLINES TL GROUP BY TL.TICKET) SQ ON T.ID = SQ.TICKET
CROSS JOIN (SELECT SUM(TL.UNITS) 'Sold Units' FROM TICKETLINES TL WHERE TICKETS.ID = SQ.TICKET) SQ
GROUP BY CASE
WHEN #PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
WHEN #PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
WHEN #PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d')
END
ORDER BY R.DATENEW
The query don't work in mysql beacuse mysql can't use an alias declared ad an upper level respect the subquery ..
in your subquery in Cross Join
CROSS JOIN (SELECT SUM(TL.UNITS) 'Sold Units'
FROM TICKETLINES TL WHERE T.TICKETID = TL.TICKET) SQ
You use T.TICKETID
The alias T
INNER JOIN TICKETS T ON R.ID = T.ID
Is declare in a place not "accessible" by the scope of the subquery ..
Then if is possible i suggest you of build a subquery that non refer to T alias .. Try (if is possible ) building an equivalent subquery wihout alias

How to avoid repetitions of columns in MySQL subquery?

i am retrieving the result from above 4 table using following query
SELECT
(SELECT SUM(CASE when c.Training_Id=1 then 1 else 0 end)
FROM courses c
INNER JOIN enrolled_students es
ON c.Course_Id = es.Course_Id
) STEM,
(SELECT SUM(CASE when c.Training_Id=2 then 1 else 0 end)
FROM courses c
INNER JOIN enrolled_students es ON c.Course_Id = es.Course_Id
) MA,
c.* FROM campus c;
The problem with this query is, two(2) students are in STEM and one(1) Student in MA against Campus_Id 3, but its repeating records against all campuses. i want if campus has no students than there should be '0' Zero.
You need to filter your subselects by Campus_Id. But first you have to use distinct table aliases. Change your last line to ca.* FROM campus ca. Then you can use a where clause in your subselects (WHERE c.Campus_Id = ca.Campus_Id).
SELECT
(SELECT SUM(CASE when c.Training_Id=1 then 1 else 0 end)
FROM courses c
INNER JOIN enrolled_students es
ON c.Course_Id = es.Course_Id
WHERE c.Campus_Id = ca.Campus_Id -- line added
) STEM,
(SELECT SUM(CASE when c.Training_Id=2 then 1 else 0 end)
FROM courses c
INNER JOIN enrolled_students es ON c.Course_Id = es.Course_Id
WHERE c.Campus_Id = ca.Campus_Id -- line added
) MA,
ca.* FROM campus ca; -- line changed
This should solve your problem.
To improve the performance you can also filter your subselects by Training_Id. In the first subselect you only need the rows with Training_Id=1. So you can change your where clause to:
WHERE c.Campus_Id = ca.Campus_Id
AND c.Training_Id = 1
Doing that you can also use COUNT instead of SUM. So your subselect would look like:
SELECT COUNT(1)
FROM courses c
INNER JOIN enrolled_students es ON c.Course_Id = es.Course_Id
WHERE c.Campus_Id = ca.Campus_Id AND c.Training_Id = 1
To prevent code duplication (your subselects are almost equal) you can join all needed tables and group by Campus_Id:
select
COUNT(co.Training_Id=1 OR NULL) STEM,
COUNT(co.Training_Id=2 OR NULL) MA,
ca.Campus_Id
from campus ca
left join courses co on co.Campus_Id = ca.Campus_Id
left join enrolled_students es on es.Course_Id = co.Course_Id
where co.Training_Id in (1, 2)
group by ca.Campus_Id

MYSQL | Getting syntax error for using subquery in COUNT()

SELECT
E.`employee_id`,
E.`full_name`,
LE.`no_of_leaves` AS AllocatedLeaves,
MLLT.`leave_type` AS LeaveTypeName,
COUNT(SELECT * FROM leave_approval WHERE employee_id = 1) AS TotalLeavesTaken
FROM employee E
INNER JOIN leave_entitlement LE
ON E.`employee_id` = LE.`employee_id`
INNER JOIN `ml_leave_type` MLLT
ON MLLT.`ml_leave_type_id` = LE.`ml_leave_type_id`
LEFT JOIN leave_approval LA
ON E.`employee_id` = LA.`employee_id`
LEFT JOIN leave_application LAPP
ON LAPP.`application_id` = LA.`leave_application_id`
LEFT JOIN ml_leave_type MLLTLA
ON MLLTLA.`ml_leave_type_id` = LAPP.`ml_leave_type_id`
i am getting syntax error near count, but i tried to find out the syntax error and i could not find any?
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 'select * from leave_approval where employee_id = 1) AS TotalLeavesTaken
from emp' at line 6
Is it really a syntax error. or is there something i am missing here??
This line:
COUNT(SELECT * FROM leave_approval WHERE employee_id = 1) AS TotalLeavesTaken
is incorrect. You can not do a count on select * subquery without placing it in parentheses, but even then you'd need a group by and additional logic. The better approach would be:
(Select count(*) from leave_approval where employee_id = 1) AS TotalLeavesTaken
change
COUNT(SELECT * FROM leave_approval WHERE employee_id = 1) AS TotalLeavesTaken
to
(SELECT count(*) FROM leave_approval WHERE employee_id = 1) AS TotalLeavesTaken
No need to use subquery for count in your case check below query
Try this:
SELECT E.employee_id, E.full_name, LE.no_of_leaves AS AllocatedLeaves,
MLLT.leave_type AS LeaveTypeName,
SUM(CASE WHEN LA.employee_id = 1 THEN 1 ELSE 0 END) AS TotalLeavesTakenByEmplyeeNo1
FROM employee E
INNER JOIN leave_entitlement LE ON E.employee_id = LE.employee_id
INNER JOIN `ml_leave_type` MLLT ON MLLT.ml_leave_type_id = LE.ml_leave_type_id
LEFT JOIN leave_approval LA ON E.employee_id = LA.employee_id
LEFT JOIN leave_application LAPP ON LAPP.application_id = LA.leave_application_id
LEFT JOIN ml_leave_type MLLTLA ON MLLTLA.ml_leave_type_id = LAPP.ml_leave_type_id
GROUP BY E.employee_id;
Try This Query
SELECT E.`employee_id`,E.`full_name`,LE.`no_of_leaves` AS AllocatedLeaves,MLLT.`leave_type` AS LeaveTypeName,
SUM( CASE WHEN LA.employee_id = '1' THEN 1 ELSE 0 END ) as TotalLeavesTaken
FROM employee E
INNER JOIN leave_entitlement LE
ON E.`employee_id` = LE.`employee_id`
INNER JOIN `ml_leave_type` MLLT
ON MLLT.`ml_leave_type_id` = LE.`ml_leave_type_id`
LEFT JOIN leave_approval LA
ON E.`employee_id` = LA.`employee_id`
LEFT JOIN leave_application LAPP
ON LAPP.`application_id` = LA.`leave_application_id`
LEFT JOIN ml_leave_type MLLTLA
ON MLLTLA.`ml_leave_type_id` = LAPP.`ml_leave_type_id`
Only you have to replace COUNT(SELECT * FROM leave_approval WHERE employee_id = 1) to SUM( CASE WHEN LA.employee_id = '1' THEN 1 ELSE 0 END )
Thanks it, Try this because i have tried this query and it will give you perfect result