DISCLAIMER : Never Learned Relational Algebra but it looks interesting
From the schema given and your question, this is what the SQL should be:
SELECT
emp_mgr.person_name
FROM
manages emp_mgr
INNER JOIN employee emp ON emp_mgr.person_name = emp.person_name
INNER JOIN employee mgr ON emp_mgr.manager_name = mgr.person_name
WHERE
emp.street = mgr.street AND
emp.city = mgr.city
;
Here is another query that only uses JOINs, no WHERE clause:
SELECT
emp.person_name
FROM
(SELECT A.person_name,B.street,B.city FROM manages A
INNER JOIN employee B ON A.person_name = B.person_name) emp
NATURAL JOIN
(SELECT A.manager_name,B.street,B.city FROM manages A
INNER JOIN employee B ON A.manager_name = B.person_name) mgr
;
The first query gets all employees who are managed and their managers in the form of a Cartesian Product. Then, it looks for a common street and city.
The second query collects personnel records (name,street,city) of employees and their managers and performs a NATURAL JOIN between the employess and their managers using (street,city).
If you can transalate both queries back to Relational Algebra, I think you will have what you are looking for. I believe the second may be of better help.
There are a few things you want to factor into your design:
1. Measurements Need a Timestamp
Make sure all of your measurements have an indication of:
- Scalar Value
- Unit of Measurement
- Date and Time the Measurement was Taken
This will allow you to work with measurements that need time-dependent conversion calculations.
2. Units of Measure Have Attributes
Each unit of measure has a few different attributes. The obvious ones are indicative, like a code and maybe a descriptive name. There are also a couple of critical other attributes to keep for each unit of measure. (i) Unit Type and (ii) Conversion Factor to the Base Unit.
The first tells you whether your unit of measure is a length, a weight, energy, power, currency, etc. etc. It should also tell you what the base unit of measure is. You should pick exactly one for each unit type. You can use things like kWh if you like, but I'd stick to the base SI units (as applicable) if I were you.
The second tells you what your unit of measure needs to be multiplied by to get it to the base. I mentioned that this is an attribute of your UOM, but in fact it needs to be in a child table. The business key of the child table that holds this base conversion factor is the combination of the UOM, its base unit type and a date/time. I would keep both an effective and an expiry date/time on the base conversion factor table. This allows you to quickly find the right rate that applies at any particular point in time. If it happens to be a rate that doesn't change, that's OK. Just use a min-collating effective date and a max-collating expiry date for the one record.
3. Trying to Table-Drive Everything Will Make You Nuts
The last piece of the puzzle is determining the calculation for moving from one kind of unit to another kind of unit. You could try to table-drive this kind of calculation but in the end the tricky ones are going to make the design so general (read complicated and slow) that it will be impractical. Instead, create a code-table of conversion calculations and use it to link one kind of Unit Type to another kind of Unit Type. Perform the actual calculations in some code somewhere. Which piece of code you use for any given conversion is what the code table tells you. How the calculation is performed is just in the code. You can have one calculation each for the various easy things, like area needs two lengths and volume needs three lengths as well as the harder ones like work needs energy and time.
When you get the details of your design figured out you should blog it and come back here to post a link!
Best Answer
Your sql query is wrong:
I'm having a bit of trouble reading your algebra expressions, but the first one looks ok. It should not matter that you do the selection (topic = "motorcycle") after the join instead of joining on the selection.
The second one can't be right. Assume there's a newspaper that published both an article on motorcycle and an article on something else. Your expression will pick the article on something else and therefore return that newspaper (incorrectly).