I’m trying to combine results from a count statement with results from other criteria (multiple tables), and am beating my head against a wall.
My skills are not strong in this area, and getting to this point has been interesting. The last issue in this phase is that although I’m seeing the results I’m looking for, my UNION ALL isn’t combining the data.
You can see in the bottom table (RESULTS), that I'm getting multiple instances of the same system. I realize that I’m calling the same count twice, and it’s probably correctly trying to show me both counts, but all of my efforts to change that have taken me to other problems.
Eventually, I would also like to add at least two more counts from additional tables as well, but hopefully that won’t be as challenging once this is past. (I am also trying to get data for "systemid" where the count doesn't apply, I only want it to return those items specifically related to the count where the count is one or more. I've copied sample tables and code below.
Any help would be greatly appreciated!
Dave
TABLE1: SYSTEMS
+-----+-----------+-----------+----------------+
| sid | Name | checkdate | MissingPatches |
+-----+-----------+-----------+----------------+
| 52 | WCLDC | 20130623 | 2 |
| 79 | WCLVS001 | 0 | 0 |
| 112 | TAR201 | 0 | 3 |
| 179 | DOX109N | 20130603 | 0 |
| 181 | DOX122T | 20130520 | 0 |
| 184 | DOX121T | 20130411 | 0 |
| 248 | ROW1767L | 20130530 | 2 |
| 251 | ROW1765L | 20130528 | 1 |
| 268 | GWR-LAP16 | 20130531 | 3 |
| 278 | ROW1522L | 20130420 | 1 |
| 281 | ROW255L | 20130509 | 1 |
| 283 | GWR-LAP03 | 20130413 | 2 |
| 313 | GMSRT | 20130623 | 1 |
| 334 | ROW1517L | 20130523 | 1 |
| 335 | ROW1792L | 0 | 0 |
| 339 | TEXCE-PC | 0 | 0 |
+-----+-----------+-----------+----------------+
TABLE2: PATCHES
╔═════╦══════════╦══════════╦═══════════╗ ║ sid ║ PatchID ║ Approved ║ Installed ║ ╠═════╬══════════╬══════════╬═══════════╣ ║ 52 ║ a34236 ║ 1 ║ 0 ║ ║ 52 ║ b54656 ║ 1 ║ 0 ║ ║ 112 ║ ds6456 ║ 1 ║ 0 ║ ║ 112 ║ se4168 ║ 1 ║ 0 ║ ║ 112 ║ f46466 ║ 1 ║ 0 ║ ║ 248 ║ t44541 ║ 1 ║ 0 ║ ║ 248 ║ uyt446 ║ 1 ║ 0 ║ ║ 251 ║ gfd841 ║ 1 ║ 0 ║ ║ 268 ║ 54dtyd ║ 1 ║ 0 ║ ║ 268 ║ dg4495 ║ 1 ║ 0 ║ ║ 268 ║ dg4444 ║ 1 ║ 0 ║ ║ 278 ║ jhr652 ║ 1 ║ 0 ║ ║ 281 ║ h46831 ║ 1 ║ 0 ║ ║ 283 ║ 54fghf ║ 1 ║ 0 ║ ║ 283 ║ 46fhtj ║ 1 ║ 0 ║ ║ 313 ║ s44971 ║ 1 ║ 0 ║ ║ 334 ║ et4796 ║ 1 ║ 0 ║ ║ 339 ║ TEXCE-PC ║ 0 ║ 0 ║ ╚═════╩══════════╩══════════╩═══════════╝</p>
CODE:
SELECT
systems.sid, systems.Name, systems.checkdate,
SUM(0) as MissingPatches
FROM (
patches LEFT JOIN Systems
ON Systems.Sid = patches.Sid
)
WHERE (
STR_TO_DATE(systems.checkdate, '%Y%m%d') < CURDATE() - INTERVAL 14 DAY
)
GROUP BY SYSTEMS.Sid
UNION ALL
SELECT
systems.sid, systems.Name, systems.checkdate,
count(approved) as MissingPatches
FROM (
patches LEFT JOIN Systems
ON Systems.Sid = patches.Sid
)
WHERE patches.Approved = 1
AND patches.Installed = 0
AND Patches.Sid = systems.Sid
GROUP BY patches.Sid
HAVING COUNT(approved) > 0
ORDER BY Sid
RESULTS:
+-----+-----------+-----------+----------------+
| sid | Name | checkdate | MissingPatches |
+-----+-----------+-----------+----------------+
| 79 | WCLVS001 | 0 | 0 |
| 112 | TAR201 | 0 | 0 |
| 112 | TAR201 | 0 | 3 |
| 179 | DOX109N | 20130603 | 0 |
| 181 | DOX122T | 20130520 | 0 |
| 184 | DOX121T | 20130411 | 0 |
| 248 | ROW1767L | 20130530 | 0 |
| 248 | ROW1767L | 20130530 | 2 |
| 251 | ROW1765L | 20130528 | 0 |
| 251 | ROW1765L | 20130528 | 1 |
| 268 | GWR-LAP16 | 20130531 | 0 |
| 268 | GWR-LAP16 | 20130531 | 3 |
| 278 | ROW1522L | 20130420 | 0 |
| 278 | ROW1522L | 20130420 | 1 |
| 281 | ROW255L | 20130509 | 0 |
| 281 | ROW255L | 20130509 | 1 |
| 283 | GWR-LAP03 | 20130413 | 0 |
| 283 | GWR-LAP03 | 20130413 | 2 |
| 313 | GMSRT | 20130623 | 1 |
| 334 | ROW1517L | 20130523 | 0 |
| 335 | ROW1792L | 0 | 0 |
| 335 | ROW1792L | 0 | 23 |
| 339 | TEXCE-PC | 0 | 0 |
+-----+-----------+-----------+----------------+