SQL多部分标识符错误,尝试使用子查询中的字段进行筛选。

huangapple go评论71阅读模式
英文:

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:

SQL多部分标识符错误,尝试使用子查询中的字段进行筛选。

But what I want to get is this only:

SQL多部分标识符错误,尝试使用子查询中的字段进行筛选。

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:

SQL多部分标识符错误,尝试使用子查询中的字段进行筛选。

But what I want to get is this only:

SQL多部分标识符错误,尝试使用子查询中的字段进行筛选。

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 作为 LastReadingIdR.SupplyPointInstallationId
     
      (
        选择 *, 
          ROW_NUMBER() OVER (PARTITION BY SupplyPointInstallationId ORDER BY LastDate DESC) rn
         Reading
      ) R 加入 @supplyPointIds S
     S.id = R.SupplyPointInstallationId
    其中 R.rn = 1

[查看演示][1]

这也可以写成:

    选择 LastReadingIdSupplyPointInstallationId
      
    (
      选择 R.Id 作为 LastReadingIdR.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

See demo

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

huangapple
  • 本文由 发表于 2023年3月15日 18:55:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定