excuse the title, i couldn't come up with something short and to the point...
原谅标题,我无法想出一些简短的事情......
I've got a table 'updates' with the three columns, text, typeid, created - text is a text field, typeid is a foreign key from a 'type' table and created is a timestamp. A user is entering an update and select the 'type' it corresponds too.
我有一个表'更新'与三列,text,typeid,created - text是一个文本字段,typeid是来自'type'表的外键,并创建一个时间戳。用户正在输入更新并选择它对应的“类型”。
There's a corresponding 'type' table with columns 'id' and 'name'.
有一个对应的'type'表,列'id'和'name'。
I'm trying to end up with a result set with as many rows as is in the 'type' table and the latest value from updates.text for the particular row in types. So if i've got 3 types, 3 rows would be returned, one row for each type and the most recent updates.text value for the type in question.
我试图得到一个结果集,其中包含'type'表中的行数,以及来自updates.text的最新值,用于类型中的特定行。因此,如果我有3种类型,将返回3行,每种类型一行,以及相关类型的最新updates.text值。
Any ideas?
thanks,
John.
3 个解决方案
#1
14
select u.text, u.typeid, u.created, t.name
from (
select typeid, max(created) as MaxCreated
from updates
group by typeid
) mu
inner join updates u on mu.typeid = u.typeid and mu.MaxCreated = u.Created
left outer join type t on u.typeid = t.typeid
#2
0
What are the actual columns you want returned?
您想要返回的实际列数是多少?
SELECT t.*,
y.*
FROM TYPE t
JOIN (SELECT u.typeid,
MAX(u.created) 'max_created'
FROM UPDATES u
GROUP BY u.typeid) x ON x.typeid = t.id
JOIN UPDATES y ON y.typeid = x.typeid
AND y.created = x.max_created
#3
0
SELECT
TYP.id,
TYP.name,
TXT.comment
FROM
dbo.Types TYP
INNER JOIN dbo.Type_Comments TXT ON
TXT.type_id = TYP.id
WHERE
NOT EXISTS
(
SELECT
*
FROM
dbo.Type_Comments TXT2
WHERE
TXT2.type_id = TYP.id AND
TXT2.created > TXT.created
)
Or:
SELECT
TYP.id,
TYP.name,
TXT.comment
FROM
dbo.Types TYP
INNER JOIN dbo.Type_Comments TXT ON
TXT.type_id = TYP.id
LEFT OUTER JOIN dbo.Type_Comments TXT2 ON
TXT2.type_id = TYP.id AND
TXT2.created > TXT.created
WHERE
TXT2.type_id IS NULL
In either case, if the created date can be identical between two rows with the same type_id then you would need to account for that.
在任何一种情况下,如果创建日期在具有相同type_id的两行之间可以相同,那么您需要考虑到这一点。
I've also assumed at least one comment per type exists. If that's not the case then you would need to make a minor adjustment for that as well.
我还假设每种类型至少存在一条评论。如果情况并非如此,那么您也需要对此进行微调。
#1
14
select u.text, u.typeid, u.created, t.name
from (
select typeid, max(created) as MaxCreated
from updates
group by typeid
) mu
inner join updates u on mu.typeid = u.typeid and mu.MaxCreated = u.Created
left outer join type t on u.typeid = t.typeid
#2
0
What are the actual columns you want returned?
您想要返回的实际列数是多少?
SELECT t.*,
y.*
FROM TYPE t
JOIN (SELECT u.typeid,
MAX(u.created) 'max_created'
FROM UPDATES u
GROUP BY u.typeid) x ON x.typeid = t.id
JOIN UPDATES y ON y.typeid = x.typeid
AND y.created = x.max_created
#3
0
SELECT
TYP.id,
TYP.name,
TXT.comment
FROM
dbo.Types TYP
INNER JOIN dbo.Type_Comments TXT ON
TXT.type_id = TYP.id
WHERE
NOT EXISTS
(
SELECT
*
FROM
dbo.Type_Comments TXT2
WHERE
TXT2.type_id = TYP.id AND
TXT2.created > TXT.created
)
Or:
SELECT
TYP.id,
TYP.name,
TXT.comment
FROM
dbo.Types TYP
INNER JOIN dbo.Type_Comments TXT ON
TXT.type_id = TYP.id
LEFT OUTER JOIN dbo.Type_Comments TXT2 ON
TXT2.type_id = TYP.id AND
TXT2.created > TXT.created
WHERE
TXT2.type_id IS NULL
In either case, if the created date can be identical between two rows with the same type_id then you would need to account for that.
在任何一种情况下,如果创建日期在具有相同type_id的两行之间可以相同,那么您需要考虑到这一点。
I've also assumed at least one comment per type exists. If that's not the case then you would need to make a minor adjustment for that as well.
我还假设每种类型至少存在一条评论。如果情况并非如此,那么您也需要对此进行微调。