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 |
+------------+-----+------------+---------------+---------------+









share|improve this question




























    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 |
    +------------+-----+------------+---------------+---------------+









    share|improve this question


























      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 |
      +------------+-----+------------+---------------+---------------+









      share|improve this question















      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-server sql-server-2014






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 8:58









      Barbaros Özhan

      11.5k71530




      11.5k71530










      asked Nov 11 at 8:51









      Sam Bin Ham

      1469




      1469
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          From your logic you can try this




          1. first one use OR


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






          share|improve this answer























          • need to replace qty * LPO with qty *unit_price and ( OR LPO IS NULL )
            – Sam Bin Ham
            Nov 11 at 9:30


















          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






          share|improve this answer






























            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





            share|improve this answer





















              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%2f53247158%2fhow-to-use-nested-case-with-multiple-condition-in-sql%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              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




              1. first one use OR


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






              share|improve this answer























              • need to replace qty * LPO with qty *unit_price and ( OR LPO IS NULL )
                – Sam Bin Ham
                Nov 11 at 9:30















              up vote
              1
              down vote













              From your logic you can try this




              1. first one use OR


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






              share|improve this answer























              • need to replace qty * LPO with qty *unit_price and ( OR LPO IS NULL )
                – Sam Bin Ham
                Nov 11 at 9:30













              up vote
              1
              down vote










              up vote
              1
              down vote









              From your logic you can try this




              1. first one use OR


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






              share|improve this answer














              From your logic you can try this




              1. first one use OR


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







              share|improve this answer














              share|improve this answer



              share|improve this answer








              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


















              • 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












              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






              share|improve this answer



























                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






                share|improve this answer

























                  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






                  share|improve this answer














                  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







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 9:43

























                  answered Nov 11 at 9:28









                  Barbaros Özhan

                  11.5k71530




                  11.5k71530






















                      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





                      share|improve this answer

























                        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





                        share|improve this answer























                          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





                          share|improve this answer












                          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






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 11 at 12:23









                          masoud

                          11




                          11






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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

                              The Sandy Post

                              Danny Elfman

                              Pages that link to "Head v. Amoskeag Manufacturing Co."