Select only one register with condition

  • Hi Guys,

    I need a help for a query development that returns only one record according to the condition. But if condition is not true, query should select other value. for example:

    SELECT
      Field
    FROM TABLE
    WHERE Field = 'BRA'

    Key Search Field
    ______________
    |BRA     |     X     |
    |CHL     |     Y     |
    |USA     |     Z     |
    |_____________|

    SELECT
      Field
    FROM TABLE
    IF   Field = 'BRA' IS NULL
    THEN GET Field = 'CHL'
    ______________
    | NULL     |     X     |
    | CHL       |     Y     |
    | USA       |     Z     |
    |______________|

    Thank you

  • antoniop.silv - Monday, November 12, 2018 10:57 AM

    Hi Guys,

    I need a help for a query development that returns only one record according to the condition. But if condition is not true, query should select other value. for example:

    SELECT
      Field
    FROM TABLE
    WHERE Field = 'BRA'

    Key Search Field
    ______________
    |BRA     |     X     |
    |CHL     |     Y     |
    |USA     |     Z     |
    |_____________|

    SELECT
      Field
    FROM TABLE
    IF   Field = 'BRA' IS NULL
    THEN GET Field = 'CHL'
    ______________
    | NULL     |     X     |
    | CHL       |     Y     |
    | USA       |     Z     |
    |______________|

    Thank you

    Sorry I think you are not able to ask what you want may be due to language barrier.
    I think you need case expression:

    select field1,case when field2 is not null then field2  else 'CHL' end as field2
    from tablename

    Saravanan

  • saravanatn - Monday, November 12, 2018 11:24 AM

    antoniop.silv - Monday, November 12, 2018 10:57 AM

    Hi Guys,

    I need a help for a query development that returns only one record according to the condition. But if condition is not true, query should select other value. for example:

    SELECT
      Field
    FROM TABLE
    WHERE Field = 'BRA'

    Key Search Field
    ______________
    |BRA     |     X     |
    |CHL     |     Y     |
    |USA     |     Z     |
    |_____________|

    SELECT
      Field
    FROM TABLE
    IF   Field = 'BRA' IS NULL
    THEN GET Field = 'CHL'
    ______________
    | NULL     |     X     |
    | CHL       |     Y     |
    | USA       |     Z     |
    |______________|

    Thank you

    Sorry I think you are not able to ask what you want may be due to language barrier.
    I think you need case expression:

    select field1,case when field2 is not null then field2  else 'CHL' end as field2
    from tablename

    No, a case statement will not work, based upon the data provided. 

    What have you tried?  Can you post your code???  There is some ambiguity in what you are asking. 
    See the link in my signature about how to post questions.

    The question I have is this.  If a row contains 'BRA', then return it.  If it is null, then return the row that contains 'CHL'.
    Looking at your example, what happens if there are 10 million rows, and only some of them contain "BRA"? How to you determine what specific row that contains "CHL" is the one that matches up with these rows?
    Expanding upon your example dats:
    Here is what you provided
    | NULL | X |
    | CHL | Y |
    | USA | Z |

    What if you have this?
    NULL X
    CHL Y
    USA Z
    BRA A
    CHL M
    USA E
    NULL N
    CHL O
    USA D
    NULL F
    CHL G
    USA H

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • Michael L John - Monday, November 12, 2018 11:35 AM

    saravanatn - Monday, November 12, 2018 11:24 AM

    antoniop.silv - Monday, November 12, 2018 10:57 AM

    Hi Guys,

    I need a help for a query development that returns only one record according to the condition. But if condition is not true, query should select other value. for example:

    SELECT
      Field
    FROM TABLE
    WHERE Field = 'BRA'

    Key Search Field
    ______________
    |BRA     |     X     |
    |CHL     |     Y     |
    |USA     |     Z     |
    |_____________|

    SELECT
      Field
    FROM TABLE
    IF   Field = 'BRA' IS NULL
    THEN GET Field = 'CHL'
    ______________
    | NULL     |     X     |
    | CHL       |     Y     |
    | USA       |     Z     |
    |______________|

    Thank you

    Sorry I think you are not able to ask what you want may be due to language barrier.
    I think you need case expression:

    select field1,case when field2 is not null then field2  else 'CHL' end as field2
    from tablename

    No, a case statement will not work, based upon the data provided. 

    What have you tried?  Can you post your code???  There is some ambiguity in what you are asking. 
    See the link in my signature about how to post questions.

    The question I have is this.  If a row contains 'BRA', then return it.  If it is null, then return the row that contains 'CHL'.
    Looking at your example, what happens if there are 10 million rows, and only some of them contain "BRA"? How to you determine what specific row that contains "CHL" is the one that matches up with these rows?
    Expanding upon your example dats:
    Here is what you provided
    | NULL | X |
    | CHL | Y |
    | USA | Z |

    What if you have this?
    NULL X
    CHL Y
    USA Z
    BRA A
    CHL M
    USA E
    NULL N
    CHL O
    USA D
    NULL F
    CHL G
    USA H

    Thank for your feedback, It's only free cases BRA, CHL, USA.

    SELECT
    ( CASE
         WHEN [LANGUAGE] = 'PT-BR'
                THEN DESCRIPTION    
         ELSE
             CASE WHEN [LANGUAGE] = 'EN-US'     
             END
        END    
    )
    FROM TABLE

    I tried with code above, but syntax does not answer

  • saravanatn - Monday, November 12, 2018 11:24 AM

    antoniop.silv - Monday, November 12, 2018 10:57 AM

    Hi Guys,

    I need a help for a query development that returns only one record according to the condition. But if condition is not true, query should select other value. for example:

    SELECT
      Field
    FROM TABLE
    WHERE Field = 'BRA'

    Key Search Field
    ______________
    |BRA     |     X     |
    |CHL     |     Y     |
    |USA     |     Z     |
    |_____________|

    SELECT
      Field
    FROM TABLE
    IF   Field = 'BRA' IS NULL
    THEN GET Field = 'CHL'
    ______________
    | NULL     |     X     |
    | CHL       |     Y     |
    | USA       |     Z     |
    |______________|

    Thank you

    Sorry I think you are not able to ask what you want may be due to language barrier.
    I think you need case expression:

    select field1,case when field2 is not null then field2  else 'CHL' end as field2
    from tablename

    Saravanatn thank you for your feedback.

    I will try to explain better with examples.

    Thank you.

  • antoniop.silv - Monday, November 12, 2018 12:18 PM

    saravanatn - Monday, November 12, 2018 11:24 AM

    antoniop.silv - Monday, November 12, 2018 10:57 AM

    Hi Guys,

    I need a help for a query development that returns only one record according to the condition. But if condition is not true, query should select other value. for example:

    SELECT
      Field
    FROM TABLE
    WHERE Field = 'BRA'

    Key Search Field
    ______________
    |BRA     |     X     |
    |CHL     |     Y     |
    |USA     |     Z     |
    |_____________|

    SELECT
      Field
    FROM TABLE
    IF   Field = 'BRA' IS NULL
    THEN GET Field = 'CHL'
    ______________
    | NULL     |     X     |
    | CHL       |     Y     |
    | USA       |     Z     |
    |______________|

    Thank you

    Sorry I think you are not able to ask what you want may be due to language barrier.
    I think you need case expression:

    select field1,case when field2 is not null then field2  else 'CHL' end as field2
    from tablename

    Saravanatn thank you for your feedback.

    I will try to explain better with examples.

    Thank you.

    SELECT 
    ( CASE 
    WHEN [LANGUAGE] = 'PT-BR' 
    THEN DESCRIPTION 
    ELSE
    CASE WHEN [LANGUAGE] = 'EN-US' 
    END
    END 

    FROM TABLE

    I tried with code above, but syntax does not answer

  • Use "TOP (1)" to limit the results to one row.  For example:


    SELECT TOP (1) Field 
    FROM TABLE 
    ORDER BY Field

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • If you want them in a certain order:SELECT TOP (1) Field
    FROM myTable
    ORDER BY CASE WHEN Field='BRA' THEN 1
         WHEN Field='CHL' THEN 2
         WHEN Field='USA' THEN 3
        END

  • How about we consider the need for the result to NOT be NULL?
    SELECT TOP (1) Field
    FROM TABLE
    WHERE Field IS NOT NULL
    ORDER BY [Key];

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • Thank you guys,
    It worked that way:

    -- Create table
    CREATE TABLE #tbLanguages
    (
        id               INT                    NOT NULL IDENTITY(1,1),
        Parts         INT                    NOT NULL,
        Language VARCHAR(30)   NULL,
        Value        VARCHAR(30)   NULL
    )

    -- Insert datas in table
    INSERT INTO #tbLanguages
    (
        Parts,
        Language,
        Value
    )
    SELECT  100,'EN-US','CABLE'
    UNION SELECT 100,'ES-AR','8F'
    UNION SELECT 100,'PT-BR','8F CX CARTOLI'
    UNION SELECT 200,'EN-US','SALE'
    UNION SELECT 200,'ES-AR','VENTA'

    -- Query get one register
    SELECT top 1 *
    FROM #tbLanguages
     INNER JOIN  ( SELECT MAX(ID) LastParts
                                  FROM  #tbLanguages
                                  WHERE
                                        Parts = @Parts
                                  GROUP BY  Value ) Filter On Filter.LastParts = #tbLanguages.ID
                                  ORDER BY 
                                        CASE
                                             WHEN Language = 'PT-BR' then 1
                                             WHEN Language = 'EN-US' then 2
                                             WHEN Language = 'ES-AR' then 3
                                             ELSE 4
                                        END

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply