Results 1 to 6 of 6

Thread: Complex SELECT (SOLVED)

  1. #1

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364

    Complex SELECT (SOLVED)

    Hi There
    take a look at the image below:

    well
    I need to select everything from table Pessoa where idUnidade from table Unidade be equal to 17

    if somebody understands my question please help me

    thanks in advance
    Attached Images Attached Images  
    Last edited by Daskalos; Apr 11th, 2005 at 04:27 PM.
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Complex SELECT

    Could be:

    Code:
    Select * From Pessoa
       Where idArea in (Select idArea From Area Where idUniDade=17)
    [edit] oops - wait a minute - that's not right - give me a second...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Complex SELECT

    Maybe this:

    Code:
    Select * From Pessoa
       Where idPessoa in (Select idPessoa From AreaPessoa
                                 Left Join Area on Area.idArea=AreaPessoa.idArea
                                 Where idUniDade=17)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364

    Re: Complex SELECT

    Quote Originally Posted by szlamany
    Maybe this:

    Code:
    Select * From Pessoa
       Where idPessoa in (Select idPessoa From AreaPessoa
                                 Left Join Area on Area.idArea=AreaPessoa.idArea
                                 Where idUniDade=17)
    hum.. it worked.. i after I saw you first post, i did this:
    Code:
    Select * From Pessoa
       Where idpessoa in (Select idpessoa From AreaPessoa Where idarea in
    (SELECT idarea FROM area where idunidade = 17))
    i don't know why, but i think you code is better
    so.. could you explain me how does this "Left Join" works??
    Last edited by Daskalos; Apr 11th, 2005 at 04:05 PM.
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Complex SELECT

    Not knowing much about the data in your tables - it's probably better yet to do this:

    Code:
    Select * From Pessoa
       Where idPessoa in (Select idPessoa From AreaPessoa
                                 Left Join Area on Area.idArea=AreaPessoa.idArea
                                 Where idUniDade=17
                                 Group by idPessoa)
    The GROUP BY will make sure that the "sub-query" recordset is as small as possible.

    The LEFT JOIN is creating a "single working table" from both the idPessoa and Area tables - all columns from both tables will be JOINED as if one table - on the criteria shown in the ON clause of the LEFT JOIN.

    I use LEFT JOIN instead of JOIN as a habit since we have so much legacy data that we've converted from mainframes. The relationships are not always clean. JOIN will throw away the "primary" table row (in the FROM clause) if the JOIN table sister row does not exist. LEFT JOIN will not do that - the "primary" table row comes through, but the columns from the JOINED table for that row will be NULL.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364

    Re: Complex SELECT

    thanks a lot ;-)
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

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