英文:
SQL Multipart identifier error when attempting to filter using a field from a subquery
问题
Ok, I'm not very good with SQL, so I don't know how to solve this one. I have this query:
DECLARE @supplyPointIds TABLE
(
id UNIQUEIDENTIFIER
)
INSERT INTO @supplyPointIds (id)
VALUES ('1DC9A405-4EC5-4379-BB2C-110973A9936B'),
('65745684-7D00-4D8A-9735-2C5BA29852B0')
SELECT R.Id AS LastReadingId, R.SupplyPointInstallationId
FROM Reading R
WHERE R.SupplyPointInstallationId IN (SELECT id FROM @supplyPointIds)
ORDER BY R.SupplyPointInstallationId, R.LastDate DESC
And it produces this:
But what I want to get is this only:
So, I wrote this query:
DECLARE @supplyPointIds TABLE
(
id UNIQUEIDENTIFIER
)
INSERT INTO @supplyPointIds (id)
VALUES ('1DC9A405-4EC5-4379-BB2C-110973A9936B'),
('65745684-7D00-4D8A-9735-2C5BA29852B0')
SELECT ReadingId.Id AS LastReadingId, R.SupplyPointInstallationId
FROM Reading R,
(SELECT TOP 1 RID.Id
FROM Reading RID
WHERE RID.SupplyPointInstallationId = R.SupplyPointInstallationId
ORDER BY RID.LastDate) AS ReadingId -- Only the most recent reading
WHERE R.SupplyPointInstallationId IN (SELECT id FROM @supplyPointIds)
ORDER BY R.SupplyPointInstallationId DESC
But, unfortunately, I get this error:
The multi-part identifier "R.SupplyPointInstallationId" could not be bound.
How can I do to get only those two records I want?
英文:
Ok, I'm not very good with SQL, so I don't know how to solve this one. I have this query:
DECLARE @supplyPointIds TABLE
(
id UNIQUEIDENTIFIER
)
INSERT INTO @supplyPointIds (id)
VALUES ('1DC9A405-4EC5-4379-BB2C-110973A9936B'),
('65745684-7D00-4D8A-9735-2C5BA29852B0')
SELECT R.Id AS LastReadingId, R.SupplyPointInstallationId
FROM Reading R
WHERE R.SupplyPointInstallationId IN (SELECT id FROM @supplyPointIds)
ORDER BY R.SupplyPointInstallationId, R.LastDate DESC
And it produces this:
But what I want to get is this only:
So, I wrote this query:
DECLARE @supplyPointIds TABLE
(
id UNIQUEIDENTIFIER
)
INSERT INTO @supplyPointIds (id)
VALUES ('1DC9A405-4EC5-4379-BB2C-110973A9936B'),
('65745684-7D00-4D8A-9735-2C5BA29852B0')
SELECT ReadingId.Id AS LastReadingId, R.SupplyPointInstallationId
FROM Reading R,
(SELECT TOP 1 RID.Id
FROM Reading RID
WHERE RID.SupplyPointInstallationId = R.SupplyPointInstallationId
ORDER BY RID.LastDate) AS ReadingId -- Only the most recent reading
WHERE R.SupplyPointInstallationId IN (SELECT id FROM @supplyPointIds)
ORDER BY R.SupplyPointInstallationId DESC
But, unfortunately, I get this error:
> The multi-part identifier "R.SupplyPointInstallationId" could not be
> bound.
How can I do to get only those two records I want?
答案1
得分: 1
以下是您要的代码的中文翻译部分:
尝试连接这两个表,并使用row_number函数如下:
选择 R.Id 作为 LastReadingId,R.SupplyPointInstallationId
从
(
选择 *,
ROW_NUMBER() OVER (PARTITION BY SupplyPointInstallationId ORDER BY LastDate DESC) rn
从 Reading
) R 加入 @supplyPointIds S
在 S.id = R.SupplyPointInstallationId
其中 R.rn = 1
[查看演示][1]
这也可以写成:
选择 LastReadingId,SupplyPointInstallationId
从
(
选择 R.Id 作为 LastReadingId,R.SupplyPointInstallationId,
ROW_NUMBER() OVER (PARTITION BY R.SupplyPointInstallationId ORDER BY R.LastDate DESC) rn
从 Reading R 加入 @supplyPointIds S
在 S.id = R.SupplyPointInstallationId
) t
其中 rn = 1
[1]: https://dbfiddle.uk/9xghhHhf
英文:
Try to join the two tables and use the row_number function as the following:
SELECT R.Id AS LastReadingId, R.SupplyPointInstallationId
FROM
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY SupplyPointInstallationId ORDER BY LastDate DESC) rn
FROM Reading
) R JOIN @supplyPointIds S
ON S.id = R.SupplyPointInstallationId
WHERE R.rn = 1
This can be also written as:
SELECT LastReadingId, SupplyPointInstallationId
From
(
SELECT R.Id AS LastReadingId, R.SupplyPointInstallationId,
ROW_NUMBER() OVER (PARTITION BY R.SupplyPointInstallationId ORDER BY R.LastDate DESC) rn
FROM Reading R JOIN @supplyPointIds S
ON S.id = R.SupplyPointInstallationId
) t
WHERE rn = 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论