OK, I have a system where orders get entered, and each order can have any number of bond, job and project numbers attached to it. The OrderDetails
table contains an orderID
used to go to the OrderNumbers
table and get all attached "numbers" to this order, and then based on a numType
column found there, it goes to one of the three tables (bond, job, project) and retrieves the actual number displayed and printed for the user.
This means that trying to query an order to search for a certain project number can return 3 rows for the same order if that order had 3 project numbers attached to it.
I am looking to be able to return my query results with only 1 row for the order, and a column which includes all 3 projects in delimited form, is this possible?
Here is my current query which returns multiple rows per order, when multiple numbers of the same type are attached to the order...
SELECT
ISNULL(b.Descr,'') as Bond, ISNULL(PO.Description,'') as PO,
ISNULL(j.Descr,'') as Job, ISNULL(Proj.Descr,'') as Project,
d.OrdNbr, d.BillofLadingNbr, d.TripAndDeliveryTicketNbr
FROM
OrderDetail d
LEFT JOIN
OrderItemNumbers n ON d.OWID = n.LoadOWID
LEFT JOIN
Bond b ON n.NumberOWID = b.OWID AND n.NumType = 0
LEFT JOIN
PONumbers PO ON n.NumberOWID = PO.OWID AND n.NumType = 1
LEFT JOIN
Job j ON n.NumberOWID = j.OWID AND n.NumType = 2
LEFT JOIN
Project Proj ON n.NumberOWID = Proj.OWID AND n.NumType = 3
WHERE
d.OWID = 'BTBD1004'
and here are the results I get....
Bond PO Job Project OrdNbr BillofLadingNbr
82001-8177-44 BTBD000063 BTBD000072
4.158 Kingsport BTBD000063 BTBD000072
IME-81-1(118) BTBD000063 BTBD000072
and here is what I am trying to get...(basically flattening those first 4 columns)
Bond PO Job Project OrdNbr BillofLadingNbr
4.158 Kingsport 82001-8177-44, IME-81-1(118) BTBD000063 BTBD000072
Any help is appreciated!!! Thanks, Doug