I have three table: Sales, Orders and Notes. My Sales table contains items that my customers have ordered, the Orders table contains items I have ordered from my suppliers and the Notes table contains miscellaneous comments regarding my ordered items. I'd like to get a result set that shows all items and their quantities ordered by my customers and ordered by me and then I'd like to show all related notes and dates in one additional column.Here's my table design:CREATE TABLE [dbo].[Sales]( [Number] [varchar](50) NOT NULL, [Name] [varchar](50) NOT NULL, [Qty] [int] NOT NULL) INSERT INTO [Sales] ([Number] ,[Name] ,[Qty])VALUES ('A123','SCREW 1/3', 3), ('A123','SCREW 1/3', 1), ('B323','SCREW 1/2', 1) CREATE TABLE [dbo].[Orders]( [OrderNumber] [varchar](50) NOT NULL, [Number] [varchar](50) NOT NULL, [Date] [datetime] NOT NULL, [Qty] [int] NOT NULL) INSERT INTO [Orders] ([OrderNumber] ,[Number] ,[Date] ,[Qty]) VALUES ('O-001', 'A123', '2011-04-01', 2), ('O-003', 'A123', '2011-03-21', 4), ('O-005', 'B323', '2011-03-21', 1)CREATE TABLE [dbo].[Notes]( [OrderNumber] [varchar](50) NOT NULL, [Notes] [varchar](200) NOT NULL) INSERT INTO [Notes] ([OrderNumber] ,[Notes]) VALUES ('O-001','expect delays'), ('O-001','removed items 221'), ('O-003','by train'), ('O-003','extra fuel charge')
The result set that I would like to see should look like this: Number Name SQty OQty Dates_NotesB323 SCREW 1/2 1 1 NULL A123 SCREW 1/3 16 24 2011-04-01:expect delays, removed items 221;2011-03-21: extra fuel charge
I started the query like this:SELECT s.Number, s.Name, SUM(s.Qty) AS SQty, SUM(o.Qty) AS OQty, <Concatenate o.Date & n.Notes>FROM Sales sLEFT JOIN Orders o ON s.Number = o.NumberLEFT JOIN Notes n ON o.OrderNumber = n.OrderNumberGROUP BY s.Number, s.Name
but I don't really know how to approach the concatenate part. Your help is appreciated.