troubleshooting Question

structure columns in query

Avatar of metropia
metropiaFlag for United States of America asked on
Microsoft SQL Server 2008SQL
43 Comments6 Solutions234 ViewsLast Modified:
I have a query that uses a union to get the results that I need, but I am having problems understanding how I need to change my query to be able to format the same results in a more readable manner. I am ending with a larger number of columns that I actually need.

The query is simple, I think the complexity comes in how it needs to be structured. May be I need to use CASE statements? I am not sure but any help would be appreciated.

My query:

SELECT 
	sl.No_ [ItemNumber]	
,   sl.Description
,   sl.[Document No_] [SalesOrderNumber]
,   sl.[Location Code] [LocationCode]
,   sl.[Shipment Date] [ShipmentDate]
,   sl.[Sell-to Customer No_] [CustomerNumber]
,   sl.Quantity [OrderQty]
,   cus.Name [CustomerName]
,   0  [FirmedPlannedQty]
,   '' [FirmedPlannedDueDate]
,   '' [FirmedPlannedNumber]
,   '' [FirmedPlannedLocation]
FROM
	dbo.[CQC$Sales Line] sl INNER JOIN
    dbo.[CQC$Customer] cus ON cus.No_ = sl.[Sell-to Customer No_]
WHERE
    sl.No_ = '10568'
AND
    sl.[Document Type] IN ('1')
AND
    sl.Quantity = sl.[Outstanding Quantity]
UNION
SELECT 
	'' [ItemNumber]	
,   '' [Description]
,   '' [SalesOrderNumber]
,   '' [LocationCode]
,   '' [ShipmentDate]
,   '' [CustomerNumber]
,   0  [OrderQty]
,   '' [CustomerName]
,	pol.Quantity [FirmedPlannedQty]
,   pol.[Due Date] [FirmedPlannedDueDate]
,   pol.[Prod_ Order No_] [FirmedPlannedNumber]
,   pol.[Location Code] [FirmedPlannedLocation]
FROM 
	dbo.[CQC$Prod_ Order Line] pol
WHERE	
	[Item No_] = '10568'	
AND 
	Status = 2
AND 
	[Variant Code] <> 'R'
ORDER BY 
	ShipmentDate asc
,	FirmedPlannedDueDate asc


the results:

current results of my query
how I need it to be (mock up with excel):

mock up with excel

Thank you very much for your help.

If more details need to be added, please let me know.
ASKER CERTIFIED SOLUTION
RehanYousaf

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 6 Answers and 43 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 6 Answers and 43 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros