我正在尝试将来自count语句的结果与来自其他条件(多个表)的结果相结合,并且正在敲打我的脑袋。
我在这方面的技能不强,达到这一点很有意思。这个阶段的最后一个问题是,虽然我看到了我正在寻找的结果,但我的UNION ALL并没有合并数据。
您可以在底部表(结果)中看到,我正在获得同一系统的多个实例。我意识到我两次调用相同的数量,并且它可能正确地试图向我显示这两个数字,但我所有改变这些的努力都让我遇到了其他问题。
最后,我还想在其他表格中添加至少两个以上的计数,但希望一旦过去就不会那么具有挑战性。 (我也试图获取计数不适用的“systemid”数据,我只希望它返回那些与计数为一或多的计数特别相关的项目。我已复制下面的示例表和代码。
任何帮助将不胜感激!
戴夫
+-----+-----------+-----------+----------------+
| 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 |
+-----+-----------+-----------+----------------+
╔═════╦══════════╦══════════╦═══════════╗ ║sid║PatchID║已批准║已安装║ ╠═════╬══════════╬══════════╬═══════════╣ ║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║ ╚═════╩══════════╩══════════╩═══════════╝
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
+-----+-----------+-----------+----------------+
| 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 |
+-----+-----------+-----------+----------------+
答案 0 :(得分:0)
我确信有更好的方法可以做到这一点,但我认为这会给你你想要的东西。只需将您的UNION
查询作为子选择,然后SUM
MissingPatches列。
SELECT s.id, Name, checkdate, SUM(MissingPatches) as MissingPatches
FROM
(
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
) as x
GROUP BY s.id, Name, checkdate