CASE syntax with addition and greater/less operators
up vote
0
down vote
favorite
I'm getting syntax errors on the >
,<
, and =
operators here and I'm really not sure why. I've tried enclosing reorder_quantity + in_stock
in ()
and I still get the same errors. What am I missing here?
I'm trying to use the following code to generate a list of amounts that I should order for certain products. Im using SQL to do so because I order the products in oz. but they are inventoried in mLs, and my POS can't handle the conversion natively.
select itemnum, itemname, in_stock, reorder_level,
case reorder_quantity + in_stock
when reorder_quantity + in_stock > reorder_level
then reorder_quantity/29.5735
when reorder_quantity + in_stock = reorder_level
then reorder_quantity/29.5735*2
when reorder_quantity + in_stock < reorder_level
then reorder_quantity/29.5735*2
else 0
end as order_amount_oz
from inventory
group by itemnum
and in_stock < reorder_level
sql sql-server tsql
add a comment |
up vote
0
down vote
favorite
I'm getting syntax errors on the >
,<
, and =
operators here and I'm really not sure why. I've tried enclosing reorder_quantity + in_stock
in ()
and I still get the same errors. What am I missing here?
I'm trying to use the following code to generate a list of amounts that I should order for certain products. Im using SQL to do so because I order the products in oz. but they are inventoried in mLs, and my POS can't handle the conversion natively.
select itemnum, itemname, in_stock, reorder_level,
case reorder_quantity + in_stock
when reorder_quantity + in_stock > reorder_level
then reorder_quantity/29.5735
when reorder_quantity + in_stock = reorder_level
then reorder_quantity/29.5735*2
when reorder_quantity + in_stock < reorder_level
then reorder_quantity/29.5735*2
else 0
end as order_amount_oz
from inventory
group by itemnum
and in_stock < reorder_level
sql sql-server tsql
Why did you need to usegroup by
when you didn't use any aggregate function?
– D-Shih
Nov 13 at 0:29
1
Could you provide some sample data and exepct result?
– D-Shih
Nov 13 at 0:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm getting syntax errors on the >
,<
, and =
operators here and I'm really not sure why. I've tried enclosing reorder_quantity + in_stock
in ()
and I still get the same errors. What am I missing here?
I'm trying to use the following code to generate a list of amounts that I should order for certain products. Im using SQL to do so because I order the products in oz. but they are inventoried in mLs, and my POS can't handle the conversion natively.
select itemnum, itemname, in_stock, reorder_level,
case reorder_quantity + in_stock
when reorder_quantity + in_stock > reorder_level
then reorder_quantity/29.5735
when reorder_quantity + in_stock = reorder_level
then reorder_quantity/29.5735*2
when reorder_quantity + in_stock < reorder_level
then reorder_quantity/29.5735*2
else 0
end as order_amount_oz
from inventory
group by itemnum
and in_stock < reorder_level
sql sql-server tsql
I'm getting syntax errors on the >
,<
, and =
operators here and I'm really not sure why. I've tried enclosing reorder_quantity + in_stock
in ()
and I still get the same errors. What am I missing here?
I'm trying to use the following code to generate a list of amounts that I should order for certain products. Im using SQL to do so because I order the products in oz. but they are inventoried in mLs, and my POS can't handle the conversion natively.
select itemnum, itemname, in_stock, reorder_level,
case reorder_quantity + in_stock
when reorder_quantity + in_stock > reorder_level
then reorder_quantity/29.5735
when reorder_quantity + in_stock = reorder_level
then reorder_quantity/29.5735*2
when reorder_quantity + in_stock < reorder_level
then reorder_quantity/29.5735*2
else 0
end as order_amount_oz
from inventory
group by itemnum
and in_stock < reorder_level
sql sql-server tsql
sql sql-server tsql
asked Nov 13 at 0:18
Yofi
154
154
Why did you need to usegroup by
when you didn't use any aggregate function?
– D-Shih
Nov 13 at 0:29
1
Could you provide some sample data and exepct result?
– D-Shih
Nov 13 at 0:30
add a comment |
Why did you need to usegroup by
when you didn't use any aggregate function?
– D-Shih
Nov 13 at 0:29
1
Could you provide some sample data and exepct result?
– D-Shih
Nov 13 at 0:30
Why did you need to use
group by
when you didn't use any aggregate function?– D-Shih
Nov 13 at 0:29
Why did you need to use
group by
when you didn't use any aggregate function?– D-Shih
Nov 13 at 0:29
1
1
Could you provide some sample data and exepct result?
– D-Shih
Nov 13 at 0:30
Could you provide some sample data and exepct result?
– D-Shih
Nov 13 at 0:30
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You're mixing the two types of case
syntax:
case expression when value1 then output1 when value2 then output2 else output3 end
case when expression = value1 then output1 when expression = value2 then output2 else output3 end
together, which is a syntax error. Chose one or the other.
Try this refactoring and simplification that‘s the same as your intention:
reorder_quantity / 29.5735 *
case when reorder_quantity + in_stock > reorder_level then 1 else 2 end as order_amount_oz
That worked, thank you!
– Yofi
Nov 13 at 22:48
add a comment |
up vote
0
down vote
If I understand correctly you can try to remove group by
and using parentheses
select itemnum, itemname, in_stock, reorder_level,
case
when (reorder_quantity + in_stock) > reorder_level
then reorder_quantity/29.5735
when (reorder_quantity + in_stock) = reorder_level
then reorder_quantity/(29.5735*2)
when (reorder_quantity + in_stock) < reorder_level
then reorder_quantity/(29.5735*2)
else 0
end as order_amount_oz
from inventory
btw you don't need the brackets around the additions
– Bohemian♦
Nov 13 at 0:45
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You're mixing the two types of case
syntax:
case expression when value1 then output1 when value2 then output2 else output3 end
case when expression = value1 then output1 when expression = value2 then output2 else output3 end
together, which is a syntax error. Chose one or the other.
Try this refactoring and simplification that‘s the same as your intention:
reorder_quantity / 29.5735 *
case when reorder_quantity + in_stock > reorder_level then 1 else 2 end as order_amount_oz
That worked, thank you!
– Yofi
Nov 13 at 22:48
add a comment |
up vote
1
down vote
accepted
You're mixing the two types of case
syntax:
case expression when value1 then output1 when value2 then output2 else output3 end
case when expression = value1 then output1 when expression = value2 then output2 else output3 end
together, which is a syntax error. Chose one or the other.
Try this refactoring and simplification that‘s the same as your intention:
reorder_quantity / 29.5735 *
case when reorder_quantity + in_stock > reorder_level then 1 else 2 end as order_amount_oz
That worked, thank you!
– Yofi
Nov 13 at 22:48
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You're mixing the two types of case
syntax:
case expression when value1 then output1 when value2 then output2 else output3 end
case when expression = value1 then output1 when expression = value2 then output2 else output3 end
together, which is a syntax error. Chose one or the other.
Try this refactoring and simplification that‘s the same as your intention:
reorder_quantity / 29.5735 *
case when reorder_quantity + in_stock > reorder_level then 1 else 2 end as order_amount_oz
You're mixing the two types of case
syntax:
case expression when value1 then output1 when value2 then output2 else output3 end
case when expression = value1 then output1 when expression = value2 then output2 else output3 end
together, which is a syntax error. Chose one or the other.
Try this refactoring and simplification that‘s the same as your intention:
reorder_quantity / 29.5735 *
case when reorder_quantity + in_stock > reorder_level then 1 else 2 end as order_amount_oz
edited Nov 13 at 22:50
answered Nov 13 at 0:52
Bohemian♦
291k61409544
291k61409544
That worked, thank you!
– Yofi
Nov 13 at 22:48
add a comment |
That worked, thank you!
– Yofi
Nov 13 at 22:48
That worked, thank you!
– Yofi
Nov 13 at 22:48
That worked, thank you!
– Yofi
Nov 13 at 22:48
add a comment |
up vote
0
down vote
If I understand correctly you can try to remove group by
and using parentheses
select itemnum, itemname, in_stock, reorder_level,
case
when (reorder_quantity + in_stock) > reorder_level
then reorder_quantity/29.5735
when (reorder_quantity + in_stock) = reorder_level
then reorder_quantity/(29.5735*2)
when (reorder_quantity + in_stock) < reorder_level
then reorder_quantity/(29.5735*2)
else 0
end as order_amount_oz
from inventory
btw you don't need the brackets around the additions
– Bohemian♦
Nov 13 at 0:45
add a comment |
up vote
0
down vote
If I understand correctly you can try to remove group by
and using parentheses
select itemnum, itemname, in_stock, reorder_level,
case
when (reorder_quantity + in_stock) > reorder_level
then reorder_quantity/29.5735
when (reorder_quantity + in_stock) = reorder_level
then reorder_quantity/(29.5735*2)
when (reorder_quantity + in_stock) < reorder_level
then reorder_quantity/(29.5735*2)
else 0
end as order_amount_oz
from inventory
btw you don't need the brackets around the additions
– Bohemian♦
Nov 13 at 0:45
add a comment |
up vote
0
down vote
up vote
0
down vote
If I understand correctly you can try to remove group by
and using parentheses
select itemnum, itemname, in_stock, reorder_level,
case
when (reorder_quantity + in_stock) > reorder_level
then reorder_quantity/29.5735
when (reorder_quantity + in_stock) = reorder_level
then reorder_quantity/(29.5735*2)
when (reorder_quantity + in_stock) < reorder_level
then reorder_quantity/(29.5735*2)
else 0
end as order_amount_oz
from inventory
If I understand correctly you can try to remove group by
and using parentheses
select itemnum, itemname, in_stock, reorder_level,
case
when (reorder_quantity + in_stock) > reorder_level
then reorder_quantity/29.5735
when (reorder_quantity + in_stock) = reorder_level
then reorder_quantity/(29.5735*2)
when (reorder_quantity + in_stock) < reorder_level
then reorder_quantity/(29.5735*2)
else 0
end as order_amount_oz
from inventory
answered Nov 13 at 0:39
D-Shih
24.1k61331
24.1k61331
btw you don't need the brackets around the additions
– Bohemian♦
Nov 13 at 0:45
add a comment |
btw you don't need the brackets around the additions
– Bohemian♦
Nov 13 at 0:45
btw you don't need the brackets around the additions
– Bohemian♦
Nov 13 at 0:45
btw you don't need the brackets around the additions
– Bohemian♦
Nov 13 at 0:45
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272005%2fcase-syntax-with-addition-and-greater-less-operators%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why did you need to use
group by
when you didn't use any aggregate function?– D-Shih
Nov 13 at 0:29
1
Could you provide some sample data and exepct result?
– D-Shih
Nov 13 at 0:30