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. Merging rows

Merging rows

Scheduled Pinned Locked Moved Database
tutorialdatabasehelpquestion
7 Posts 3 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.
  • J Offline
    J Offline
    jonatan_556
    wrote on last edited by
    #1

    Hi everyone, First of all sorry for any mistake I might give as English it is not my natural language. My issues is: I built a query where I had to join 8 tables. The query should only be returning 4 rows but instead it is returning 8 rows as one of the tables I'm using in the join's has a field type to identify , for example if it is source and destination(0 or 1). The final result should return only one row with either with source and destination information base on the type. Two different columns (source,destination) but same row. Any ideas how to solve this? I appreciate any help. Many thanks.

    M Richard DeemingR 2 Replies Last reply
    0
    • J jonatan_556

      Hi everyone, First of all sorry for any mistake I might give as English it is not my natural language. My issues is: I built a query where I had to join 8 tables. The query should only be returning 4 rows but instead it is returning 8 rows as one of the tables I'm using in the join's has a field type to identify , for example if it is source and destination(0 or 1). The final result should return only one row with either with source and destination information base on the type. Two different columns (source,destination) but same row. Any ideas how to solve this? I appreciate any help. Many thanks.

      M Offline
      M Offline
      Michael_Davies
      wrote on last edited by
      #2

      My English is bad and I'm English but my mind reading is atrocious. - Show the SQL

      J 1 Reply Last reply
      0
      • J jonatan_556

        Hi everyone, First of all sorry for any mistake I might give as English it is not my natural language. My issues is: I built a query where I had to join 8 tables. The query should only be returning 4 rows but instead it is returning 8 rows as one of the tables I'm using in the join's has a field type to identify , for example if it is source and destination(0 or 1). The final result should return only one row with either with source and destination information base on the type. Two different columns (source,destination) but same row. Any ideas how to solve this? I appreciate any help. Many thanks.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Can you recreate the problem in a simple example on SQL Fiddle[^]? Without seeing your table structure, query, sample data, and expected output, it's almost impossible to answer your question.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        J 1 Reply Last reply
        0
        • M Michael_Davies

          My English is bad and I'm English but my mind reading is atrocious. - Show the SQL

          J Offline
          J Offline
          jonatan_556
          wrote on last edited by
          #4

          Hi Michael, Thanks for replying. Please find below a sample of the SQL. It is not the real script but reflects the scenario I tried to describe. "select shipId,number,reference,[weight],SA.typeOfAddress,addressLine as Source, addressLine as Destination FROM Shipments AS S INNER JOIN ShipmentAddresses AS SA ON SA.shipmentId = S.shipId INNER JOIN Addresses as A ON A.addressId = SA.addressId" This query is returning 2 rows: shipId | number | reference | weight | typeAddress| Source | Destination | 1 1 ref1 12 0 London Road London Road 1 1 ref1 12 1 LiverPool Lliverpool I would like a single row: shipId | number | reference | weight | Source | Destination | 1 1 ref1 12 London Road LiverPool Note: the type of address in the first example of results was just to indicate that 0 is source and 1 is destination. Many thanks for the help.

          Richard DeemingR 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Can you recreate the problem in a simple example on SQL Fiddle[^]? Without seeing your table structure, query, sample data, and expected output, it's almost impossible to answer your question.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            J Offline
            J Offline
            jonatan_556
            wrote on last edited by
            #5

            Hi Richard, Hope you are alright and thanks for replying. Could you please see my above answer to Michael? Any help will be appreciated. Many thanks.

            1 Reply Last reply
            0
            • J jonatan_556

              Hi Michael, Thanks for replying. Please find below a sample of the SQL. It is not the real script but reflects the scenario I tried to describe. "select shipId,number,reference,[weight],SA.typeOfAddress,addressLine as Source, addressLine as Destination FROM Shipments AS S INNER JOIN ShipmentAddresses AS SA ON SA.shipmentId = S.shipId INNER JOIN Addresses as A ON A.addressId = SA.addressId" This query is returning 2 rows: shipId | number | reference | weight | typeAddress| Source | Destination | 1 1 ref1 12 0 London Road London Road 1 1 ref1 12 1 LiverPool Lliverpool I would like a single row: shipId | number | reference | weight | Source | Destination | 1 1 ref1 12 London Road LiverPool Note: the type of address in the first example of results was just to indicate that 0 is source and 1 is destination. Many thanks for the help.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Try something like this:

              SELECT
              S.shipId,
              S.number,
              S.reference,
              S.[weight],
              SA.addressLine As Source,
              DA.addressLine As Destination
              FROM
              Shipments As S

              -- Source address:
              INNER JOIN ShipmentAddresses As SSA
              ON SSA.shipmentId = S.shipId And SSA.typeOfAddress = 0
              INNER JOIN Addresses As SA
              ON SA.addressId = SSA.addressId
              
              -- Destination address
              INNER JOIN ShipmentAddresses As DSA
              ON DSA.shipmentId = S.shipId And DSA.typeOfAddress = 1
              INNER JOIN Addresses As DA
              ON DA.addressId = DSA.addressId
              

              ;


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              J 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Try something like this:

                SELECT
                S.shipId,
                S.number,
                S.reference,
                S.[weight],
                SA.addressLine As Source,
                DA.addressLine As Destination
                FROM
                Shipments As S

                -- Source address:
                INNER JOIN ShipmentAddresses As SSA
                ON SSA.shipmentId = S.shipId And SSA.typeOfAddress = 0
                INNER JOIN Addresses As SA
                ON SA.addressId = SSA.addressId
                
                -- Destination address
                INNER JOIN ShipmentAddresses As DSA
                ON DSA.shipmentId = S.shipId And DSA.typeOfAddress = 1
                INNER JOIN Addresses As DA
                ON DA.addressId = DSA.addressId
                

                ;


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                J Offline
                J Offline
                jonatan_556
                wrote on last edited by
                #7

                Hi Richard, Sorry the delay in answering. the strategy you described above worked perfectly on my real production scenario. Many thanks.

                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