SPLady
asked on
union join
Hi Experts!
what is the best way to add an additonal table to an exisiting union join?
what is the best way to add an additonal table to an exisiting union join?
SELECT recType, recordID, transDate, transyear, qty, extendedStandardCost, currencyCode, businessEntity, product, site, lineValue AS 'OrderAmount',
CAST(0 AS NUMERIC(30, 2)) AS 'InvoiceAmount', subProduct, CASE WHEN TransYear = Year(getdate()) - 1 THEN LineValue ELSE CAST(0 AS Numeric(30,
2)) END AS 'OrderAmount_LastYear', CAST(0 AS Numeric(30, 2)) AS 'InvoiceAmount_LastYear', CASE WHEN TransYear = Year(getdate())
THEN LineValue ELSE CAST(0 AS Numeric(30, 2)) END AS 'OrderAmount_CurrentYear', CAST(0 AS Numeric(30, 2))
AS 'InvoiceAmount_CurrentYear'
FROM dbo.vReportData
WHERE (recType = 'order') AND (businessEntity = 'Energy')
UNION ALL
SELECT recType, recordID, transDate, transyear, qty, extendedStandardCost, currencyCode, businessEntity, product, site, 0 AS 'OrderAmount',
lineValue AS 'InvoiceAmount', subProduct, CAST(0 AS Numeric(30, 2)) AS 'OrderAmount_LastYear', CASE WHEN TransYear = Year(getdate())
- 1 THEN LineValue ELSE CAST(0 AS Numeric(30, 2)) END AS 'InvoiceAmount_LastYear', CAST(0 AS Numeric(30, 2)) AS 'OrderAmount_CurrentYear',
CASE WHEN TransYear = Year(getdate()) THEN LineValue ELSE CAST(0 AS Numeric(30, 2)) END AS 'InvoiceAmount_CurrentYear'
FROM dbo.vReportData AS ReportData1
WHERE (recType = 'invoice') AND (businessEntity = 'Energy')
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you @brandongalderisi what is the best solution or how do I troubleshoot this issue?
ASKER
duh a very dim light goes on... For one thing i enter duplicates in my plan table...
Okay, just for clarification, is vplanjoin a table or a view and are you really working with a table or a view?
When you look at planjoin, what columns define a unique row? If all of those columns cannot be used in the JOIN you are doing, then you will have duplicate rows . . . basically because you are specifying enough constraints to uniquely select a single row for the planjoin side of the JOIN.
When you look at planjoin, what columns define a unique row? If all of those columns cannot be used in the JOIN you are doing, then you will have duplicate rows . . . basically because you are specifying enough constraints to uniquely select a single row for the planjoin side of the JOIN.
ASKER
thank you @8080 diver
vplanjoin is a view, my goal is to pull in the plan numbers to compare to the actual orders and invoices. The plan column and a combination of the other columns create unique rows.. In the creation of the view I used an union so that I could create columns from recType for order and invoice.
vplanjoin is a view, my goal is to pull in the plan numbers to compare to the actual orders and invoices. The plan column and a combination of the other columns create unique rows.. In the creation of the view I used an union so that I could create columns from recType for order and invoice.
ASKER
@brandongalderisi what would that look like @8080 diver, I am working on the same issue, I am trying to figure out how to add the the planjoin table with out getting duplicates. I dont have duplicates until I add that table however, there are relationships on four of the columns :(
Open in new window