Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joining one table of project numbers to 3 allowing nulls (outer join)

Status
Not open for further replies.

wvotta

MIS
Aug 19, 2009
31
US
I have a project table, 3 types of project costs (PO, expenses or non-PO, and hours) there is also a rate table for hours and department (which I had to do for security when entering data).

Below (after my ramblings) is my current sql madness. Hopefully it's spaced well enough to be readable. The '2009' will be replaced by a php variable for the year.
This query "works" in that it gets me the all the project numbers, including those with no corresponding records (which I need) and it gets the correct hours total by pulling in rate and it also shows the department.

You'll see my fancy COALESCE statement which works great save that I have repeated records in more than just the project numbers. I need them for outputting the data in table where the columns are all months (so the significant date is important, and the significant month is a shortcut to save me from doing it in php). What I'm trying to do is get a full list of project numbers with unique rows for the 3 joined tables.

In short, I want
project1 null null hours1
project1 null null hours2
project2 null null null
project3 expense1 null null
project3 null null hours3
project3 null PO1 null
project4 expense2 null null
project5 null PO2 null

...etc so that when a row contains data for hours, it shows null in all other columns (save that of the main project table), and likewise for the other 2.

Here's my SQL now:
SELECT
(
IF(
(COALESCE(projexp.exp_date,projpos.po_date,projhours.weekending) IS NOT NULL),
(IF(
FROM_UNIXTIME(COALESCE(projexp.exp_date,projpos.po_date,projhours.weekending),'%Y') = '2009',
(COALESCE(projexp.exp_date,projpos.po_date,projhours.weekending)),
NULL
)),
NULL
)
) AS "SigDate",
(COALESCE(FROM_UNIXTIME(projexp.exp_date,'%m'),FROM_UNIXTIME(projpos.po_date,'%m'),FROM_UNIXTIME(projhours.weekending,'%m'))) AS "SigMo",
projects.project_no,
projects.project_desc,
projexp.expID,
projexp.project_no,
projexp.exp_desc,
projexp.exp_date,
projexp.exp_type,
projexp.exp_amt,
projexp.exp_notes,
projpos.posID,
projpos.po_no,
projpos.project_no,
projpos.po_date,
projpos.po_amt,
projpos.po_notes,
projhours.hrsID,
projhours.project_no,
projhours.empid,
projhours.weekending,
(projhours.hours * (SELECT emp_rate FROM emprates WHERE empid = projhours.empid)) AS "hourstot",
(SELECT emp_dept FROM emprates WHERE empid = projhours.empid) AS "emp_dept"
FROM projects
LEFT JOIN projexp ON projects.project_no = projexp.project_no
LEFT JOIN projpos ON projects.project_no = projpos.project_no
LEFT JOIN projhours ON projects.project_no = projhours.project_no
ORDER BY projects.project_no ASC, SigDate ASC, SigMo ASC;

Here's the table structures:
mysql> SHOW COLUMNS FROM projects;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| project_no | varchar(25) | NO | PRI | NULL | |
| project_desc | varchar(250) | NO | MUL | NULL | |
+--------------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM projexp;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| expID | int(11) | NO | PRI | NULL | auto_increment |
| project_no | varchar(25) | NO | MUL | NULL | |
| exp_desc | varchar(250) | NO | | NULL | |
| exp_date | int(11) | NO | | NULL | |
| exp_type | varchar(25) | NO | | NULL | |
| exp_amt | double | NO | | NULL | |
| exp_notes | varchar(250) | NO | MUL | NULL | |
+------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM projpos;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| posID | int(11) | NO | PRI | NULL | auto_increment |
| po_no | int(11) | NO | UNI | NULL | |
| project_no | varchar(25) | NO | MUL | NULL | |
| po_date | int(11) | NO | | NULL | |
| po_amt | double | NO | | NULL | |
| po_notes | varchar(250) | NO | MUL | NULL | |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM projhours;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| hrsID | int(11) | NO | PRI | NULL | auto_increment |
| project_no | varchar(25) | NO | MUL | NULL | |
| empid | int(5) | NO | MUL | NULL | |
| hours | decimal(5,2) | NO | | NULL | |
| weekending | int(11) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM emprates;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| empid | int(5) | NO | PRI | NULL | |
| emp_dept | varchar(45) | NO | MUL | NULL | |
| emp_name | varchar(45) | NO | MUL | NULL | |
| emp_rate | decimal(5,2) | NO | | NULL | |
+----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top