Sql 左连接一行
我有一个SQL Server查询,如下所示:Sql 左连接一行,sql,sql-server,left-join,Sql,Sql Server,Left Join,我有一个SQL Server查询,如下所示: SELECT ListingId, hh.DateAltered FROM Listings (NOLOCK) LEFT JOIN ( SELECT h.ParentId, h.DateAltered FROM History AS h WHERE h.ParentType = 'Listing' ) hh ON hh.ParentId = Listings.Lis
SELECT ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
SELECT
h.ParentId,
h.DateAltered
FROM
History AS h
WHERE h.ParentType = 'Listing'
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
所以基本上我有两个表,列表
和历史
。Listings
表只有一行。历史记录
表有5行链接到列表
表记录(从左连接
可以看出)
当我运行上面的查询时,它返回5行。这是因为历史记录
表中有5行链接到列表
表中的1行
ListingId DateAltered
56082 2013-11-06 09:27:29.647
56082 2013-11-08 14:30:42.543
56082 2013-11-08 15:11:30.390
56082 2013-11-14 09:54:21.060
56082 2014-01-09 16:23:52.440
但是我只需要History
表中的一行,这样我就可以看到最后一个datechanged
是什么了
因此,我认为将TOP(1)
添加到LEFT JOIN
查询中同样简单:
SELECT ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
SELECT TOP(1)
h.ParentId,
h.DateAltered
FROM
History AS h
WHERE h.ParentType = 'Listing'
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
它现在返回1行(这是我想要的),但是现在datealterned
列是NULL
:
ListingId DateAltered
56082 NULL
为什么会这样?我该如何解决这个问题呢?您加入的只是历史记录中的一行,您不知道它是否有匹配的parentid。您可以将顶部(1)放在主查询中,并按日期对其排序
SELECT TOP(1) ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
SELECT
h.ParentId,
h.DateAltered
FROM
History AS h
WHERE h.ParentType = 'Listing'
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
order by hh.DateAltered desc
不过,我的建议是:
SELECT l.ListingId
, max(hh.DateAltered) as DateAltered
FROM Listings (NOLOCK) l
left join
History AS hh
ON hh.ParentId = l.ListingId
and hh.ParentType = 'Listing'
WHERE l.ListingId = 56082
group by
l.ListingId
这将为您提供每个listingid更改的最大日期。您仅使用历史记录中的一行进行连接,您不知道它是否具有匹配的parentid。您可以将顶部(1)放在主查询中,并按日期对其排序
SELECT TOP(1) ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
SELECT
h.ParentId,
h.DateAltered
FROM
History AS h
WHERE h.ParentType = 'Listing'
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
order by hh.DateAltered desc
不过,我的建议是:
SELECT l.ListingId
, max(hh.DateAltered) as DateAltered
FROM Listings (NOLOCK) l
left join
History AS hh
ON hh.ParentId = l.ListingId
and hh.ParentType = 'Listing'
WHERE l.ListingId = 56082
group by
l.ListingId
这将为您提供每个listingid更改的最大日期。您仅使用历史记录中的一行进行连接,您不知道它是否具有匹配的parentid。您可以将顶部(1)放在主查询中,并按日期对其排序
SELECT TOP(1) ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
SELECT
h.ParentId,
h.DateAltered
FROM
History AS h
WHERE h.ParentType = 'Listing'
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
order by hh.DateAltered desc
不过,我的建议是:
SELECT l.ListingId
, max(hh.DateAltered) as DateAltered
FROM Listings (NOLOCK) l
left join
History AS hh
ON hh.ParentId = l.ListingId
and hh.ParentType = 'Listing'
WHERE l.ListingId = 56082
group by
l.ListingId
这将为您提供每个listingid更改的最大日期。您仅使用历史记录中的一行进行连接,您不知道它是否具有匹配的parentid。您可以将顶部(1)放在主查询中,并按日期对其排序
SELECT TOP(1) ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
SELECT
h.ParentId,
h.DateAltered
FROM
History AS h
WHERE h.ParentType = 'Listing'
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
order by hh.DateAltered desc
不过,我的建议是:
SELECT l.ListingId
, max(hh.DateAltered) as DateAltered
FROM Listings (NOLOCK) l
left join
History AS hh
ON hh.ParentId = l.ListingId
and hh.ParentType = 'Listing'
WHERE l.ListingId = 56082
group by
l.ListingId
这将为您提供每个listingid更改的最大日期。之所以发生这种情况,是因为您选择的
Top1
记录与listings表没有相同的ID
您需要ID匹配的历史记录表中的最新记录。您可以使用行号
SELECT ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
select *
from
(
SELECT
h.ParentId,
h.DateAltered,
ROW_NUMBER() over (partition by parentid order by datealtered desc) rn
FROM
History AS h
WHERE h.ParentType = 'Listing'
) h
where rn=1
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
之所以发生这种情况,是因为您选择的
top 1
记录与列表表的ID不同
您需要ID匹配的历史记录表中的最新记录。您可以使用行号
SELECT ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
select *
from
(
SELECT
h.ParentId,
h.DateAltered,
ROW_NUMBER() over (partition by parentid order by datealtered desc) rn
FROM
History AS h
WHERE h.ParentType = 'Listing'
) h
where rn=1
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
之所以发生这种情况,是因为您选择的
top 1
记录与列表表的ID不同
您需要ID匹配的历史记录表中的最新记录。您可以使用行号
SELECT ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
select *
from
(
SELECT
h.ParentId,
h.DateAltered,
ROW_NUMBER() over (partition by parentid order by datealtered desc) rn
FROM
History AS h
WHERE h.ParentType = 'Listing'
) h
where rn=1
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
之所以发生这种情况,是因为您选择的
top 1
记录与列表表的ID不同
您需要ID匹配的历史记录表中的最新记录。您可以使用行号
SELECT ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
select *
from
(
SELECT
h.ParentId,
h.DateAltered,
ROW_NUMBER() over (partition by parentid order by datealtered desc) rn
FROM
History AS h
WHERE h.ParentType = 'Listing'
) h
where rn=1
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
如果要更改上次
日期
值,应使用MAX
和分组依据
:
SELECT ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
SELECT
h.ParentId,
MAX(h.DateAltered) AS DateAltered
FROM
History AS h
WHERE h.ParentType = 'Listing'
GROUP BY h.ParentId
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
如果要更改上次
日期
值,应使用MAX
和分组依据
:
SELECT ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
SELECT
h.ParentId,
MAX(h.DateAltered) AS DateAltered
FROM
History AS h
WHERE h.ParentType = 'Listing'
GROUP BY h.ParentId
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
如果要更改上次
日期
值,应使用MAX
和分组依据
:
SELECT ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
SELECT
h.ParentId,
MAX(h.DateAltered) AS DateAltered
FROM
History AS h
WHERE h.ParentType = 'Listing'
GROUP BY h.ParentId
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
如果要更改上次
日期
值,应使用MAX
和分组依据
:
SELECT ListingId, hh.DateAltered
FROM Listings (NOLOCK)
LEFT JOIN (
SELECT
h.ParentId,
MAX(h.DateAltered) AS DateAltered
FROM
History AS h
WHERE h.ParentType = 'Listing'
GROUP BY h.ParentId
) hh
ON hh.ParentId = Listings.ListingId
WHERE ListingId = 56082
您需要使用子查询并将值作为列返回 差不多
SELECT ListingId,
(SELECT max(h.DateAltered)
FROM History AS h
WHERE h.ParentId = Listings.ListingId
and h.ParentType = 'Listing') as LatestDateAltered
FROM Listings (NOLOCK)
WHERE ListingId = 56082
您需要使用子查询并将值作为列返回 差不多
SELECT ListingId,
(SELECT max(h.DateAltered)
FROM History AS h
WHERE h.ParentId = Listings.ListingId
and h.ParentType = 'Listing') as LatestDateAltered
FROM Listings (NOLOCK)
WHERE ListingId = 56082
您需要使用子查询并将值作为列返回 差不多
SELECT ListingId,
(SELECT max(h.DateAltered)
FROM History AS h
WHERE h.ParentId = Listings.ListingId
and h.ParentType = 'Listing') as LatestDateAltered
FROM Listings (NOLOCK)
WHERE ListingId = 56082
您需要使用子查询并将值作为列返回 差不多
SELECT ListingId,
(SELECT max(h.DateAltered)
FROM History AS h
WHERE h.ParentId = Listings.ListingId
and h.ParentType = 'Listing') as LatestDateAltered
FROM Listings (NOLOCK)
WHERE ListingId = 56082
尝试
MIN(datechanged)
?尝试MIN(datechanged)
?尝试MIN(datechanged)
?尝试MIN(datechanged)
?如果您需要从历史记录中选择更多具有最大datechanged的列,这是一个很好的解决方案。然而,在这种情况下,似乎一个简单的max/groupby就足够了,因为只有分组(maxed)值是可用的required@BrettSchneider我同意如果您需要从历史记录中选择更多列并更改最大日期,这是一个很好的解决方案。然而,在这种情况下,似乎一个简单的max/groupby就足够了,因为只有分组(maxed)值是可用的required@BrettSchneider我同意如果您需要从历史记录中选择更多列并更改最大日期,这是一个很好的解决方案。然而,在这种情况下,似乎一个简单的max/groupby就足够了,因为只有分组(maxed)值是可用的required@BrettSchneider我同意如果您需要从历史记录中选择更多列并更改最大日期,这是一个很好的解决方案。然而,在这种情况下,似乎一个简单的max/groupby就足够了,因为只有分组(maxed)值是可用的required@BrettSchneider我同意