I've MYSQL Query, its working fine
The QUERY Is:
SELECT * , IF(totexec >= totexecrun1, totexec-totexecrun1,0) AS rewrk,
SUM(tcu) tcunit ,
IF(totexec=0, ((SUM(tcu)/totexec)*100),0) AS proflevel
FROM mntest_schedule a
LEFT JOIN mnrelease_details b
ON b.tester=a.tester
AND a.project=b.project
LEFT JOIN tc_details c
ON b.tc_id=c.tc_name
AND a.project=c.project
WHERE a.rel_name='automanual_assign'
AND a.project='JupiterQA'
GROUP BY a.tester;
I tried to execute the same Query in MSSQL but its throwing error.
ERROR IS:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
Am I did anything wrong with this query?
SELECT * ,
CASE WHEN totexec >= totexecrun1 THEN totexec - totexecrun1
ELSE 0
END AS rewrk ,
SUM(tcu) tcunit ,
CASE WHEN totexec = 0 THEN ( SUM(tcu) / totexec ) * 100
ELSE 0
END AS proflevel
FROM mntest_schedule a
LEFT JOIN mnrelease_details b ON b.tester = a.tester
AND a.project = b.project
LEFT JOIN tc_details c ON b.tc_id = c.tc_name
AND a.project = c.project
WHERE a.rel_name = 'automanual_assign'
AND a.project = 'JupiterQA'
GROUP BY a.tester;
Use CASE instead of IF.
Refer this(Understanding Case Expression in SQL Server with Example) to learn CASE in SQL SERVER.
SELECT * ,
CASE WHEN (totexec >= totexecrun1)
THEN totexec-totexecrun1
ELSE 0 END AS rewrk,
SUM(tcu) tcunit ,
CASE WHEN (totexec=0)
THEN ((SUM(tcu)/totexec)*100)
ELSE 0 END AS proflevel
FROM mntest_schedule a LEFT JOIN mnrelease_details b
ON b.tester=a.tester AND a.project=b.project
LEFT JOIN tc_details c ON b.tc_id=c.tc_name AND a.project=c.project
WHERE a.rel_name='automanual_assign' AND a.project='JupiterQA'
GROUP BY a.tester;
Related
I want to use the sum function in the ELSE part of the CASE Statement. I need to give the SUM function conditions but I'm unable to do so.
I tried to use the GROUP BY Function within the sum function but it doesn't work.
CASE WHEN SectionCrossList = NULL THEN InstructorCredits ELSE InstructorCredits/Sum(Instructorcredits) GROUP BY (AcademicYear,SectionCrossList) END NumCreditSplit
Msg 156, Level 15, State 1, Line 220 Incorrect syntax near the keyword 'GROUP'.
Msg 102, Level 15, State 1, Line 221 Incorrect syntax near 'NumCreditSplit'.
You should use CTE or window function sum
SELECT AcademicYear,SectionCrossList,
CASE
WHEN SectionCrossList = NULL
THEN
InstructorCredits
ELSE
InstructorCredits/Sum(Instructorcredits)
end NumCreditSplit
FROM <yourTable>
group by AcademicYear,SectionCrossList
What is wrong with my code?
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near 'UP'.
SQL server says missing something
UPDATE
(
SELECT
T.Br, U.Br AS Br1 ,
T.Dis, U.Dis AS Disc1 ,
T.DeletedDate , U.DeletedDate AS DeletedDate1 ,
T.INSERT_TS, U.INSERT_TS AS INSERT_TS1
FROM dbo.Matrix T
JOIN tlt_svs.Matrix_Update U
ON T.Key= U.Key)UP
SET
UP.Br = UP.Br1 ,
UP.Dis = UP.Dis1 ,
UP.DeletedDate = UP.DeletedDate1 ,
UP.INSERT_TS = UP.INSERT_TS1
Maybe you want an UPDATE with a join, i.e. update the values of matrix with the corresponding values of matrix_update?
UPDATE m
SET m.br = mu.br,
m.dis = mu.dis,
m.deleteddate = mu.deleteddate,
m.insert_ts = mu.insert_ts
FROM dbo.matrix m
INNER JOIN tlt_svs.matrix_update mu
ON mu.key = m.key;
I am getting an incorrect syntax error on line 35/36 in the following query:
CREATE PROCEDURE afm.SP_Get_ZINTLL
AS
Begin
SELECT DISTINCT ls_id AS ZINTLSID_0
,'ARC' AS ZINTSRCCODE_0 ,landlord_tenant
,date_start
,coalesce(ls.bl_id, ls.pr_id) as [ZINTLSTYPE_0]
,coalesce(date_end, GETDATE()) as [ZINTSTRDAT_0]
,tn_name
,ld_name
,isnull(purchase_requisition, 0) as [ZINTBPRNUM_0]
,coalesce(ls.asset_id,0) as [ZINTBPSNUM_0]
, ZINTCCE1_0 = ls.ls_id
--init_value defaulting to JHB removed 2018-06-22
SELECT dim_code
FROM afm.za_dim_lookup
WHERE init_value = coalesce(ls.registered_office, '')
AND dim_type = 'DT1'
,ZINTCCE2_0 = ls.ls_id
SELECT dim_code
FROM afm.za_dim_lookup
WHERE init_value = 'DEFAULT_LS'
AND dim_type = 'DT2'
)
end
As a pure guess, the 2 SELECTs are meant to be subqueries, then this is probably what you are after...
SELECT DISTINCT
ls_id AS ZINTLSID_0,
'ARC' AS ZINTSRCCODE_0,
landlord_tenant,
date_start,
COALESCE(ls.bl_id, ls.pr_id) AS [ZINTLSTYPE_0],
COALESCE(date_end, GETDATE()) AS [ZINTSTRDAT_0],
tn_name,
ld_name,
ISNULL(purchase_requisition, 0) AS [ZINTBPRNUM_0],
COALESCE(ls.asset_id, 0) AS [ZINTBPSNUM_0],
ls.ls_id AS ZINTCCE1_0,
--init_value defaulting to JHB removed 2018-06-22
(SELECT dim_code
FROM afm.za_dim_lookup
WHERE init_value = COALESCE(ls.registered_office, '')
AND dim_type = 'DT1'
AND ZINTCCE2_0 = ls.ls_id), --AS ...??? Will this only return 1 row?
(SELECT dim_code
FROM afm.za_dim_lookup
WHERE init_value = 'DEFAULT_LS'
AND dim_type = 'DT2') --AS ...??? Will this only return 1 row?
FROM ...;
This is probably going to generate more errors though (I suspect a sub-query returned more than 1 row error would be one).
You would be far better off providing sample data and expected results, as well as your full query.
These are the questions:
Write a script that will give you six columns of output.
a) the customer first name
b) the customer last name
c) the order number
d) the item name
e) the discount
f) a column with the following words for the discount----either NONE, LESS THAN TWENTY, LESS THAN FORTY, or MORE THAN FORTY
I have done this:
SELECT
Customers.FirstName,
Customers.LastName,
Orders.OrderID,
Products.ProductName,
OrderItems.DiscountAmount,
IF(OrderItems.DiscountAmount = 0, 'NONE', IF(OrderItems.DiscountAmount <= 20, 'LESS THAN TWENTY', IF(OrderItems.DiscountAmount <= 40, 'LESS THAN FORTY', 'MORE THAN FORTY'))) AS discount
FROM
Customers
INNER JOIN
Orders ON (Customers.ShippingAddressID = Orders.ShipAddressID
AND Customers.BillingAddressID = Orders.BillingAddressID)
INNER JOIN
OrderItems ON (Orders.OrderID = OrderItems.OrderID)
INNER JOIN
Products ON (OrderItems.ProductID = Products.ProductID)
It gives me an error on my if statement? Can you see what I'm doing wrong?
This is the database I'm using:
Error messages:
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Your database diagram and errors suggest you are using Microsoft SQL Server, not MySQL. The IF command/expression/keyword is not standardized and implemented differently in different platforms.
Therefore you should not use MySQL proprietary IF-syntax but the MSSQL proprietary IIF-syntax. IF is a control-of-flow statement in MSSQL.
This is SQL Server. There is no if statement. Use case:
(CASE WHEN OrderItems.DiscountAmount = 0 THEN 'NONE'
WHEN OrderItems.DiscountAmount <= 20 THEN 'LESS THAN TWENTY'
WHEN OrderItems.DiscountAmount <= 40 THEN 'LESS THAN FORTY'
ELSE 'MORE THAN FORTY'
END) AS discount
Use "case..when" statement here. Usage link here: case statement
Case
when OrderItems.DiscountAmount = 0 then 'NONE'
when OrderItems.DiscountAmount <= 20 then 'LESS THAN TWENTY'
when OrderItems.DiscountAmount <= 40 then 'LESS THAN FORTY'
else 'MORE THAN FORTY'
End AS discount
I am trying to enter the following to an UPDATE trigger of QuoteItem:
UPDATE QuoteItem
SET List = StyleItem.List, CostFactor = StyleItem.CostFactor, Cost = NULL
FROM dbo.fn_GetQuoteItemListPrice(QuoteItem.ItemId, QuoteItem.RoomId)
AS StyleItem
CROSS JOIN (QuoteItem JOIN INSERTED ON
QuoteItem.QuoteItemId = INSERTED.QuoteItemId)
WHERE (INSERTED.List IS NULL) AND (INSERTED.ItemId IS NOT NULL)
I get the follwoing error:
Msg 4104, Level 16, State 1, Procedure QuoteItem_UPDATE, Line 6
The multi-part identifier "QuoteItem.ItemId" could not be bound.
Msg 4104, Level 16, State 1, Procedure QuoteItem_UPDATE, Line 6
The multi-part identifier "QuoteItem.RoomId" could not be bound.
UPDATE QuoteItem
SET List = StyleItem.List, CostFactor = StyleItem.CostFactor, Cost = NULL
FROM dbo.fn_GetQuoteItemListPrice(xx.ItemId, xx.RoomId)
AS StyleItem
CROSS JOIN (QuoteItem JOIN INSERTED ON
QuoteItem.QuoteItemId = INSERTED.QuoteItemId) xx
WHERE (INSERTED.List IS NULL) AND (INSERTED.ItemId IS NOT NULL)
Try the change I made above, assigning a table alias xx and referencing that instead..
Answer found here by Naomi Nosonovsky:
UPDATE QuoteItem
SET List = StyleItem.List, CostFactor = StyleItem.CostFactor, Cost = NULL
FROM QuoteItem JOIN INSERTED AS INSERTED ON QuoteItem.QuoteItemId =
INSERTED.QuoteItemId
CROSS APPLY dbo.fn_GetQuoteItemListPrice(QuoteItem.ItemId, QuoteItem.RoomId)
AS StyleItem
WHERE (INSERTED.List IS NULL) AND (INSERTED.ItemId IS NOT NULL)