Ver Mensaje Individual
  #2 (permalink)  
Antiguo 06/09/2017, 08:52
Avatar de iislas
iislas
Colaborador
 
Fecha de Ingreso: julio-2007
Ubicación: Mexico, D.F.
Mensajes: 6.482
Antigüedad: 17 años, 5 meses
Puntos: 180
Respuesta: Select con cabecera y detalle

Veamos si esto da solucion a tu consulta

Código SQL:
Ver original
  1. CREATE TABLE Header
  2. (
  3.       InvoiceNumber INT
  4.     , [DATE] DATETIME
  5.     , Amount DECIMAL(10,2)
  6. )
  7. GO
  8.  
  9. INSERT INTO Header VALUES
  10. (100,GETDATE(),100.00),
  11. (101,GETDATE()+2,100.00)
  12. GO
  13.  
  14. CREATE TABLE Details
  15. (
  16.      InvoiceNumber INT
  17.     ,Customer VARCHAR(100)
  18.     ,Purchaseorder VARCHAR(100)
  19.     ,DetailAmount DECIMAL(30,2)
  20. )
  21. GO
  22.  
  23. INSERT INTO Details VALUES
  24. (100,'Customer A', 'Purchase Order A', 1000.2 ),
  25. (100,'Customer B', 'Purchase Order B', 787.23 ),
  26. (101,'Customer C', 'Purchase Order F', 12131.99),
  27. (101,'Customer D', 'Purchase Order G', 2334.18)
  28. GO
  29.  
  30. SELECT InvoiceNumber , [DATE] , Amount
  31. FROM
  32. (
  33.     SELECT CAST(InvoiceNumber AS sql_variant) InvoiceNumber , CAST( [DATE]  AS sql_variant) [DATE] , Amount
  34.     , CONVERT(FLOAT,InvoiceNumber) rnk
  35.     FROM Header
  36.     UNION ALL
  37.     SELECT Customer, purchaseorder, detailamount , rnk FROM (
  38.     SELECT id.*, concat(id.InvoiceNumber, '.', ROW_NUMBER() OVER (PARTITION BY id.InvoiceNumber ORDER BY id.invoicenumber)) rnk
  39.     FROM Details i
  40.     INNER JOIN Details id ON i.InvoiceNumber = id.InvoiceNumber) K
  41. )p ORDER BY rnk


InvoiceNumber Date Amount
100 2017-09-06 09:53:53.270 100.00
Customer A Purchase Order A 1000.20
Customer B Purchase Order B 787.23
Customer A Purchase Order A 1000.20
Customer B Purchase Order B 787.23
101 2017-09-08 09:53:53.270 100.00
Customer C Purchase Order F 12131.99
Customer D Purchase Order G 2334.18
Customer C Purchase Order F 12131.99
Customer D Purchase Order G 2334.18
__________________
MCTS Isaias Islas