SQL sp Help

Hello,
I use below code to get Invoices details from my Database,
the issue I have is, if the number  of pieces is 0 for an order number, then it will not display the order info.

Your help is appreciated,

;WITH CTE1 AS (SELECT foi.Invoicenumber,FO.OrderNo,OrderDate,AccountNumber,Caller,Department,Reference,DeliveredTime,
PickupCompanyName,PickupStreet,PickupCity,PickupPostalCode,PickupProvince,PickupCountry,PickupContact,PickupPhone,
DeliveryCompanyName,DeliveryStreet,DeliveryCity,DeliveryPostalCode,DeliveryProvince,DeliveryCountry,DeliveryContact,DeliveryPhone,
ServiceTypeID, (Select Description from ServiceTypes where ServiceTypes.ServiceTypeID = fo.ServiceTypeID) AS [Service],
fo.Weight,fo.CubedWeight,fop.pieceno, pt.description, WaitingTimeDriver1,WaitingTimeDriver2,POD,Operator,Instructions,
ISNULL((SELECT dbo.GetOrderExtras(fo.Orderno)), '')  AS [ExtrasBreakDown],
WebOrder,PickupDriver,DeliveryDriver,DeclaredValue,InsuranceRequired,Sales1Commission,Sales2Commission,BaseAmount,
WeightAmount,WaitingTimeAmount,PackageAmount,VehicleAmount,AfterHoursAmount,InsuranceAmount,FuelAmount,DiscountAmount,
(WaitingTimeAmount + PackageAmount + AfterHoursAmount + InsuranceAmount) AS [Other_Amount],
Tax1Amount,Tax1Rate,Tax1Name,Tax1ID,Tax2Amount,Tax2Name,Tax2ID,SubTotalAmount,TotalAmount
FROM finalizedorders fo JOIN FinalizedOrderPackages fop ON fo.orderno = fop.orderno JOIN finalizedordersinvoices foi ON fo.orderno = foi.orderno JOIN Packagetypes pt ON fop.packagetypeid = pt.packagetypeid
WHERE ((foi.invoicenumber = @InvoiceNumber))) ,
CTE2
AS (SELECT *, row_number() OVER ( PARTITION BY invoicenumber, orderno, Description ORDER BY PieceNO DESC) rn FROM CTE1 c1),
CTE3
AS (SELECT DISTINCT InvoiceNumber, OrderNo,OrderDate,AccountNumber,Caller,Department,Reference,DeliveredTime,
PickupCompanyName,PickupStreet,PickupCity,PickupPostalCode,PickupProvince,PickupCountry,PickupContact,PickupPhone,DeliveryCompanyName,DeliveryStreet,DeliveryCity,DeliveryPostalCode,DeliveryProvince,
DeliveryCountry,DeliveryContact,DeliveryPhone,ServiceTypeID, Service,Weight,CubedWeight,sum(PieceNO) OVER (  PARTITION BY invoicenumber, orderno) PieceNO, rtrim(substring(isnull((SELECT ',' + CONVERT(VARCHAR, PieceNO) + ' '+ Description
FROM CTE2 c1 WHERE c1.rn = 1 AND c1.invoicenumber = c2.invoicenumber AND c1.orderno = c2.orderno FOR XML PATH('')), ' '), 2, 2000)) Description , WaitingTimeDriver1,WaitingTimeDriver2,POD,Operator,Instructions,ExtrasBreakDown, WebOrder,PickupDriver,DeliveryDriver,DeclaredValue,InsuranceRequired,Sales1Commission,Sales2Commission,BaseAmount,WeightAmount,WaitingTimeAmount,PackageAmount,
VehicleAmount,AfterHoursAmount,InsuranceAmount,FuelAmount,DiscountAmount,Other_Amount, Tax1Amount,Tax1Rate,Tax1Name,Tax1ID,Tax2Amount,Tax2Name,Tax2ID,SubTotalAmount,TotalAmount FROM CTE2 c2 WHERE c2.rn = 1) SELECT * FROM CTE3
W.E.BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>FROM finalizedorders fo JOIN FinalizedOrderPackages
Without going through all of your code, if fo is 'order info' and FinalizedOrderPackages is the 'number of pieces', then you'll need to change the JOIN to a LEFT JOIN so that all fo rows with no corresponding FinalizedOrderPackages  is returned.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
W.E.BAuthor Commented:
That did it,
Awesome.
thanks.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your code.  -Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.