Exercise #30 |
|||||||||||||||||||||||||||||||||||||||
Under the assumption that the income (inc) and expenses (out) of the money at each outlet (point) are registered any number of times a day, get a result set with fields: outlet, date, expense, income. Note that a single record must correspond to each outlet at each date. Use Income and Outcome tables. This task is required to gather data from two tables in one resultant set so the income and the outcome in one point must be in one row. Similar task ( Solution 2.1.1
The idea is in the joining of table with incomes and with outcomes by equality of point`s number and a date. Left join guarantees right result in case when there is some days with income only (outcome is NULL). Then it unions with an opposite query which has left join the outcomes table with the incomes table. This takes into account opposite situation when there is outcome and income is NULL. The duplicates are removed by UNION clause. The query 2.1.1 returns wrong result if many income/outcome operations at one point happens in one day. As example for this situation we get typical day: March 24, 2001. Let`s execute a couple of queries:
We get such result: Income
Outcome
In case when there are both: incomes and outcomes, outer join and inner join are the same. Every single row from one table joins with every row from other one if the date and the point`s number are equal. That`s why before data is grouping such result would be returned (the income and outcome columns are only displayed):
After grouping and summarizing we get doubling of the result as for income as well for outcome. If there would be three incomes, we would get tripling of outcome and so on. The duplicates aren`t matter, because each query in union returns analogous result: there is only one row for each pair {point, date}.
|