Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Multiple Rows to Single Rows

Multiple Rows to Single Rows

Scheduled Pinned Locked Moved Database
database
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chris Meech
    wrote on last edited by
    #1

    I have a query of the form

    select * from (
    select ...
    ,...
    ,...
    ,a.value_1 open_value_1
    ,to_number(NULL) close_value_1
    from table a
    where a.date_col = date_1
    union all
    select ...
    ,...
    ,...
    ,to_number(NULL) open_value_1
    ,b.value_1 close_value_1
    from table b
    where b.date_col = date_2
    );

    If the list of columns represented by the ... is considered a primary key, for the situations where the primary keys are the same I end up with multiple rows

    AA BBB CCC open_value_1
    AA BBB CCC close_value_1

    How would I tailor my query to give me only one row instead of two rows. Like this

    AA BBB CCC open_value_1 close_value_1

    Thanks for any suggestions. :)

    Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

    D P A 3 Replies Last reply
    0
    • C Chris Meech

      I have a query of the form

      select * from (
      select ...
      ,...
      ,...
      ,a.value_1 open_value_1
      ,to_number(NULL) close_value_1
      from table a
      where a.date_col = date_1
      union all
      select ...
      ,...
      ,...
      ,to_number(NULL) open_value_1
      ,b.value_1 close_value_1
      from table b
      where b.date_col = date_2
      );

      If the list of columns represented by the ... is considered a primary key, for the situations where the primary keys are the same I end up with multiple rows

      AA BBB CCC open_value_1
      AA BBB CCC close_value_1

      How would I tailor my query to give me only one row instead of two rows. Like this

      AA BBB CCC open_value_1 close_value_1

      Thanks for any suggestions. :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      Look into Pivot queries. It looks like that may be what you want.

      1 Reply Last reply
      0
      • C Chris Meech

        I have a query of the form

        select * from (
        select ...
        ,...
        ,...
        ,a.value_1 open_value_1
        ,to_number(NULL) close_value_1
        from table a
        where a.date_col = date_1
        union all
        select ...
        ,...
        ,...
        ,to_number(NULL) open_value_1
        ,b.value_1 close_value_1
        from table b
        where b.date_col = date_2
        );

        If the list of columns represented by the ... is considered a primary key, for the situations where the primary keys are the same I end up with multiple rows

        AA BBB CCC open_value_1
        AA BBB CCC close_value_1

        How would I tailor my query to give me only one row instead of two rows. Like this

        AA BBB CCC open_value_1 close_value_1

        Thanks for any suggestions. :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        JOIN

        1 Reply Last reply
        0
        • C Chris Meech

          I have a query of the form

          select * from (
          select ...
          ,...
          ,...
          ,a.value_1 open_value_1
          ,to_number(NULL) close_value_1
          from table a
          where a.date_col = date_1
          union all
          select ...
          ,...
          ,...
          ,to_number(NULL) open_value_1
          ,b.value_1 close_value_1
          from table b
          where b.date_col = date_2
          );

          If the list of columns represented by the ... is considered a primary key, for the situations where the primary keys are the same I end up with multiple rows

          AA BBB CCC open_value_1
          AA BBB CCC close_value_1

          How would I tailor my query to give me only one row instead of two rows. Like this

          AA BBB CCC open_value_1 close_value_1

          Thanks for any suggestions. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

          A Offline
          A Offline
          Alegria_Lee
          wrote on last edited by
          #4

          SELECT ...,...,...,MAX(open_value_1),MAX(close_value_1) FROM
          (
          --YOUR SELECT QUERY
          )A
          GROUP BY ...,...,...

          B C 2 Replies Last reply
          0
          • A Alegria_Lee

            SELECT ...,...,...,MAX(open_value_1),MAX(close_value_1) FROM
            (
            --YOUR SELECT QUERY
            )A
            GROUP BY ...,...,...

            B Offline
            B Offline
            Blue_Boy
            wrote on last edited by
            #5

            :thumbsup:


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

            1 Reply Last reply
            0
            • A Alegria_Lee

              SELECT ...,...,...,MAX(open_value_1),MAX(close_value_1) FROM
              (
              --YOUR SELECT QUERY
              )A
              GROUP BY ...,...,...

              C Offline
              C Offline
              Chris Meech
              wrote on last edited by
              #6

              Perfect. Thanks very much. I knew I needed some grouping to happen and couldn't think of putting some function like max together. It works exactly as I needed it to. :)

              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

              A 1 Reply Last reply
              0
              • C Chris Meech

                Perfect. Thanks very much. I knew I needed some grouping to happen and couldn't think of putting some function like max together. It works exactly as I needed it to. :)

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

                A Offline
                A Offline
                Alegria_Lee
                wrote on last edited by
                #7

                :-D you are welcome

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups