hey plz help me out

  • ========================

    Table structure

    ======================

    create table test (

    country int,

    zone int,

    branch int,

    user1 varchar(20),

    )

    i have two records

    country,zone ,branch ,user

    9 1 1 farooq

    9 1 1 jawwad

    i want this output

    country zone branch user user2

    9 1 1 farooq jawwad

    using self join

  • Sounds like homework. What have you tried and where are you getting stuck?

  • agree this sounds like homework... appreciate your resourcefulness. Hope your professor doesnt check sql server central.

    select a.country,a.zone,a.branch,a.user1 as 'user', b.user1 as user2

    from test a, test b

    where a.user1='farooq'

    and b.user1='jawwad'

  • You might want to read up in left join.

  • NJ-DBA (8/11/2011)


    select a.country,a.zone,a.branch,a.user1 as 'user', b.user1 as user2

    from test a, test b

    where a.user1='farooq'

    and b.user1='jawwad'

    Um, don't use this code. It's outdated, deprecated, and will disappear from SQL Server in the future without warning.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • ... which he would read about in books online.

  • Brandie Tarvin (8/11/2011)


    NJ-DBA (8/11/2011)


    select a.country,a.zone,a.branch,a.user1 as 'user', b.user1 as user2

    from test a, test b

    where a.user1='farooq'

    and b.user1='jawwad'

    Um, don't use this code. It's outdated, deprecated, and will disappear from SQL Server in the future without warning.

    It's not that it's deprecated, that syntax is not (you can't really deprecate a cross join, it's *= that's been removed). It's that it won't work properly if there are multiple matching rows in the tables. It also doesn't answer the question. The teacher wants a self-join (though no idea on what), that query uses a cross join.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • For the record, I thought it was deprecated and gave him that answer on purpose!... didnt realize though that it is not technically a "self join".

  • NJ-DBA (8/11/2011)


    For the record, I thought it was deprecated and gave him that answer on purpose!... didnt realize though that it is not technically a "self join".

    Don't feel bad, she has to tech edit a few of my replies more often that I'd like to admit! 😉

  • myquery

    =============

    select distinct a.country,a.zone,a.branch,a.user1,' ' as 'user2'

    from test a ,test b

    where

    a.country=b.country

    anda.zone=b.zone

    and a.branch=b.branch

    guys i have this two records of users of same country ,zone ,branch

    e.g

    9,1,1,farooq

    9,1,1,jawwad

    now i want ouput in one row

    e.g

    9,1,1,farooq,jawwad

    my above query is not working.... self joing is the solution but m nt getting the query ryt!!

  • hey guys i have figure it out.....and got the correct query thanx anyways 🙂

  • faruk.arshad (8/11/2011)


    hey guys i have figure it out.....and got the correct query thanx anyways 🙂

    Great, forum etiquette would have you post your solution so that others may learn. Also, you may also learn as others may show you other (perhaps better) alternatives to your solution.

Viewing 12 posts - 1 through 12 (of 12 total)

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