0

I have this query:

Select
  X.type1transsum, Y.type2transsum,
  (X.type1transsum + Y.type2transsum),
  X.`date`
From (
  Select sum(trans) as type1transsum, `date`
  from type1trans where shopcode = 1037
  group by `date`
) X 
Left join (
  Select sum(trans) as type2transsum, `date`
  from type2trans where shopcode = 1037
  group by `date`
) Y on X.`date` = Y.`date` 

The type2trans table has records for each date, while in type1trans there are no records for some days (as those days have no transactions).

For the dates where there is no records in the type1trans then there is no record shown for the type2 dates and a total for that date being the same as type2 since type1 null. How can I get type1 to return null for the date?

1 Answer 1

1

Use a RIGHT JOIN instead of a LEFT JOIN (or just swap the two subselects around in the query).

A useful rule to remember the difference between left and right joins is that a left join includes every row on its left side, even if there's no matching row on the right, while a right join, conversely, includes every row on its right side.

1
  • thanks - I had just figured out to swap the subselects around. thanks for the tip re right and left
    – CHinAU
    Dec 28, 2012 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.