SQL error
DBAsupport.com Forums - Powered by vBulletin
Results 1 to 10 of 11

Thread: SQL error

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    6

    SQL error

    My query islike below.

    SELECT DISTINCT CASE
    WHEN esc.x1 = Substr(inp.y, 0, 3)
    AND esc.x2 = Substr(inp.y, 4, 2)THEN esc.cc
    WHEN esc.mcc = Substr(inp.y, 0, 3)
    AND esc.mnc = Substr(inp.y, 4, 3)THEN esc.cc
    END
    FROM xxx inp,
    yyy esc
    WHERE CASE
    WHEN esc.x1 IN( Substr(inp.y, 0, 3) )
    AND esc.x2 IN( Substr(inp.y, 4, 2) )THEN esc.cc
    WHEN esc.x1 IN( Substr(inp.y, 0, 3) )
    AND esc.x2 IN( Substr(inp.y, 4, 3) )THEN esc.cc
    END IS NOT NULL

    Now the query is returning more than one row because it is satisfying both the case statement. what i want is if the case statement is NULL then only it should go for second one.Otherwise only the output of first case should be the output.Pls help me to modify the query accordingly.

    Thnx in advance.

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,554
    So... is the idea to return a row when either one of the two case conditions is true?
    If that's the case I would use two case statements joined by OR.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Jun 2011
    Posts
    6

    SQL error

    Hi thnx for your reply.

    Can you put it in the query what ever you have explained as it would be helpful.

    Thnx in advance.

  4. #4
    Join Date
    Jul 2002
    Location
    Lake Worth, FL
    Posts
    1,478

    Wink

    Try this:
    Code:
    SELECT DISTINCT
          CASE
              WHEN esc.x1 IN (SUBSTR (inp.y, 0, 3))
                   AND esc.x2 IN (SUBSTR (inp.y, 4, 2), SUBSTR (inp.y, 4, 3))
               THEN
                 esc.cc
              ELSE NULL
           END
      FROM xxx inp, yyy esc
     WHERE 
          CASE
              WHEN esc.x1 IN (SUBSTR (inp.y, 0, 3))
                   AND esc.x2 IN (SUBSTR (inp.y, 4, 2), SUBSTR (inp.y, 4, 3))
               THEN
                 esc.cc
              ELSE NULL
           END
              IS NOT NULL;
    "The person who says it cannot be done should not interrupt the person doing it." --Chinese Proverb

  5. #5
    Join Date
    Jun 2011
    Posts
    6

    SQL Error

    Thnx for your reply will try and let you know.

  6. #6
    Join Date
    Jun 2011
    Posts
    6

    SQL error

    Hi,

    The above query also takes both the CASE statements.

    Where as it should take only one case and if 1st case is NULL only then it should go for 2ns CASE

  7. #7
    Join Date
    Jul 2002
    Location
    Lake Worth, FL
    Posts
    1,478

    Angry

    Quote Originally Posted by wenowd View Post
    My query islike below.

    SELECT DISTINCT CASE
    WHEN esc.x1 = Substr(inp.y, 0, 3)
    AND esc.x2 = Substr(inp.y, 4, 2)THEN esc.cc
    WHEN esc.mcc = Substr(inp.y, 0, 3)
    AND esc.mnc = Substr(inp.y, 4, 3)THEN esc.cc
    END
    FROM xxx inp,
    yyy esc
    WHERE CASE
    WHEN esc.x1 IN( Substr(inp.y, 0, 3) )
    AND esc.x2 IN( Substr(inp.y, 4, 2) )THEN esc.cc
    WHEN esc.x1 IN( Substr(inp.y, 0, 3) )
    AND esc.x2 IN( Substr(inp.y, 4, 3) )THEN esc.cc
    END IS NOT NULL

    Now the query is returning more than one row because it is satisfying both the case statement. what i want is if the case statement is NULL then only it should go for second one.Otherwise only the output of first case should be the output.Pls help me to modify the query accordingly.

    Thnx in advance.
    Please clarify, I am only seeing ONE case statement with two "WHEN" conditions.
    What do you mean by: "what i want is if the case statement is NULL then only it should go for second one." which is the first and which is the second?
    "The person who says it cannot be done should not interrupt the person doing it." --Chinese Proverb

  8. #8
    Join Date
    Jun 2011
    Posts
    6
    Can you put those CASE statements with OR condition because i changed but still it going for both CASE statements.

    Thnx in advanvce

  9. #9
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,554
    Quote Originally Posted by wenowd View Post
    Can you put those CASE statements with OR condition because i changed but still it going for both CASE statements.
    Please show us what you did
    Please remember to wrap code into code tags (#)
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  10. #10
    Join Date
    Jun 2011
    Posts
    6
    Hi,

    Here is the below query i tried with OR condition

    #####################################

    SELECT DISTINCT CASE
    WHEN esc.x1 = Substr(inp.y, 0, 3)
    AND esc.x2 = Substr(inp.y, 4, 2)THEN esc.cc
    WHEN esc.mcc = Substr(inp.y, 0, 3)
    AND esc.mnc = Substr(inp.y, 4, 3)THEN esc.cc
    END
    FROM xxx inp,
    yyy esc
    where ( esc.x1=substr(inp.y,0,3) and esc.x2=substr(inp.y,4,2))
    or (esc.x1=substr(inp.y,0,3) and esc.x2=substr(inp.y,4,3))

    #############################################

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width