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









share|improve this question






















  • 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















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









share|improve this question






















  • 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













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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 at 0:18









Yofi

154




154












  • 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


















  • 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
















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












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You're mixing the two types of case syntax:




  1. case expression when value1 then output1 when value2 then output2 else output3 end

  2. 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





share|improve this answer























  • That worked, thank you!
    – Yofi
    Nov 13 at 22:48


















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





share|improve this answer





















  • btw you don't need the brackets around the additions
    – Bohemian
    Nov 13 at 0:45











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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:




  1. case expression when value1 then output1 when value2 then output2 else output3 end

  2. 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





share|improve this answer























  • That worked, thank you!
    – Yofi
    Nov 13 at 22:48















up vote
1
down vote



accepted










You're mixing the two types of case syntax:




  1. case expression when value1 then output1 when value2 then output2 else output3 end

  2. 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





share|improve this answer























  • That worked, thank you!
    – Yofi
    Nov 13 at 22:48













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You're mixing the two types of case syntax:




  1. case expression when value1 then output1 when value2 then output2 else output3 end

  2. 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





share|improve this answer














You're mixing the two types of case syntax:




  1. case expression when value1 then output1 when value2 then output2 else output3 end

  2. 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






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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












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





share|improve this answer





















  • btw you don't need the brackets around the additions
    – Bohemian
    Nov 13 at 0:45















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





share|improve this answer





















  • btw you don't need the brackets around the additions
    – Bohemian
    Nov 13 at 0:45













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?