Help with SQL Query

  • I'm a newbie, I need help with sql query.

    The requirement is : To get data from table which has column which has values example x=1 or Yes

    Table:

    ID varname varvalue

    1 x 1

    2 y yes

    3 z no

    .

    .

    select all records of table if varname=x and varvalue=1 or 'yes'

    and varname=y and varvalue=2 or 'no'

    How do we translate above req into sql query

    Any help is greatly appreciated.

  • rtanda (10/16/2010)


    I'm a newbie, I need help with sql query.

    The requirement is : To get data from table which has column which has values example x=1 or Yes

    Table:

    ID varname varvalue

    1 x 1

    2 y yes

    3 z no

    .

    .

    select all records of table if varname=x and varvalue=1 or 'yes'

    and varname=y and varvalue=2 or 'no'

    How do we translate above req into sql query

    Any help is greatly appreciated.

    hi , from what description you gave this shall work.

    Declare @b-2 table

    (

    ID int,

    varname varchar(10),

    varvalue varchar(20)

    )

    insert into @b-2 values(1,'x','1')

    insert into @b-2 values(2,'y','yes')

    insert into @b-2 values (3,'z','no')

    insert into @b-2 values (4,'y','no')

    select * from @b-2 where (varname='x' and

    (varvalue='1' or varvalue='yes')) or (varname='y' and

    (varvalue='2' or varvalue='no'))

  • Sounds like a homework question.

    If you use the answer from above without quoting the source (therewith stating it's not your solution) it'll be considered as plagiarism. It's cheating anyway.

    Another story would have been if you had at least tried to solve it on your own, and if got stuck, ask for clarification/explanation together with what you've tried and what you need help with.

    @sayedkhalid99:

    Other professionals around (on different threads) did remove their solutions as soon as the homework issue has been brought up. You might want to do the same 😉



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Thanks for quick reply

  • Thanks for the quick reply sayedkhalid99

Viewing 5 posts - 1 through 4 (of 4 total)

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