(TRANSACT SQL) ¿como crear Master-Detail en una fila usando sql?

Tengo dos mesas

table1
------
ID
NAME
ADDRESS

table2
-------
ID
PHONE
EMAIL

how can i create report like this

------------------------------------


01   Dave   123 Veneu

     555-5  A@YAHOO.COM
     66-66  B@Yahoo.co.id
     213-1  D@c.com


02   John   23 Park

     322-1  C@you.com
     54-23  D@Net.com
     231-2  me@you.com

im using sql server 2005 express,, thank you in advance.

preguntado el 08 de noviembre de 11 a las 09:11

Is your data model correct? It's rare for email addresses and phone numbers to be in a 1-1 relación. -

4 Respuestas

Not sure why you would ever want to write this in anything other than a report designer, but just for the hell of it:

SELECT ID AS Column1, NAME AS Column2, Address AS Column3, ID AS SortColumn1, 1 AS SortColumn2
UNION
SELECT '', PHONE, EMAIL, ID AS SortColumn1, 2 AS SortColumn2
ORDER BY SortColumn1, SortColumn2

The output is going to basically be a load of gibberish really, and you've got the two extra columns on the end of to get rid of.

respondido 08 nov., 11:13

You shouldn't. It's a general principle that formatting should not be done in the database layer.

SQL Server should be used to generate data, then your application should process the data, including the formatting.

I would open two queries. One that loads table one, ordered by the ID column. And the other that load table two, also ordered by the ID column. You can then iterate through both record sets at the same time, something like the following pseudo-code...

rs1 = SQL.Execute("SELECT * FROM table1 ORDER BY ID")
rs2 = SQL.Execute("SELECT * FROM table2 ORDER BY ID, phone")

rs2.Next()
WHILE rs1.Next()
  Output The Address Info Here
  WHILE rs1.ID = rs2.ID
    Output The Phone/Email Info Here
    rs2.Next()
  END WHILE
END WHILE

respondido 08 nov., 11:14

In order to have one-to-many relationship, as in your example (one person has multiple phones and emails), you need to add some kind of link column to the second table, which would contain the ID of the person the email / phone belongs to.

So your table structure should look like this:

table1
------
ID
NAME
ADDRESS

table2
-------
ID
TABLE1_ID
PHONE
EMAIL

Then, you could query your data using joins:

SELECT table1.name, table1.address, table2.phone, table2.email WHERE table2.table1_id = table1.id

respondido 08 nov., 11:13

I strongly agree with dems, but if you really need to come up with something like that, the following could work (albeit without the empty lines)

 SELECT case 
            when group_rn = 1 then id 
            else ''
        end as id, 
        case 
          when group_rn = 1 then name
          else phone 
        end as name_phone_column, 
        case 
          when group_rn = 1 then address
          else _email 
        end as address_email_column
 FROM (
     SELECT t1.id,
            t1.name,
            t1.address,
            t2.email,
            t2.phone, 
            row_number() over (partition by t1.id order t1.name) as group_rn
     FROM table1 t1
        LEFT JOIN table2 t2 ON t1.id = t2.id
 ) t
 ORDER BY id

Esto supone que phone y name both have the same datatype, just like address y email.

respondido 08 nov., 11:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.