我的表格如下:
SELECT *
FROM checkresult
WHERE timestamp IN (
SELECT MAX(timestamp)
FROM checkresult
WHERE test_object_id = 1
GROUP BY name_id
);
我想要每个组的最后一条记录(name_id)。但我只想要testobject_id = 1
的结果但这似乎不起作用。我感谢你的每一个帮助。
Id Timestamp name_id testobject_id
----------------------------------------------------
2 | 2017-12-15 18:36:46 | 1 | 1
3 | 2017-12-15 18:36:46 | 2 | 1
期望的输出:
/* Collect all divs into a NodeList with
|| querySelectorAll()
|| Then convert the NodeList into an array with
|| Array.from()
*/
var divs = Array.from(document.querySelectorAll('div'));
/* Run the array method map() on divs[]
|| map() will run a function on each element of
|| an array. It will return a new array that
|| comprises of each of the functions' return.
*/
var INDEX_VALUE = divs.map(function(div, idx, divs) {
// Get the text of each div
var txt = div.textContent;
// Display INDEX VALUE and text in console
console.log('Index: ' + idx + ' Content: '+txt);
// Return an array of all the results
return idx;
});
console.log(INDEX_VALUE);
答案 0 :(得分:1)
我认为您只想在最外层的查询中使用where
:
SELECT cr.*
FROM checkresult cr
WHERE cr.test_object_id = 1 AND
cr.timestamp = (SELECT MAX(cr2.timestamp)
FROM checkresult cr2
WHERE cr2.name_id = cr.name_id
);
注意使用相关子查询的更改。这很重要,因为不同名称的行可以具有相同的时间戳值。一个名字的最新内容可能不是最新的。
答案 1 :(得分:0)
改为使用内部联接:
SELECT *
FROM checkresult r
INNER JOIN (
SELECT name_id, MAX(timestamp)
FROM checkresult
WHERE test_object_id = 1
GROUP BY name_id
) g ON r. name_id = g.name_id
and r.timestamp = g.timestamp
WHERE r.test_object_id = 1
答案 2 :(得分:0)
这样的事情:
SELECT
Id
, Timestamp
, name_id
, test_object_id
FROM
(
SELECT
Id
, Timestamp
, name_id
, test_object_id
, ROW_NUMBER() OVER (PARTITION BY name_id ORDER BY Timestamp DESC) R
FROM checkresult
WHERE test_object_id = 1
) Q
WHERE R = 1
答案 3 :(得分:0)
select c.*
from checkresult c
where c.timestamp = (
select max(timestamp) from checkresult where name_id = c.name_id
)
group by c.name_id