I'm attempting to convert a MySQL query to a T-SQL query and the IF statement that's enclosed within a SUM statement is tripping me up. Any suggestions?
SELECT
CMTS_RQ.[Dated],
CMTS_RQ.CMTS_Name,
Count(CMTS_RQ.CMTS_Name) AS emat_count,
Sum(if(CMTS_RQ.US_Pwr>=37 and CMTS_RQ.US_Pwr<=49)) AS us_pwr_good
FROM
CMTS_RQ
GROUP BY
CMTS_RQ.CMTS_Name,
CMTS_RQ.[Dated]
But I get an error:
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'if'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
T-SQL doesn't have a "inline" IF statement - use a CASE instead:
SELECT
CMTS_RQ.[Dated],
CMTS_RQ.CMTS_Name,
Count(CMTS_RQ.CMTS_Name) AS emat_count,
Sum(CASE
WHEN CMTS_RQ.US_Pwr >=37 AND CMTS_RQ.US_Pwr <= 49
THEN 1
ELSE 0
END) AS us_pwr_good
FROM
CMTS_RQ
GROUP BY
CMTS_RQ.CMTS_Name,
CMTS_RQ.[Dated]
So if the value of CMTS_RQ.US_Pwr is >= 37 AND <= 49 then add 1 to the SUM - otherwise 0. Does that give you what you're looking for?
In SQL Server 2012 and newer, you can use the new IIF function:
SUM(IIF(CMTS_RQ.US_Pwr >= 37 AND CMTS_RQ.US_Pwr <= 49, 1, 0)) AS us_pwr_good
Related
I need this query without using Group by:
SELECT
SUM(a.MstSalesCounterVoucherDetails.Qty)
FROM
MstSalesCounterVoucherDetails a
WHERE
(ISNULL(a.MstSalesCounterVoucherDetails.UOMQTY, 0) - ISNULL(a.MstSalesCounterVoucherDetails.Qty, 0))
AND (a.VoucherId=374956)
Every statement you put in the WHERE clause needs to result in a true or false. You have an arithmetic expression that yields a number, but not a true/false, what is known as a boolean. This numeric result is not automatically converted to a boolean (as in some other programming languages).
WHERE (ISNULL(a.MstSalesCounterVoucherDetails.UOMQTY, 0) - ISNULL(a.MstSalesCounterVoucherDetails.Qty, 0))
You need to change this to, for instance:
WHERE (ISNULL(a.MstSalesCounterVoucherDetails.UOMQTY, 0) - ISNULL(a.MstSalesCounterVoucherDetails.Qty, 0)) <> 0
add condition like > , < or = where you are calculating UOMQTY-Qty
where (ISNULL(a.MstSalesCounterVoucherDetails.UOMQTY, 0) - ISNULL(a.MstSalesCounterVoucherDetails.Qty, 0))>0 AND (a.VoucherId=374956)
I use a case when it works perfectly fine on MySQL but not MS SQL please help.
It seems the equal is not accepted -- if not what then will work
SELECT A FROM TABLE A
WHERE
CASE WHEN COUNT = 2 THEN GOAL = 2 ELSE GOAL = 3 END
Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword
'CASE'.
You need to change your query to WHERE columnName = value.
The following query is what you are expecting:
SELECT A.*
FROM TABLE A
WHERE GOAL = CASE WHEN COUNT = 2 THEN 2 ELSE 3 END
I need help in converting my Oracle trigger to T-SQL. Any help is greatly appreciated
CREATE TRIGGER "REQUESTOR_TRG"
BEFORE INSERT ON REQUESTOR
FOR EACH ROW
BEGIN
<<COLUMN_SEQUENCES>>
BEGIN
IF INSERTING AND :NEW.ID IS NULL THEN
SELECT REQUESTOR_SEQ.NEXTVAL INTO :NEW.ID FROM SYS.DUAL;
END IF;
END COLUMN_SEQUENCES;
END;
I am getting these errors:
Msg 102, Level 15, State 1, Procedure REQUESTOR_TRG, Line 2
Incorrect syntax near 'BEFORE'
Msg 4145, Level 15, State 1, Procedure REQUESTOR_TRG, Line 7
An expression of non-boolean type specified in a context where a condition is expected, near 'AND'.
Msg 102, Level 15, State 1, Procedure REQUESTOR_TRG, Line 8
Incorrect syntax near ':'.
Msg 102, Level 15, State 1, Procedure REQUESTOR_TRG, Line 10
Incorrect syntax near 'COLUMN_SEQUENCES'.
select 2678400 * 1393
When I run this query, I'm getting this error in SQL Server:
Msg 8115, Level 16, State 2, Line 23
Arithmetic overflow error converting expression to data type int.
Please help me to resolve this issue.
Since this result overflows the capacity of INT - use BIGINT instead:
SELECT CAST(2678400 AS BIGINT) * 1393
I have this query that spawns the following error:
SELECT * FROM Quota
WHERE LEFT(QtLabel, LEN(QtLabel)-2) IN (
'1032',
'3300',
'9682'
)
Msg 536, Level 16, State 5, Line 1
Invalid length parameter passed to the SUBSTRING function.
Am I doing something wrong? It tends to show up when I use the LEN() function. Might it be a datatype issue?
Is it possible that LEN(QtLabel) <= 2 ?
Are you sure that each QtLabel field is longer than 2 characters?
LEFT probably uses SUBSTRING internally. What happens if the length of QtLabel is <= 2?
It's because some QtLabel values don't contain > 2 characters, so you end up trying to do a LEFT() with a negative value as the number to restrict to.
In your scenario, you're assuming all QtLabel values are 6 characters, so uou should do:
SELECT * FROM Quota
WHERE LEN(QtLabel) = 6
AND LEFT(QtLabel, LEN(QtLabel)-2) IN (
'1032',
'3300',
'9682'
)
SELECT LEFT('MyText', -2)
will throw the same error