My head is about to explode with everything I’ve read about SQL Server and Joins in the last 2 hours.
tbl_customers
-------------
IPaddress
CustomerID
tbl_purchases
-------------
OrderID (pkey)
CustomerID
OrderTotal
I want to get the total purchase amounts per IP address. There are more columns in the tbl_customers table, such that there are duplicate (IPaddress, CustomerID) rows. I have used the following query:
SELECT DISTINCT IPaddress, SUM(OrderTotal) FROM tbl_customers a
INNER JOIN tbl_purchases b ON a.CustomerID = b.CustomerID
GROUP BY IPaddress;
But it retrieves the duplicate rows from tbl_customers and causes the sum function to count the same purchase multiple times. What am I doing wrong? Efficiency isn’t really an issue as I’m dealing with under 10K records.
OR