SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stuck with a SQL query.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SergioM
Posting Yak Master

144 Posts

Posted - 07/22/2013 :  12:29:16  Show Profile  Reply with Quote


To Clarify: My aim is to eliminate the Cartesian product. How do I do this?



I'm a bit stuck with a query. I have to do a P&L based on orders, but each order can have several different units. Moreover the last inner join (d), causes a Cartesian Product. In the first example, I'm able to avoid that by adding 'DISTINCT' to the SELECT statement.

First Code
SELECT DISTINCT
	 a.[ID]
	 ,B.[ID]
	,b.[AverageCost]
	,b.[PostingFee]
	,d.[Commission]
	,c.[EbayTransactionFee]
	,b.[ShippingCost]
	,b.[LineTaxTotal]
	,a.[FinalShippingFee]
	,b.[ProductRebateValue]
	,a.[GrandTotal]
	,CONVERT(DECIMAL(18,2),a.[GrandTotal]-(b.[AverageCost] + d.[Commission] + a.[FinalShippingFee]))  AS 'Profit'
	
  FROM [SC].[dbo].[bvc_Order]				a
  INNER JOIN [SC].[dbo].[bvc_OrderItem]		b
  ON a.ID = b.OrderID
  INNER JOIN [SC].[dbo].[Product]			c
  ON b.[ProductID] = c.[ID]
  INNER JOIN [SC].[dbo].[BuyDotComOrders]	d
  ON d.[OrderSourceOrderId] = a.[OrderSourceOrderId]


However, when I add a GROUP BY clause to see the P&L based on orders, the distinct statement doesn't help in the same way.

Second Code
SELECT DISTINCT
	 a.[ID]
	,SUM(b.[AverageCost])
	,SUM(b.[PostingFee])
	,SUM(d.[Commission])
	,SUM(c.[EbayTransactionFee])
	,SUM(b.[ShippingCost])			-- <--- SUM OR NOT?
	,SUM(b.[LineTaxTotal])
	,a.[FinalShippingFee]
	,SUM(b.[ProductRebateValue])
	,SUM(a.[GrandTotal])
	,CONVERT(DECIMAL(18,2),a.[GrandTotal]-(b.[AverageCost] + d.[Commission] + a.[FinalShippingFee]))  AS 'Profit'
	
  FROM [SC].[dbo].[bvc_Order]				a
  INNER JOIN [SC].[dbo].[bvc_OrderItem]		b
  ON a.ID = b.OrderID
  INNER JOIN [SC].[dbo].[Product]			c
  ON b.[ProductID] = c.[ID]
  INNER JOIN [SC].[dbo].[BuyDotComOrders]	d
  ON d.[OrderSourceOrderId] = a.[OrderSourceOrderId]

GROUP BY a.[ID]



How do I proceed? If I can't figure this out, I have to make each column it's own subquery. Which will do the job, but it's not the smart way to do it.

-Sergio
I use Microsoft SQL 2008

Edited by - SergioM on 07/22/2013 13:00:39

SwePeso
Patron Saint of Lost Yaks

Sweden
30115 Posts

Posted - 07/22/2013 :  12:44:05  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Don't you need to sum the profits too?


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/22/2013 :  12:49:27  Show Profile  Reply with Quote

quote:
Originally posted by SwePeso

Don't you need to sum the profits too?


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA



and how about a.[FinalShippingFee]
Go to Top of Page

SergioM
Posting Yak Master

144 Posts

Posted - 07/22/2013 :  12:50:26  Show Profile  Reply with Quote
quote:
Originally posted by SwePeso

Don't you need to sum the profits too?


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA


I'll have to re approach that one. Grand Total is for the entire order but each of it's components are line-specific.

-Sergio
I use Microsoft SQL 2008
Go to Top of Page

SergioM
Posting Yak Master

144 Posts

Posted - 07/22/2013 :  12:51:05  Show Profile  Reply with Quote
quote:
Originally posted by MuMu88
and how about a.[FinalShippingFee]


Nope. That one is for the entire order.

-Sergio
I use Microsoft SQL 2008
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000