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. Help me with this Query

Help me with this Query

Scheduled Pinned Locked Moved Database
databasehelptutorial
4 Posts 2 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.
  • M Offline
    M Offline
    Mhiny
    wrote on last edited by
    #1

    I have 2 tables

    a id
    x
    y

    b id amount1 amount2 amount3 a_id
    1 5 3 5 x
    1 2 4 5 y

    I want result like this -->

    	x	5	3	5	y	2	4	5
    

    how to create query to retrieve this data.can i use pivot .Thank for your help.

    W 1 Reply Last reply
    0
    • M Mhiny

      I have 2 tables

      a id
      x
      y

      b id amount1 amount2 amount3 a_id
      1 5 3 5 x
      1 2 4 5 y

      I want result like this -->

      	x	5	3	5	y	2	4	5
      

      how to create query to retrieve this data.can i use pivot .Thank for your help.

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      In order to help, you need to provide more information about what you are trying to achieve. For example: Can table A have more rows. If so, do you want to still add more columns and keep the data on a single row (I believe that this wouldn't be possible). Can table B have several rows per one row in table A, what happens then etc. Based on the data you provided the following query should provide the result you described, but I think it's hardly what you wanted:

      SELECT a1.id, b1.amount1, b1.amount2, b1.amount3,
      a2.id, b2.amount1, b2.amount2, b2.amount3
      FROM a a1,
      b b1,
      a a2,
      b b2
      WHERE a1.id = 'x'
      AND b1.a_id = a1.id
      AND a2.id = 'y'
      AND b2.a_id = a2.id

      The need to optimize rises from a bad design. My articles[^]

      M 1 Reply Last reply
      0
      • W Wendelius

        In order to help, you need to provide more information about what you are trying to achieve. For example: Can table A have more rows. If so, do you want to still add more columns and keep the data on a single row (I believe that this wouldn't be possible). Can table B have several rows per one row in table A, what happens then etc. Based on the data you provided the following query should provide the result you described, but I think it's hardly what you wanted:

        SELECT a1.id, b1.amount1, b1.amount2, b1.amount3,
        a2.id, b2.amount1, b2.amount2, b2.amount3
        FROM a a1,
        b b1,
        a a2,
        b b2
        WHERE a1.id = 'x'
        AND b1.a_id = a1.id
        AND a2.id = 'y'
        AND b2.a_id = a2.id

        The need to optimize rises from a bad design. My articles[^]

        M Offline
        M Offline
        Mhiny
        wrote on last edited by
        #3

        table 'a' is dynamic can insert or delete id
        than i can not create static query (like the sample that you show me)
        the data can be like this
        a id
        x
        y
        z

        b id amount1 amount2 amount3 a_id
        1 5 3 5 x
        1 2 4 5 y
        1 7 8 9 z
        2 3 1 3 x
        2 4 5 6 y
        2 8 3 1 z

        result is:

        x 5 3 5 y 2 4 5 z 7 8 9 --> id = 1 in table b
        x 3 1 3 y 4 5 6 z 8 3 1 --> id = 2 in table b

        W 1 Reply Last reply
        0
        • M Mhiny

          table 'a' is dynamic can insert or delete id
          than i can not create static query (like the sample that you show me)
          the data can be like this
          a id
          x
          y
          z

          b id amount1 amount2 amount3 a_id
          1 5 3 5 x
          1 2 4 5 y
          1 7 8 9 z
          2 3 1 3 x
          2 4 5 6 y
          2 8 3 1 z

          result is:

          x 5 3 5 y 2 4 5 z 7 8 9 --> id = 1 in table b
          x 3 1 3 y 4 5 6 z 8 3 1 --> id = 2 in table b

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          I don't think that what you're asking for is reasonably possible. This would lead to several problems: - how the client side knows which columns are fetched from which row - what if id X in table has 3 rows in table B, but id Y has two rows, what would be the result etc I would look for alternative solutions for the client side. If the client needs the data in several (undetermine amount) columns based on several rows, fetching the data in such format from the database isn't practical. Could you for example use XML formatting for the data from the database and then use XML data at client or should you fetch the correct rows from the database as they are and handle all the formatting at the client. The correct approach depends on the requirements, what is the client side actually doing.

          The need to optimize rises from a bad design. My articles[^]

          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