Results in Parallel columns

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vivekian

    Results in Parallel columns

    Hi ,

    I need to place the results of two different queries in the same result
    table parallel to each other.
    So if the result of the first query is

    1 12
    2 34
    3 45

    and the second query is

    1 34
    2 44
    3 98

    the results should be displayed as

    1 12 34
    2 34 44
    3 45 98

    If a union is done for both the queries , we get the results in rows.
    How can the above be done.

    Thanks in advance,
    vivekian

  • pietlinden@hotmail.com

    #2
    Re: Results in Parallel columns


    vivekian wrote:[color=blue]
    > Hi ,
    >
    > I need to place the results of two different queries in the same result
    > table parallel to each other.
    > So if the result of the first query is
    >
    > 1 12
    > 2 34
    > 3 45
    >
    > and the second query is
    >
    > 1 34
    > 2 44
    > 3 98
    >
    > the results should be displayed as
    >
    > 1 12 34
    > 2 34 44
    > 3 45 98
    >[/color]
    SELECT qry1.fld1 As FirstField, qry1.fld2 As SecondField, qry2.fild2 as
    ThirdField
    FROM qry1 INNER JOIN qry2 ON qry1.Field1=qry 2.fld1
    ORDER BY FirstField

    Comment

    • Gene Wirchenko

      #3
      Re: Results in Parallel columns

      On 14 Jun 2006 14:26:00 -0700, "vivekian" <vivekaseeja@gm ail.com>
      wrote:
      [color=blue]
      >I need to place the results of two different queries in the same result
      >table parallel to each other.
      >So if the result of the first query is
      >
      >1 12
      >2 34
      >3 45[/color]

      select ... into cursor one
      [color=blue]
      >and the second query is
      >
      >1 34
      >2 44
      >3 98[/color]

      select ... into cursor two
      [color=blue]
      >the results should be displayed as
      >
      >1 12 34
      >2 34 44
      >3 45 98[/color]

      select one.*,two.*
      from one full outer join two on one.pk=two.pk
      with suitable changes for DBMS and table and column names.
      [color=blue]
      >If a union is done for both the queries , we get the results in rows.
      >How can the above be done.[/color]

      Sincerely,

      Gene Wirchenko

      Comment

      • Mike C#

        #4
        Re: Results in Parallel columns

        CREATE TABLE #a (Idx INT NOT NULL PRIMARY KEY,
        Val INT NOT NULL)

        CREATE TABLE #b (Idx INT NOT NULL PRIMARY KEY,
        Val INT NOT NULL)

        INSERT INTO #a (Idx, Val)
        SELECT 1, 12
        UNION SELECT 2, 34
        UNION SELECT 3, 45

        INSERT INTO #b (Idx, Val)
        SELECT 1, 34
        UNION SELECT 2, 44
        UNION SELECT 3, 98

        SELECT a.Idx, a.Val, b.Val
        FROM #a a
        INNER JOIN #b b
        ON a.Idx = b.Idx
        ORDER BY a.Idx

        DROP TABLE #a
        DROP TABLE #b

        "vivekian" <vivekaseeja@gm ail.com> wrote in message
        news:1150320359 .977336.291500@ i40g2000cwc.goo glegroups.com.. .[color=blue]
        > Hi ,
        >
        > I need to place the results of two different queries in the same result
        > table parallel to each other.
        > So if the result of the first query is
        >
        > 1 12
        > 2 34
        > 3 45
        >
        > and the second query is
        >
        > 1 34
        > 2 44
        > 3 98
        >
        > the results should be displayed as
        >
        > 1 12 34
        > 2 34 44
        > 3 45 98
        >
        > If a union is done for both the queries , we get the results in rows.
        > How can the above be done.
        >
        > Thanks in advance,
        > vivekian
        >[/color]


        Comment

        • Larry Linson

          #5
          Re: Results in Parallel columns

          If I understand the question, my vote goes to Piet's solution. If you have
          unmatched rows in either Query, then you'd need something a bit different.

          Larry Linson
          Microsoft Access MVP

          "vivekian" <vivekaseeja@gm ail.com> wrote in message
          news:1150320359 .977336.291500@ i40g2000cwc.goo glegroups.com.. .[color=blue]
          > Hi ,
          >
          > I need to place the results of two different queries in the same result
          > table parallel to each other.
          > So if the result of the first query is
          >
          > 1 12
          > 2 34
          > 3 45
          >
          > and the second query is
          >
          > 1 34
          > 2 44
          > 3 98
          >
          > the results should be displayed as
          >
          > 1 12 34
          > 2 34 44
          > 3 45 98
          >
          > If a union is done for both the queries , we get the results in rows.
          > How can the above be done.
          >
          > Thanks in advance,
          > vivekian
          >[/color]


          Comment

          • pietlinden@hotmail.com

            #6
            Re: Results in Parallel columns

            Hang on a minute... I guess my answer was a bit premature before. What
            database are you using to do this? If you're using Oracle, you can use
            a full outer join and PL/SQL to do it.

            If you're using Access, it's a different kettle of fish, because Access
            doesn't support full outer joins.

            So end the suspense. What database are you doing this in? (Or just
            target your post to the relevant database group). The answer will
            largely depend on that, since Oracle, Access, SQL Server, and DB2 all
            have their own implementations of the SQL92 standard...

            Comment

            • vivekian

              #7
              Re: Results in Parallel columns


              pietlinden@hotm ail.com wrote:[color=blue]
              > Hang on a minute... I guess my answer was a bit premature before. What
              > database are you using to do this?[/color]

              using SQL Server

              thanks,
              vivekian

              Comment

              • -CELKO-

                #8
                Re: Results in Parallel columns

                >>I need to place the results of two different queries in the same result table parallel to each other. <<

                This is not a table; the rows of a table model elements of a set of the
                same kind of thing. What you want is a display kludge to show, say,
                automobiles and squid as if they were the same kind of things.

                Here is your kludge, since people here often gripe that I do not post
                the bad code the OP wants:

                SELECT *, ROW_NUMBER() OVER(ORDER BY duh) AS lft_nbr
                FROM Foo
                FULL OUTER JOIN
                SELECT *, ROW_NUMBER() OVER(ORDER BY doh) AS rgt_nbr
                FROM Bar
                ON Foo.lft_nbr = Bar.rgt_nbr;

                The right way is handle display issues in the applications and front
                ends, not the RDBMS.

                Comment

                Working...