如何在 SQL 查询中添加列,包括。左外连接
我有这个查询:
SELECT p.ProductName,
dt.MaxTimeStamp,
p.Responsible
FROM Product p
LEFT JOIN (SELECT ProductID, MAX(TimeStamp) AS MaxTimeStamp
FROM StateLog
WHERE State = 0
GROUP BY ProductID, State) dt ON p.ProductID = dt.ProductID
ORDER BY p.ProductName;
它的工作原理是这样的,但现在我也需要选择“状态”。
棘手的部分是,我只想要“状态”为假的最新“时间戳”。 但现在我还需要最新“时间戳”的“状态”。
我尝试了这个:
SELECT p.ProductName, dt.State, dt.MaxTimeStamp, p.Responsible
FROM Product p
LEFT JOIN (SELECT ProductID, MAX(TimeStamp) AS MaxTimeStamp, State
FROM StateLog
WHERE State = 0
GROUP BY ProductID, State) dt ON p.ProductID =dt.ProductID
ORDER BY p.ProductName;
但它不起作用,因为它给了我最新“时间戳”的“状态”。
所以我希望有一些聪明的头脑可以帮助我。我猜这个问题要么非常简单,要么很难解决。
I have this Query:
SELECT p.ProductName,
dt.MaxTimeStamp,
p.Responsible
FROM Product p
LEFT JOIN (SELECT ProductID, MAX(TimeStamp) AS MaxTimeStamp
FROM StateLog
WHERE State = 0
GROUP BY ProductID, State) dt ON p.ProductID = dt.ProductID
ORDER BY p.ProductName;
It works like it should, but now I need to SELECT "State" out too.
The tricky part is, that I only want the lastest "TimeStamp" where "State" was false.
But now I also need the "State" for the lastest "TimeStamp".
I tried this:
SELECT p.ProductName, dt.State, dt.MaxTimeStamp, p.Responsible
FROM Product p
LEFT JOIN (SELECT ProductID, MAX(TimeStamp) AS MaxTimeStamp, State
FROM StateLog
WHERE State = 0
GROUP BY ProductID, State) dt ON p.ProductID =dt.ProductID
ORDER BY p.ProductName;
But it didn't work, because it gave me the "State" for the lastest "TimeStamp".
So I hope there is some clever heads out there that can help me. I'm guessing that this is either very simple or very hard to solve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
努力破译您正在寻找的内容,但阅读字里行间是否可以总结为:
1)最近的 StateLog.Timestamp,其中 State 为零
2)最近的 StateLog.Timestamp 的状态
在这种情况下,以下内容(相当丑陋)查询可能会起作用。假设您的分组依据中的“状态”列是“状态”的印刷错误,因为它没有返回到任何地方。
Struggling to decipher what you're looking for but reading between the lines could it be summarised as:
1) Most recent StateLog.Timestamp where State is zero
2) State of most recent StateLog.Timestamp
In which case, the following (rather ugly) query would probably work. Assumed the 'Status' column in your group by was a misprint of 'State' as its not returned anywhere.
tvanfosson 和 OMG Ponies 的出色格式化工作。
使用
GROUP BY
时,每列都需要:1. 应用聚合函数,或者
2. 出现在
GROUP BY
子句中。我不知道
Status
是什么,但我假设您需要它。因此,这是您的查询应该如何显示的示例:
但是所有这些都是愚蠢的,因为正如评论中提到的,您按
State = 0
进行过滤,因此您的查询不可能返回任何内容除了 0 以外的状态。Great formatting work by tvanfosson and OMG Ponies.
When using
GROUP BY
every column needs to either:1. Have an aggregate function applied to it, or
2. Appear in the
GROUP BY
clause.I don't know what
Status
is, but I'm assuming you need it.So this is an example of how your query should look:
But all of this is foolishness because, as mentioned in a comment, you filter by
State = 0
so there is no possibility that your query will return anything other than a 0 for State.我想知道您的逻辑是否有点混乱。我看到您正在按状态分组,但您不在任何地方使用状态。
我无法对此进行测试,但如果您发布表创建脚本和数据填充脚本,我们可以快速测试它。
I am wondering if your logic is a bit confused .I see you are grouping by status but you dont use status anywhere.
I could not test this out but if you post table creation script and data population script we can test it out quickly.