Jason Congerton
Jason Congerton

Reputation: 830

Joining table issue with SQL Server 2008

I am using the following query to obtain some sales figures. The problem is that it is returning the wrong data.

I am joining together three tables tbl_orders tbl_orderitems tbl_payment. The tbl_orders table holds summary information, the tbl_orderitems holds the items ordered and the tbl_payment table holds payment information regarding the order. Multiple payments can be placed against each order.

I am trying to get the sum of the items sum(mon_orditems_pprice), and also the amount of items sold count(uid_orderitems).

When I run the following query against a specific order number, which I know has 1 order item. It returns a count of 2 and the sum of two items.

Item                       ProdTotal    ProdCount
Westvale Climbing Frame    1198         2

This order has two payment records held in the tbl_payment table, which is causing the double count. If I remove the payment table join it reports the correct figures, or if I select an order which has a single payment it works as well. Am I missing something, I am tired!!??

SELECT 
  txt_orditems_pname,
  SUM(mon_orditems_pprice) AS prodTotal,
  COUNT(uid_orderitems) AS prodCount
FROM dbo.tbl_orders
  INNER JOIN dbo.tbl_orderitems ON (dbo.tbl_orders.uid_orders = dbo.tbl_orderitems.uid_orditems_orderid)
  INNER JOIN dbo.tbl_payment ON (dbo.tbl_orders.uid_orders = dbo.tbl_payment.uid_pay_orderid)
WHERE
  uid_orditems_orderid = 61571
GROUP BY
  dbo.tbl_orderitems.txt_orditems_pname
ORDER BY
  dbo.tbl_orderitems.txt_orditems_pname

Any suggestions?

Thank you.

Drill down Table columns

dbo.tbl_payment.bit_pay_paid (1/0) Has this payment been paid, yes no
dbo.tbl_orders.bit_order_archive (1/0) Is this order archived, yes no
dbo.tbl_orders.uid_order_webid (integer) Web Shop's ID
dbo.tbl_orders.bit_order_preorder (1/0) Is this a pre-order, yes no
YEAR(dbo.tbl_orders.dte_order_stamp) (2012) Sales year
dbo.tbl_orders.txt_order_status (varchar) Is the order dispatched, awaiting delivery
dbo.tbl_orderitems.uid_orditems_pcatid (integer) Product category ID

Upvotes: 1

Views: 106

Answers (1)

Ivan Golović
Ivan Golović

Reputation: 8832

It's a normal behavior, if you remove grouping clause you'll see that there really are 2 rows after joining and they both have 599 as a mon_orditems_pprice hence the SUM is correct. When there is a multiple match in any joined table the entire output row becomes multiple and the data that is being summed (or counted or aggregated in any other way) also gets summed multiple times. Try this:

SELECT      txt_orditems_pname,
            SUM(mon_orditems_pprice) AS prodTotal,
            COUNT(uid_orderitems) AS prodCount
FROM        dbo.tbl_orders
INNER JOIN  dbo.tbl_orderitems ON (dbo.tbl_orders.uid_orders = dbo.tbl_orderitems.uid_orditems_orderid)
INNER JOIN  
(
    SELECT  x.uid_pay_orderid
    FROM    dbo.tbl_payment x
    GROUP   BY x.uid_pay_orderid
) AS payments ON (dbo.tbl_orders.uid_orders = payments.uid_pay_orderid)

WHERE
  uid_orditems_orderid = 61571
GROUP BY
  dbo.tbl_orderitems.txt_orditems_pname
ORDER BY
  dbo.tbl_orderitems.txt_orditems_pname

I don't know what data from tbl_payment you are using, are any of the columns from the SELECT list actually from tbl_payment? Why is tbl_payment being joined?

Upvotes: 1

Related Questions