DBAsupport.com Forums - Powered by vBulletin
Results 1 to 8 of 8

Thread: multiple values query

  1. #1
    Join Date
    Sep 2004
    Posts
    24

    multiple values query

    Hi!

    Can you please give me a hint for solving this problem:

    I have a table with multiple values - e.g.

    id |lastname| mother|father|child
    007 Maier Klara
    007 Maier Paul
    007 Maier Fritzchen
    008 Schmidt Anna
    008 Schmidt Heinz
    008 Schmidt Petra


    and the result should look like this:

    ID lastname mother father child
    007 Maier Klara Paul Fritzchen
    008 Schmidt Anna Heinz Petra

    thanks for your help.
    Petra.

  2. #2
    Join Date
    Jun 2005
    Location
    Calgary, Alberta, Canada
    Posts
    9
    Check out CONNECT BY, there might be a way with that.

  3. #3
    Join Date
    Nov 2002
    Location
    Geneva Switzerland
    Posts
    3,142
    I think you have a fundamental design problem here:
    With the table as it is you have NO WAY of ensuring that mother/father/child are retrieved in the required order.

    To get the required layout, search in this forum (or AskTom) for "Pivot".
    "The power of instruction is seldom of much efficacy except in those happy dispositions where it is almost superfluous" - Gibbon, quoted by R.P.Feynman

  4. #4
    Join Date
    Feb 2005
    Posts
    158
    I think you are suffering from formatting

    If
    id |lastname| mother|father|child
    007 Maier Klara
    007 Maier Paul
    007 Maier Fritzchen
    008 Schmidt Anna
    008 Schmidt Heinz
    008 Schmidt Petra

    Is actually
    id.....|lastname| mother|father|child
    007..|Maier...|Klara..|......|.........|
    007..|Maier...|.......|Paul..|.........|
    007..|Maier...|.......|......|Fritzchen|
    008..|Schmidt.|Anna...|......|.........|
    008..|Schmidt.|.......|Heinz.|.........|
    008..|Schmidt.|.......|......|Petra....|

    Then
    select id, max(mother), max(father), max(child)
    ....
    group by id
    should work

    (Assuming each id only has one mother, father and child)

  5. #5
    Join Date
    Jun 2005
    Location
    London, UK
    Posts
    159
    Using "view source" in the browser and interpreting the tab characters, it seems to be something like this, as gamyers suggested:

    Code:
    ID     Lastname   Mother    Father    Child
    ------ ---------- --------- --------- ------------
    007    Maier      Klara
    007    Maier                Paul
    007    Maier                          Fritzchen
    008    Schmidt    Anna
    008    Schmidt                        Heinz
    008    Schmidt                        Petra
    It's a bit easier to read without the pipes and dots

  6. #6
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,684
    Hi.

    I would definitely go with gamyers approach if the number of children were fixed. If they can be variable I would probably use some string aggregation to return the data like:

    http://www.oracle-base.com/articles/...Techniques.php

    Of course, there could be multiple mothers and fathers if you were talking about legal stuff, rather than biological

    Cheers

    Tim...
    Tim...
    OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
    OCA PL/SQL Developer
    Oracle ACE Director
    My website: oracle-base.com
    My blog: oracle-base.com/blog

  7. #7
    Join Date
    Sep 2004
    Posts
    24
    Thanks for your answers.

    sorry, for the bad formatting, but it looks like WilliamR.

    PHP Code:
    ID     Lastname   Mother    Father    Child
    ------ ---------- --------- --------- ------------
    007    Maier      Klara
    007    Maier                      Paul
    007    Maier                                Fritzchen
    008    Schmidt    Anna
    008    Schmidt                 Heinz
    008    Schmidt                               Petra 
    But it's possible that there are more than 1 children.

    This kind of table structure is made from an external software.
    For a special report I need to restructure the records.

    thx for the help.

  8. #8
    Join Date
    Aug 2002
    Location
    Colorado Springs
    Posts
    5,253
    If you have multiple children then ...
    Code:
    select id, lastname,mother,father,child
    from
    (
    select id,lastname,max(mother) over (id) mother,
    max(father) over (id) father,child
    from ...
    )
    where child is not null
    David Aldridge,
    "The Oracle Sponge"

    Senior Manager, Business Intelligence Development
    XM Satellite Radio
    Washington, DC

    Oracle ACE

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