I have this following problem:
I have two tables 1 table for customer bought items and the other for employee sales:
Table 1 (Emp)
EmpId
ProductId
dateSold
Price
Qty
Table 2 (Cust)
CustomerId
ProductId
dateSold
Price
Qty
do this is the query im tring at the moment:
SELECT SUM(EmployeeSales.productPrice * EmployeeSales.Qty +
userBoughtItems.ProductPrice * userBoughtItems.qty) AS Total,
EmployeeSales.dateSold,
userBoughtItems.dateSold AS Expr1
FROM EmployeeSales
INNER JOIN userBoughtItems ON EmployeeSales.dateSold = userBoughtItems.dateSold
GROUP BY EmployeeSales.dateSold, userBoughtItems.dateSold
I want to get the total of both tables on the same date…
A
joinrepeats the right table for every matching row in the left table. In your example I’d expect that to result in a lot of duplicated rows. Consider using aunioninstead:The subquery contains a list of all sales, and the outer query sums them per day.
If you’re using SQL Sever 2005 or older, replace
cast(dt as date)withdateadd(dd, 0, datediff(dd, 0, dt)).