conditional query

  • I have a table statementmaster in my SQL 2005 database with records as

    ID Serial Narration Reference

    1 700 1 7662626

    2 900 3232323

    3 700 2 32323233

    4 100 1 23233

    5 700 1 43434

    I am trying to write a Sql query for which the output should display all the records from the statementmaster table and when the 'Serial' column value is 700 then I need only those records where the 'Narration' column value is 1. So with the above example the query should fetch all the records except where ID value is 3 as the 'Serial' is 700 and the 'Narraton' is 2

    The script to create the table and data is given below

    create table statementmaster

    (

    ID int not null,

    Serial int not null,

    Narration int null,

    Reference int not null

    )

    Insert into statementmaster values (1,700,1,7662626)

    Insert into statementmaster values (2,900,,3232323)

    Insert into statementmaster values (3,700,2,32323233)

    Insert into statementmaster values (4,100,1,23233)

    Insert into statementmaster values (5,700,1,43434)

  • Looked like a home work, any way here you go...

    SELECT * FROM #statementmaster

    WHERE Serial ! = 700 or

    (Serial = 700 AND Narration = 1 )


    Bru Medishetty

    Blog -- LearnSQLWithBru

    Join on Facebook Page Facebook.comLearnSQLWithBru

    Twitter -- BruMedishetty

  • based on the available criteria:

    Here's another method

    SELECT ID,Serial,Narration,Reference

    FROM statementmaster

    Where isnull(Narration,1) = 1

    And Serial like (case when serial = '700' then '700' else '%' end)

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 3 posts - 1 through 2 (of 2 total)

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