如何正确选择子查询

发布于 2025-01-20 20:20:26 字数 482 浏览 0 评论 0原文

我有两个查询,可以给我一个条目。我该如何选择表上的两个?

query1:从[table1] =&gt中选择max([column3]); 42 query2:从[table1] =&gt中选择顶部1 [column1]; 'test'

我想要一个像这样的结果

集1 Result1Result2
42'test'

但是如何正确执行呢?我可以以某种方式从无处选择吗?

I have two queries that give me back a single entry. How can I select both of these as on table?

query1: Select max([column3]) from [table1] => 42
query2: Select Top 1 [column1] from [table1] => 'test'

I want a resultset like this

result1result2
42'test'

But how to do it correctly? Can I maybe select from nowhere somehow?

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

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

发布评论

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

评论(1

雪花飘飘的天空 2025-01-27 20:20:26

您可以使用row_number,两次:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY column3 DESC) rn1,
              ROW_NUMBER() OVER (ORDER BY some_col) rn2
    FROM table1
)

SELECT MAX(CASE WHEN rn1 = 1 THEN column3 END) AS result1,
       MAX(CASE WHEN rn2 = 1 THEN column1 END) AS result2
FROM cte;

请注意,我假设存在列some_col您打算用于选择column1值。

You could use ROW_NUMBER, twice:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY column3 DESC) rn1,
              ROW_NUMBER() OVER (ORDER BY some_col) rn2
    FROM table1
)

SELECT MAX(CASE WHEN rn1 = 1 THEN column3 END) AS result1,
       MAX(CASE WHEN rn2 = 1 THEN column1 END) AS result2
FROM cte;

Note that I assume there exists a column some_col which you intend to use for choosing the column1 value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文