-2

I have these tables Tables I want to have a result like this

╭────────────┬────────────┬──────┬────────────────────────┬────────────────────────╮
│ InvoiceID  │    Date    │ Sum  │ SUM(RecietInvoice.Sum) │ RecietInvoice.RecietID │
├────────────┴────────────┴──────┴────────────────────────┴────────────────────────┤
│      1     │ 11/11/2010 │ 1200 │           1200         │        34,45           │
╰────────────┴────────────┴──────┴────────────────────────┴────────────────────────╯

In the RecietInvoice.RecietID I want to have all the RecietID for this Invoice.

Kulpemovitz
  • 531
  • 1
  • 9
  • 23

1 Answers1

2

Here is one way (untested might have typos)

Select I.InvoiceID, I.Date, RI.Sum, 
   SUM(R.CashTotal) OVER(PARTITION BY  I.InvoiceID) 
     stuff( (SELECT ', '+CAST(RecietR2.RecietID as varchar(max))
             FROM  Reciet R2
             WHERE RecietR2.InvoiceID  = I.InvoiceID
             FOR XML PATH ('')
           ), 1, 2, '') as RecietIDs
FROM Invoice I
LEFT JOIN RecietInvoice RI ON I.InvoiceID = RI.InvoiceID
LEFT JOIN Reciet R ON RI.RecietID = R.RecietID
Hogan
  • 69,564
  • 10
  • 76
  • 117