I have a column discount_amount, if there's no SUM(discount_amount) (NULL) THEN it's zero (0).
I wrote the query with this
SELECT COALESCE(SUM(discount_amount)
FROM order_discount
WHERE order_discount.discount_type_id = 6
AND order_discount.order_match_id = om1.id, 0);
but I get an error
Error Code: 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 'FROM order_discount WHERE order_discount.discou' at line 2
You seem to want:
SELECT COALESCE(SUM(discount_amount), 0)
FROM order_discount
WHERE order_discount.discount_type_id = 6 AND order_discount.order_match_id = om1.id;
This has to be part of bigger query, where derive table om1 is defined somehow.
Alternatively, you can COALESCE() the result of the subquery like this:
COALESCE(
(
SELECT SUM(discount_amount)
FROM order_discount
WHERE order_discount.discount_type_id = 6 AND order_discount.order_match_id = om1.id
),
0
)
Again, this only makes sense if included in a bigger query.
Related
I'm trying to pull records from one table (workers) that matches 2 of 5 status options (Active or Pending), within a specific date range, and I want to identify from a separate table (work_earn) a specific payment method (ACH), and where they have not yet been paid (IS NULL)
I've tried a few different ways, but I keep getting a syntax error. What am I missing? Here are two queries that I tried, with corresponding error messages.
SELECT * FROM workers
WHERE (status = 'Active') OR (status= 'Pending')
and date >= '$Start' AND date <= '$End' AND
LEFT JOIN work_earn method = 'ACH' AND ON work_earn.wid IS NULL
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 work_earn method = 'ACH' AND ON work_earn.wid IS NULL LIMIT 0, 30' at line 1
SELECT * FROM workers
WHERE status = 'Active' OR 'Pending' and date >= '2018-04-18' AND date <= '2018-03-18' AND
LEFT JOIN work_earn method = 'ACH' AND ON work_earn.wid IS NULL
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 work_earn method = 'ACH' AND ON work_earn.wid IS NULL LIMIT 0, 30' at line 1
Any help would be greatly appreciated!
I have this script calculates profit
Steps :
barangbeli = harsat / diameter
p1 = barangbeli * 10
p2 = prof / 100
result = barangbeli + profit;
thanks
CREATE VIEW tbkeluar as
SELECT mbarang.kdbrg, mbarang.nmbrg, mbarang.spek,if(SUM(bkeluar.qty), SUM(bkeluar.qty), 0)as qty,(tbmasuk.harsat/mbarang.diameter) as hargabeli, ((hargabeli*10)/100 )+hargabeli) as profit
LEFT JOIN bkeluar on mbarang.kdbrg = bkeluar.kdbrg group by mbarang.kdbrg
i have 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 ') as profit from mbarang
LEFT JOIN bkeluar on mbarang.kdbrg = bkeluar.kdbrg gro' at line 2
The error message you received should be helpful enough to know whats wrong.
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 ') as profit from mbarang LEFT JOIN bkeluar on mbarang.kdbrg = bkeluar.kdbrg gro' at line 2
Based from the error message, MySQL says that you have a syntax error because of this character ), with some characters appended for you to locate it: ) as profit from mbarang
As you can see from your SELECT statement, you have an extra )
SELECT mbarang.kdbrg
, mbarang.nmbrg
, mbarang.spek
,if(SUM(bkeluar.qty), SUM(bkeluar.qty), 0)as qty
,(tbmasuk.harsat/mbarang.diameter) as hargabeli
, ((hargabeli*10)/100 )+hargabeli) as profit
^ delete this extra parenthesis
Your SELECT statement (in CREATE VIEW) is missing its FROM clause. Do you mean FROM mbarang?
You're also probably missing at least one more JOIN; the table tbmasuk is mentioned in the SELECT clause, but isn't mentioned anywhere else in the statement.
I am getting error in the partition clause. Please help me. The error is
Select Model_Name,
Case Model_Subtype
When 'Weight' then 'Static'
When 'Linked Account' then 'Dynamic'
When 'Flexible' then 'Dynamic'
Else 'Not Defined'
End as Model_Type, Security_Name, Market_Value,Weight,
Case When Weight = 0 And Market_Value= 0 Then 0
When Weight = 0 Then Cast(Market_Value/ nullif(SUM(market_Value)
OVER (Partition by Model_Name),0) AS Decimal (10,4))
When Weight !=0 Then Weight/100
Else Weight End as Target_Weight,
vehicle.Vehicle_Name
from
OFF_PRODUCT_MODEL model
Join OFF_PRODUCT_MODEL_TARGET target on target.Model_Id = model.Model_Id
Join OFF_PRODUCT_SECURITIES Sec on sec.Security_Id = target.Security_Id
left outer Join Offer_Back_End.tblStrategies Strategy on Strategy.Vestmark_Sleeve_Code = model.Model_Name
left outer join Offer_Back_End.tblVehicles vehicle On vehicle.Vehicle_Id = strategy.Vehicle_ID
Where (Strategy.Active_Strategy is null or Strategy.Active_Strategy = 1)
Err] 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 '(Partition by Model_Name),0) AS Decimal (10,4))
When Weight !=0 Then Weight/' at line 11
Umm, OVER (Partition by Model_Name) is analytical function which MySQL unfortunately doesn't support and don't have them and so you are getting the said error.
MySQL does not support OVER() clause. In general Window Functions are not part of MySQL.
You can however emulate it - please refer to the following answer:
https://stackoverflow.com/a/20432371/900564
SELECT School_info.district_id,count(School_info.no_of_class) ls
from School_info where school_level IN('ls')
JOIN
SELECT t.district_id,count(t.no_of_class) p from School_info as t
where school_level IN('ps')
ON
( School_info.district_id = t.district_id)
Where is my error , i did not find it . when i ran the query above , it shows as : -
MySQL said: Documentation
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 SELECT t.district_id,count(t.no_of_class) p from
School_info as t where sc' at line 2
Your query has a lot of syntax errors, but in any case it can be simplified as this:
select
district_id
, sum(school_level = 'ls') ls
, sum(school_level = 'ps') p
from school_info
where school_level in ('ls','ps')
group by district_id
The sum(school_level = 'p') uses the fact that MySQL does a boolean evaluation of the expression inside the sum() which returns 1 if true. Summing all 1s gives the same result as counting rows.
This will give you an output like this:
district_id ls p
where ls and p are counts.
so i get an error on the this sql query, but i canĀ“t see my mistake myself:
SELECT group_members.group_id,
group_members.permissions,
group.group_name
FROM group_members,
group
WHERE group_members.group_id=group.group_id
AND group.group_id = 1
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 'group WHERE group_members.group_id=group.group_id AND group.group_id = 1
Thanks for your help!
Group is a reserved word in MySQL either enclose it in backticks "`" or better yet do not use it as a table name
SELECT group_members.group_id, group_members.permissions, `group`.group_name
FROM group_members, `group`
WHERE group_members.group_id=`group`.group_id
AND `group`.group_id = 1
Try this
SELECT group_members.group_id, group_members.permissions, `group`.group_name
FROM group_members, group
WHERE group_members.group_id=`group`.group_id
AND `group`.group_id = 1