how to use Nested Case with multiple condition in SQL
up vote
1
down vote
favorite
How to modify the below sql script to calculate following condition
Total_Inc_Tax will be qty x unit_price with 5% when the Issue_date between 1-Jan-2018 and 31-Mar-2018 if LPO NULL or not
After 31-Mar-2018 when LPO not NULL shouldn't add 5 % with qty x unit_price other wise add 5%.
How it works with nested case or suggest any other way.
My_Table:
+------------+-----+------------+---------------+
| Issue_date | qty | unit_price | LPO |
+------------+-----+------------+---------------+
| 10-Jan-18 | 1 | 42 | 1-2018-001166 |
| 12-Jan-18 | 1 | 100 | NULL |
| 20-Sep-18 | 1 | 25 | NULL |
| 15-Oct-18 | 2 | 12 | 1-2018-002233 |
| 20-Oct-18 | 1 | 100 | 1-2018-002233 |
+------------+-----+------------+---------------+
SELECT Qty,unit_price,LPO,
case
when issue_date <= '2018-03-31' and issue_date >= '2018-01-01'
then (((qty) *(unit_price)) * 1.05 )
else
(((qty) *(unit_price)) * 1.05 )
end as Tot_inc_Tax
from My_Table
Expected Result
+------------+-----+------------+---------------+---------------+
| Issue_date | qty | unit_price | LPO | Total_Inc_Tax |
+------------+-----+------------+---------------+---------------+
| 10-Jan-18 | 1 | 42 | 1-2018-001166 | 44.1 |
| 12-Jan-18 | 1 | 100 | NULL | 105 |
| 20-Sep-18 | 1 | 25 | NULL | 26.25 |
| 15-Oct-18 | 2 | 12 | 1-2018-002233 | 24 |
| 20-Oct-18 | 1 | 100 | 1-2018-002233 | 100 |
+------------+-----+------------+---------------+---------------+
sql
add a comment |
up vote
1
down vote
favorite
How to modify the below sql script to calculate following condition
Total_Inc_Tax will be qty x unit_price with 5% when the Issue_date between 1-Jan-2018 and 31-Mar-2018 if LPO NULL or not
After 31-Mar-2018 when LPO not NULL shouldn't add 5 % with qty x unit_price other wise add 5%.
How it works with nested case or suggest any other way.
My_Table:
+------------+-----+------------+---------------+
| Issue_date | qty | unit_price | LPO |
+------------+-----+------------+---------------+
| 10-Jan-18 | 1 | 42 | 1-2018-001166 |
| 12-Jan-18 | 1 | 100 | NULL |
| 20-Sep-18 | 1 | 25 | NULL |
| 15-Oct-18 | 2 | 12 | 1-2018-002233 |
| 20-Oct-18 | 1 | 100 | 1-2018-002233 |
+------------+-----+------------+---------------+
SELECT Qty,unit_price,LPO,
case
when issue_date <= '2018-03-31' and issue_date >= '2018-01-01'
then (((qty) *(unit_price)) * 1.05 )
else
(((qty) *(unit_price)) * 1.05 )
end as Tot_inc_Tax
from My_Table
Expected Result
+------------+-----+------------+---------------+---------------+
| Issue_date | qty | unit_price | LPO | Total_Inc_Tax |
+------------+-----+------------+---------------+---------------+
| 10-Jan-18 | 1 | 42 | 1-2018-001166 | 44.1 |
| 12-Jan-18 | 1 | 100 | NULL | 105 |
| 20-Sep-18 | 1 | 25 | NULL | 26.25 |
| 15-Oct-18 | 2 | 12 | 1-2018-002233 | 24 |
| 20-Oct-18 | 1 | 100 | 1-2018-002233 | 100 |
+------------+-----+------------+---------------+---------------+
sql
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How to modify the below sql script to calculate following condition
Total_Inc_Tax will be qty x unit_price with 5% when the Issue_date between 1-Jan-2018 and 31-Mar-2018 if LPO NULL or not
After 31-Mar-2018 when LPO not NULL shouldn't add 5 % with qty x unit_price other wise add 5%.
How it works with nested case or suggest any other way.
My_Table:
+------------+-----+------------+---------------+
| Issue_date | qty | unit_price | LPO |
+------------+-----+------------+---------------+
| 10-Jan-18 | 1 | 42 | 1-2018-001166 |
| 12-Jan-18 | 1 | 100 | NULL |
| 20-Sep-18 | 1 | 25 | NULL |
| 15-Oct-18 | 2 | 12 | 1-2018-002233 |
| 20-Oct-18 | 1 | 100 | 1-2018-002233 |
+------------+-----+------------+---------------+
SELECT Qty,unit_price,LPO,
case
when issue_date <= '2018-03-31' and issue_date >= '2018-01-01'
then (((qty) *(unit_price)) * 1.05 )
else
(((qty) *(unit_price)) * 1.05 )
end as Tot_inc_Tax
from My_Table
Expected Result
+------------+-----+------------+---------------+---------------+
| Issue_date | qty | unit_price | LPO | Total_Inc_Tax |
+------------+-----+------------+---------------+---------------+
| 10-Jan-18 | 1 | 42 | 1-2018-001166 | 44.1 |
| 12-Jan-18 | 1 | 100 | NULL | 105 |
| 20-Sep-18 | 1 | 25 | NULL | 26.25 |
| 15-Oct-18 | 2 | 12 | 1-2018-002233 | 24 |
| 20-Oct-18 | 1 | 100 | 1-2018-002233 | 100 |
+------------+-----+------------+---------------+---------------+
sql
How to modify the below sql script to calculate following condition
Total_Inc_Tax will be qty x unit_price with 5% when the Issue_date between 1-Jan-2018 and 31-Mar-2018 if LPO NULL or not
After 31-Mar-2018 when LPO not NULL shouldn't add 5 % with qty x unit_price other wise add 5%.
How it works with nested case or suggest any other way.
My_Table:
+------------+-----+------------+---------------+
| Issue_date | qty | unit_price | LPO |
+------------+-----+------------+---------------+
| 10-Jan-18 | 1 | 42 | 1-2018-001166 |
| 12-Jan-18 | 1 | 100 | NULL |
| 20-Sep-18 | 1 | 25 | NULL |
| 15-Oct-18 | 2 | 12 | 1-2018-002233 |
| 20-Oct-18 | 1 | 100 | 1-2018-002233 |
+------------+-----+------------+---------------+
SELECT Qty,unit_price,LPO,
case
when issue_date <= '2018-03-31' and issue_date >= '2018-01-01'
then (((qty) *(unit_price)) * 1.05 )
else
(((qty) *(unit_price)) * 1.05 )
end as Tot_inc_Tax
from My_Table
Expected Result
+------------+-----+------------+---------------+---------------+
| Issue_date | qty | unit_price | LPO | Total_Inc_Tax |
+------------+-----+------------+---------------+---------------+
| 10-Jan-18 | 1 | 42 | 1-2018-001166 | 44.1 |
| 12-Jan-18 | 1 | 100 | NULL | 105 |
| 20-Sep-18 | 1 | 25 | NULL | 26.25 |
| 15-Oct-18 | 2 | 12 | 1-2018-002233 | 24 |
| 20-Oct-18 | 1 | 100 | 1-2018-002233 | 100 |
+------------+-----+------------+---------------+---------------+
sql
sql
edited Nov 11 at 8:58
Barbaros Özhan
11.5k71530
11.5k71530
asked Nov 11 at 8:51
Sam Bin Ham
1469
1469
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
From your logic you can try this
first one use
ORsecond one use
AND
And make sure all is match with your logic.
SELECT Issue_date,Qty,unit_price,LPO,
case
when (issue_date BETWEEN '2018-01-01' AND '2018-06-30') OR unit_price IS NULL
THEN qty *LPO * 1.05
when issue_date > '2018-03-31' AND unit_price IS NOT NULL
THEN qty * LPO END 'Total_Inc_Tax'
from My_Table
sqlfiddle
need to replace qty * LPO with qty *unit_price and ( OR LPO IS NULL )
– Sam Bin Ham
Nov 11 at 9:30
add a comment |
up vote
1
down vote
One calculation option would be :
with my_table(Issue_date, qty, unit_price, LPO) as
(
select '2018-01-10',1,42 ,'1-2018-001166' union all
select '2018-01-12',1,100,NULL union all
select '2018-09-20',1,25 ,NULL union all
select '2018-10-15',2,12 ,'1-2018-002233' union all
select '2018-10-20',1,100,'1-2018-002233'
)
select Qty,unit_price,LPO,
(case
when issue_date between '2018-01-01' and '2018-03-31'
then (((qty) *(unit_price)) * 1.05 )
when issue_date > '2018-03-31' then
( case when lpo is null then
(((qty) *(unit_price)) * 1.05 )
else
(((qty) *(unit_price)))
end )
end ) as Tot_inc_Tax
from My_Table;
dbfiddle demo
add a comment |
up vote
0
down vote
you can use this :
SELECT Qty,unit_price,LPO,
case
when issue_date between '2018-01-01' and '2018-03-31'
then (((qty) *(unit_price)) * 1.05 )
else when issue_date > '2018-03-31' and LPO is null
(((qty) *(unit_price)) * 1.05 )
end as Tot_inc_Tax
from My_Table
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
From your logic you can try this
first one use
ORsecond one use
AND
And make sure all is match with your logic.
SELECT Issue_date,Qty,unit_price,LPO,
case
when (issue_date BETWEEN '2018-01-01' AND '2018-06-30') OR unit_price IS NULL
THEN qty *LPO * 1.05
when issue_date > '2018-03-31' AND unit_price IS NOT NULL
THEN qty * LPO END 'Total_Inc_Tax'
from My_Table
sqlfiddle
need to replace qty * LPO with qty *unit_price and ( OR LPO IS NULL )
– Sam Bin Ham
Nov 11 at 9:30
add a comment |
up vote
1
down vote
From your logic you can try this
first one use
ORsecond one use
AND
And make sure all is match with your logic.
SELECT Issue_date,Qty,unit_price,LPO,
case
when (issue_date BETWEEN '2018-01-01' AND '2018-06-30') OR unit_price IS NULL
THEN qty *LPO * 1.05
when issue_date > '2018-03-31' AND unit_price IS NOT NULL
THEN qty * LPO END 'Total_Inc_Tax'
from My_Table
sqlfiddle
need to replace qty * LPO with qty *unit_price and ( OR LPO IS NULL )
– Sam Bin Ham
Nov 11 at 9:30
add a comment |
up vote
1
down vote
up vote
1
down vote
From your logic you can try this
first one use
ORsecond one use
AND
And make sure all is match with your logic.
SELECT Issue_date,Qty,unit_price,LPO,
case
when (issue_date BETWEEN '2018-01-01' AND '2018-06-30') OR unit_price IS NULL
THEN qty *LPO * 1.05
when issue_date > '2018-03-31' AND unit_price IS NOT NULL
THEN qty * LPO END 'Total_Inc_Tax'
from My_Table
sqlfiddle
From your logic you can try this
first one use
ORsecond one use
AND
And make sure all is match with your logic.
SELECT Issue_date,Qty,unit_price,LPO,
case
when (issue_date BETWEEN '2018-01-01' AND '2018-06-30') OR unit_price IS NULL
THEN qty *LPO * 1.05
when issue_date > '2018-03-31' AND unit_price IS NOT NULL
THEN qty * LPO END 'Total_Inc_Tax'
from My_Table
sqlfiddle
edited Nov 11 at 9:18
answered Nov 11 at 9:02
D-Shih
24.6k61431
24.6k61431
need to replace qty * LPO with qty *unit_price and ( OR LPO IS NULL )
– Sam Bin Ham
Nov 11 at 9:30
add a comment |
need to replace qty * LPO with qty *unit_price and ( OR LPO IS NULL )
– Sam Bin Ham
Nov 11 at 9:30
need to replace qty * LPO with qty *unit_price and ( OR LPO IS NULL )
– Sam Bin Ham
Nov 11 at 9:30
need to replace qty * LPO with qty *unit_price and ( OR LPO IS NULL )
– Sam Bin Ham
Nov 11 at 9:30
add a comment |
up vote
1
down vote
One calculation option would be :
with my_table(Issue_date, qty, unit_price, LPO) as
(
select '2018-01-10',1,42 ,'1-2018-001166' union all
select '2018-01-12',1,100,NULL union all
select '2018-09-20',1,25 ,NULL union all
select '2018-10-15',2,12 ,'1-2018-002233' union all
select '2018-10-20',1,100,'1-2018-002233'
)
select Qty,unit_price,LPO,
(case
when issue_date between '2018-01-01' and '2018-03-31'
then (((qty) *(unit_price)) * 1.05 )
when issue_date > '2018-03-31' then
( case when lpo is null then
(((qty) *(unit_price)) * 1.05 )
else
(((qty) *(unit_price)))
end )
end ) as Tot_inc_Tax
from My_Table;
dbfiddle demo
add a comment |
up vote
1
down vote
One calculation option would be :
with my_table(Issue_date, qty, unit_price, LPO) as
(
select '2018-01-10',1,42 ,'1-2018-001166' union all
select '2018-01-12',1,100,NULL union all
select '2018-09-20',1,25 ,NULL union all
select '2018-10-15',2,12 ,'1-2018-002233' union all
select '2018-10-20',1,100,'1-2018-002233'
)
select Qty,unit_price,LPO,
(case
when issue_date between '2018-01-01' and '2018-03-31'
then (((qty) *(unit_price)) * 1.05 )
when issue_date > '2018-03-31' then
( case when lpo is null then
(((qty) *(unit_price)) * 1.05 )
else
(((qty) *(unit_price)))
end )
end ) as Tot_inc_Tax
from My_Table;
dbfiddle demo
add a comment |
up vote
1
down vote
up vote
1
down vote
One calculation option would be :
with my_table(Issue_date, qty, unit_price, LPO) as
(
select '2018-01-10',1,42 ,'1-2018-001166' union all
select '2018-01-12',1,100,NULL union all
select '2018-09-20',1,25 ,NULL union all
select '2018-10-15',2,12 ,'1-2018-002233' union all
select '2018-10-20',1,100,'1-2018-002233'
)
select Qty,unit_price,LPO,
(case
when issue_date between '2018-01-01' and '2018-03-31'
then (((qty) *(unit_price)) * 1.05 )
when issue_date > '2018-03-31' then
( case when lpo is null then
(((qty) *(unit_price)) * 1.05 )
else
(((qty) *(unit_price)))
end )
end ) as Tot_inc_Tax
from My_Table;
dbfiddle demo
One calculation option would be :
with my_table(Issue_date, qty, unit_price, LPO) as
(
select '2018-01-10',1,42 ,'1-2018-001166' union all
select '2018-01-12',1,100,NULL union all
select '2018-09-20',1,25 ,NULL union all
select '2018-10-15',2,12 ,'1-2018-002233' union all
select '2018-10-20',1,100,'1-2018-002233'
)
select Qty,unit_price,LPO,
(case
when issue_date between '2018-01-01' and '2018-03-31'
then (((qty) *(unit_price)) * 1.05 )
when issue_date > '2018-03-31' then
( case when lpo is null then
(((qty) *(unit_price)) * 1.05 )
else
(((qty) *(unit_price)))
end )
end ) as Tot_inc_Tax
from My_Table;
dbfiddle demo
edited Nov 11 at 9:43
answered Nov 11 at 9:28
Barbaros Özhan
11.5k71530
11.5k71530
add a comment |
add a comment |
up vote
0
down vote
you can use this :
SELECT Qty,unit_price,LPO,
case
when issue_date between '2018-01-01' and '2018-03-31'
then (((qty) *(unit_price)) * 1.05 )
else when issue_date > '2018-03-31' and LPO is null
(((qty) *(unit_price)) * 1.05 )
end as Tot_inc_Tax
from My_Table
add a comment |
up vote
0
down vote
you can use this :
SELECT Qty,unit_price,LPO,
case
when issue_date between '2018-01-01' and '2018-03-31'
then (((qty) *(unit_price)) * 1.05 )
else when issue_date > '2018-03-31' and LPO is null
(((qty) *(unit_price)) * 1.05 )
end as Tot_inc_Tax
from My_Table
add a comment |
up vote
0
down vote
up vote
0
down vote
you can use this :
SELECT Qty,unit_price,LPO,
case
when issue_date between '2018-01-01' and '2018-03-31'
then (((qty) *(unit_price)) * 1.05 )
else when issue_date > '2018-03-31' and LPO is null
(((qty) *(unit_price)) * 1.05 )
end as Tot_inc_Tax
from My_Table
you can use this :
SELECT Qty,unit_price,LPO,
case
when issue_date between '2018-01-01' and '2018-03-31'
then (((qty) *(unit_price)) * 1.05 )
else when issue_date > '2018-03-31' and LPO is null
(((qty) *(unit_price)) * 1.05 )
end as Tot_inc_Tax
from My_Table
answered Nov 11 at 12:23
masoud
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53247158%2fhow-to-use-nested-case-with-multiple-condition-in-sql%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