如何更改 sql 查询返回的最后一行中的值?

发布于 2024-10-17 11:19:01 字数 464 浏览 2 评论 0原文

我在编写一个 SQL 查询时遇到问题,该查询返回将特定值放入最后返回行的字段中的行(此修改仅对返回的结果进行,不能对数据库记录进行修改)。

下面是一个示例:

SELECT A.a, 
       A.b, 
       A.c
  FROM A
 WHERE A.b = 10

返回的结果:

A.a |A.b |A.c
--------------
1    10   zaza
2    10   zozo
3    10   zuzu
4    10   zozo

我希望请求自动在最后一行的 Ac 字段中放入特定值,例如:

A.a |A.b |A.c
--------------
1    10   zaza
2    10   zozo
3    10   zuzu
4    10   XXXX

I'm having trouble writing an SQL query that returns rows for which a specific value is put in a field of the last returned row (this modification is only made on the results returned, there must be no modification of the database records).

Here is an example :

SELECT A.a, 
       A.b, 
       A.c
  FROM A
 WHERE A.b = 10

Results returned :

A.a |A.b |A.c
--------------
1    10   zaza
2    10   zozo
3    10   zuzu
4    10   zozo

I would like the request to automatically put a specific value in the A.c field of the last row, such as :

A.a |A.b |A.c
--------------
1    10   zaza
2    10   zozo
3    10   zuzu
4    10   XXXX

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

复古式 2024-10-24 11:19:01

如果行的顺序由列 a 确定,则以下内容将替换“最后”行中的 C 值。

select a
      ,b
      ,case when row_number() over(order by a) = count(*) over() then 'XXX' 
                                                                 else c end as c
  from v
order by a;   

If the order of the rows are determined by column a, the following would replace the C-value in the "last" row.

select a
      ,b
      ,case when row_number() over(order by a) = count(*) over() then 'XXX' 
                                                                 else c end as c
  from v
order by a;   
差↓一点笑了 2024-10-24 11:19:01

首先,您需要一个 ORDER BY 语句。 Oracle 不保证行将按照输入的顺序返回,因此您需要将 SQL 更改为:

select A.a, A.b, A.c
From A
Where A.b = 10
ORDER BY A.a;

以使“最后”行的概念具有任何意义。

这是一种方法...很可能有比这更好的方法...

SELECT A, B, C
FROM (
SELECT A.A, A.B, A.C
FROM   A
WHERE  A.A <> (SELECT MAX(A.A) FROM A WHERE A.B = 10)
AND    A.B = 10 
UNION ALL
SELECT A.A, A.B, 'whatever'
FROM   A
WHERE  A.A = (SELECT MAX(A.A) FROM A WHERE A.B = 10)
AND    A.B = 10
)
ORDER BY A.A

如果有 2 行 AA 相同,您不会说行为应该是什么(如果这可能吗?)

Firstly you need an ORDER BY statement. Oracle does not guarantee that rows will be returned in the order that they were entered, so you need to change your SQL to:

select A.a, A.b, A.c
From A
Where A.b = 10
ORDER BY A.a;

for the concept of a "last" row to have any meaning.

Here's one way... there may well be a better way of doing it than this...

SELECT A, B, C
FROM (
SELECT A.A, A.B, A.C
FROM   A
WHERE  A.A <> (SELECT MAX(A.A) FROM A WHERE A.B = 10)
AND    A.B = 10 
UNION ALL
SELECT A.A, A.B, 'whatever'
FROM   A
WHERE  A.A = (SELECT MAX(A.A) FROM A WHERE A.B = 10)
AND    A.B = 10
)
ORDER BY A.A

You don't say what the behaviour should be if there's 2 rows where A.A are the same (if this is possible?)

海夕 2024-10-24 11:19:01

稍微不那么混乱:

SELECT A,
       B,
       CASE
         WHEN A = (SELECT MAX(A) FROM TEST WHERE B = 10) THEN 'XXXX'
         ELSE C
       END C
FROM TEST

A bit less messy:

SELECT A,
       B,
       CASE
         WHEN A = (SELECT MAX(A) FROM TEST WHERE B = 10) THEN 'XXXX'
         ELSE C
       END C
FROM TEST
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文