MySQL 'partition by' clause - mysql

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

Related

Where is my mistake selecting coalesce mysql?

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.

mysql query with Or, Left Join, Outer Left Join, AND ON and IS NULL

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!

CASE WHEN mySQL keeps getting error

I am trying to use the CASE statement, but I keep getting a syntax error. I have followed the docs but I cannot seem to get rid of this syntax error:
query:
SELECT students.firstname, students.lastname, students.tnumber, grades.tnumber
FROM students, grades
CASE WHEN grades.tnumber=students.tnumber AND grades.courseid='CSC2110' AND (grades.Grade='A' OR grades.Grade='B' OR grades.Grade='C')THEN 'YES' ELSE 'NO' END
error:
error executing query. MySQL: 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 'CASE WHEN grades.tnumber=students.tnumber AND grades.courseid='CSC2110' A' at line 1
Ignore the tables and select statements. I am more concerned are checking the syntax of my CASE statement and why it is throwing an error.
It isn't completely clear what you want the CASE expression to do, but it appears that it should be in the SELECT list. I also moved the join condition, which strangely appeared in the CASE expression, to the ON clause.
SELECT students.firstname,
students.lastname,
students.tnumber,
grades.tnumber,
CASE WHEN grades.courseid = 'CSC2110' AND
(grades.Grade='A' OR grades.Grade='B' OR grades.Grade='C')
THEN 'YES' ELSE 'NO' END AS someColumn
FROM students
INNER JOIN grades
ON grades.tnumber = students.tnumber
You can also try this one mate, using proper and readable syntax:
SELECT
s.tnumber, s.firstname, s.lastname,
IF (
-- condition
grades.Grade = 'A' OR grades.Grade = 'B' OR grades.Grade = 'C',
-- if true
'YES',
-- else
'NO'
) `status`
FROM
students s
INNER JOIN grades g ON g.tnumber = s.tnumber
WHERE grades.courseid = 'CSC2110';
Note
You can where that I have included a WHERE section where you can easily reuse this code with other courseid
I also added a suggested format when presenting data which is for people, always put the id code infront
One more point is that, by using a proper format, it help you and others to understand your code

calculate profit 10% from buy price

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.

error in updating mysql table with filters

i have a mysql table with the follwoing fields :
id, desc, value, people, amount, weight
in the above order i run the follwoing
update match1 set weight = 5 where desc = 'fat' and id != '6';
follwoing is the error message i get :
**#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 'desc = 'bat' and id = 6' at line 1
can someone please let me know whats wrong with this?
the column desc is a keyword in mysql. use backquotes i.e.
where `desc` = 'fat'