我有一个非常简单的Maria DB用于家庭自动化。这是我的ER的基本摘录:
<div id="my-container">
<div class="container h-40">
<div class="row align-items-center mt-3">
<div class="col-6 text-center">
<img width="150" height="150" src="img/user-placeholder.svg">
<p class="mt-2 mb-0">pholder</p>
</div>
<div class="col-6">
<div class="mb-2">
<div class="row">
<div class="col-6 text-center">pholder</div>
<div class="col-6 text-center">pholder</div>
</div>
<div class="row">
<div class="col-6 text-center">Friends</div>
<div class="col-6 text-center">Posts</div>
</div>
</div>
<butto class="btn btn-transparent mt-2">Click</button>
</div>
</div>
<br>
<p class="text-center m-0">Contents</p>
<hr class="mt-0 mb-2">
</div>
<div id="filling" class="h-60 text-center">
<div class="sample">
<img width="270" height="270" src="my_img.jpg">
<p>coding</p>
</div>
<div class="sample">
<img width="270" height="270" src="my_img.jpg">
<p>coding</p>
</div>
<div class="sample">
<img width="270" height="270" src="my_img.jpg">
<p>coding</p>
</div>
</div>
</div>
#filling{
display: flex;
flex-direction: column;
overflow-y: scroll;
max-height: 50%;
}
html {
height: 100%
}
body {
height: 100%;
}
#my-container {
height: 100%;
}
是一个日志表。它用于跟踪在不同时间执行的非常不同的动作(例如温度测量,关门,打开电源开关等)。
我使用以下查询来获取有趣的数据:
TelldusActionsPerformed
我想优化查询,以仅返回SELECT
TelldusActionsPerformed.PerformedTime AS TelldusActionsPerformed_PerformedTime,
TelldusUnits.Name AS TelldusUnits_Name,
TelldusActionValues.ActionValue AS TelldusActionValues_ActionValue,
TelldusActionValueTypes.Name AS TelldusActionValueTypes_Name
FROM
TelldusActions
INNER JOIN TelldusUnits ON TelldusActions.FK_TelldusUnit_Id = TelldusUnits.Id
INNER JOIN TelldusActionTypes ON TelldusActions.FK_TelldusActionType_Id = TelldusActionTypes.Id
INNER JOIN TelldusActionValues ON TelldusActions.FK_TelldusActionValue_Id = TelldusActionValues.Id
INNER JOIN TelldusActionValueTypes ON TelldusActionValues.FK_TelldusActionValueType_Id = TelldusActionValueTypes.Id
INNER JOIN TelldusActionsPerformed ON TelldusActionsPerformed.FK_TelldusAction_Id = TelldusActions.Id
WHERE TelldusUnits.Name IN ("Grovkök golvtermostat", "Huvudtermostat", "Uterum golvtermostat")
AND TelldusActionValueTypes.Name IN ("watt","temp")
ORDER BY TelldusActionsPerformed.PerformedTime DESC
+ TelldusUnits.Name
字段的绝对最新(时间)唯一组合:
我相信我需要TelldusActionValueTypes.Name
才能获得独特的组合吗?是否可以仅使用SQL将GROUP BY TelldusUnits.Name, TelldusActionValueTypes.Name
+ TelldusUnits.Name
字段的搜索结果限制为最新的唯一组合?怎么样?
如果我开箱即用地运行Murillo先生的脚本(我只添加了FROM_UNIXTIME(),在我的原始问题中忘记了它):
TelldusActionValueTypes.Name
SELECT
FROM_UNIXTIME(MAX(TelldusActionsPerformed.PerformedTime)) AS TelldusActionsPerformed_PerformedTime,
TelldusUnits.Name AS TelldusUnits_Name,
TelldusActionValues.ActionValue AS TelldusActionValues_ActionValue,
TelldusActionValueTypes.Name AS TelldusActionValueTypes_Name
FROM
TelldusActions
INNER JOIN TelldusUnits ON TelldusActions.FK_TelldusUnit_Id = TelldusUnits.Id
INNER JOIN TelldusActionTypes ON TelldusActions.FK_TelldusActionType_Id = TelldusActionTypes.Id
INNER JOIN TelldusActionValues ON TelldusActions.FK_TelldusActionValue_Id = TelldusActionValues.Id
INNER JOIN TelldusActionValueTypes ON TelldusActionValues.FK_TelldusActionValueType_Id = TelldusActionValueTypes.Id
INNER JOIN TelldusActionsPerformed ON TelldusActionsPerformed.FK_TelldusAction_Id = TelldusActions.Id
WHERE TelldusUnits.Name IN ("Grovkök golvtermostat", "Huvudtermostat", "Uterum golvtermostat")
AND TelldusActionValueTypes.Name IN ("watt","temp")
GROUP BY TelldusUnits.Name, TelldusActionValues.ActionValue, TelldusActionValueTypes.Name
ORDER BY TelldusActionsPerformed.PerformedTime DESC
使我感到困惑。根据{{1}},该表似乎不是后代吗?
(ORDER BY TelldusActionsPerformed.PerformedTime DESC
,UNIX时间戳记的数据类型为int(11))
有关Murillo先生的通缉令的下一次更新使我有了更好的选择:
PerformedTime
仍然有一个细节需要注意(根据我的原始问题:我只希望查询返回PerformedTime
+ SELECT
FROM_UNIXTIME(MAX(TelldusActionsPerformed.PerformedTime)) AS TelldusActionsPerformed_PerformedTime,
TelldusUnits.Name AS TelldusUnits_Name,
TelldusActionValues.ActionValue AS TelldusActionValues_ActionValue,
TelldusActionValueTypes.Name AS TelldusActionValueTypes_Name
FROM
TelldusActions
INNER JOIN TelldusUnits ON TelldusActions.FK_TelldusUnit_Id = TelldusUnits.Id
INNER JOIN TelldusActionTypes ON TelldusActions.FK_TelldusActionType_Id = TelldusActionTypes.Id
INNER JOIN TelldusActionValues ON TelldusActions.FK_TelldusActionValue_Id = TelldusActionValues.Id
INNER JOIN TelldusActionValueTypes ON TelldusActionValues.FK_TelldusActionValueType_Id = TelldusActionValueTypes.Id
INNER JOIN TelldusActionsPerformed ON TelldusActionsPerformed.FK_TelldusAction_Id = TelldusActions.Id
WHERE TelldusUnits.Name IN ("Grovkök golvtermostat", "Huvudtermostat", "Uterum golvtermostat")
AND TelldusActionValueTypes.Name IN ("watt","temp")
GROUP BY TelldusUnits.Name, TelldusActionValues.ActionValue, TelldusActionValueTypes.Name
ORDER BY MAX(TelldusActionsPerformed.PerformedTime) DESC
字段的绝对最新(时间)唯一组合。 / p>
答案 0 :(得分:2)
是的,如果您想要最新的时间,则需要添加一个GROUP BY子句,而不考虑TelldusActionsPerformed.PerformedTime。请尝试此脚本
Phones$wt <- rowMeans(subset(Phones, select = c(price, users, rating, market, years)), na.rm = TRUE)
答案 1 :(得分:0)
即使Murillos不能完全回答我的问题,我还是得到了他的真正帮助(我仍然认为这是一个很好的答案)。这是我最后使用的查询,(希望)回答了我的原始问题:
SELECT
OrderedResultSet.TelldusActionsPerformed_PerformedTime,
OrderedResultSet.TelldusUnits_Name,
OrderedResultSet.TelldusActionValues_ActionValue,
OrderedResultSet.TelldusActionValueTypes_Name
FROM (
SELECT
FROM_UNIXTIME(MAX(TelldusActionsPerformed.PerformedTime)) AS TelldusActionsPerformed_PerformedTime,
TelldusUnits.Name AS TelldusUnits_Name,
TelldusActionValues.ActionValue AS TelldusActionValues_ActionValue,
TelldusActionValueTypes.Name AS TelldusActionValueTypes_Name
FROM
TelldusActions
INNER JOIN TelldusUnits ON TelldusActions.FK_TelldusUnit_Id = TelldusUnits.Id
INNER JOIN TelldusActionTypes ON TelldusActions.FK_TelldusActionType_Id = TelldusActionTypes.Id
INNER JOIN TelldusActionValues ON TelldusActions.FK_TelldusActionValue_Id = TelldusActionValues.Id
INNER JOIN TelldusActionValueTypes ON TelldusActionValues.FK_TelldusActionValueType_Id = TelldusActionValueTypes.Id
INNER JOIN TelldusActionsPerformed ON TelldusActionsPerformed.FK_TelldusAction_Id = TelldusActions.Id
WHERE TelldusUnits.Name IN ("Grovkök golvtermostat", "Huvudtermostat", "Uterum golvtermostat")
AND TelldusActionValueTypes.Name IN ("watt","temp")
GROUP BY TelldusUnits.Name, TelldusActionValues.ActionValue, TelldusActionValueTypes.Name
ORDER BY MAX(TelldusActionsPerformed.PerformedTime) DESC
) as OrderedResultSet
GROUP BY OrderedResultSet.TelldusUnits_Name, OrderedResultSet.TelldusActionValueTypes_Name