重复使用主查询中的另一列中的子查询结果

发布于 2025-01-31 19:32:46 字数 1604 浏览 4 评论 0原文

在下面的查询中,列GLNAME和GLDESC获取相同的值,但是我需要具有相同的值,带有2个不同的列名,以满足预定义的Excel模板的需求。

SELECT T0.GLCode, (SELECT ST0.Name From GL ST0 WHERE ST0.Code = T0.GLCode) AS GLName, 
(SELECT ST0.Name FROM GL ST0 WHERE ST0.Code = T0.GLCode) AS GLDesc
FROM Trans T0

如何重复使用第一个子查询的结果来避免编写第二个子查询? 这是一个简化的示例。在实际查询中,大约有10个子查询需要在另一个子查询中重复使用。在我的情况下,很难构建另一个表,其中所有必需的值都可以从主查询中获取值,因为主表非常大,并且要从主表中获取的值仅用于基于基于的特定代码在Trans表的过滤记录上。

编辑1:

我专门询问有关子查询的重用。我不想在这里使用加入,因为带有多个连接的真实查询大约需要60秒,而用子查询的查询大约需要7-8秒。唯一的不便是我必须多次编写相同的子查询。

编辑2:

查询在存储过程中,因此:

CREATE PROCEDURE [dbo].[My_Query_Proc]
AS
BEGIN

    SELECT T0.GLCode, (SELECT ST0.Name From GL ST0 WHERE ST0.Code = T0.GLCode) AS GLName, 
    (SELECT ST0.Name FROM GL ST0 WHERE ST0.Code = T0.GLCode) AS GLDesc
    FROM Trans T0

END

我更喜欢在存储过程中使用单个查询。 我从外部数据命令中的Excel文件中调用此存储过程。我更喜欢将其封装在存储过程中,而不是将查询直接编写在Excel文件中。

如果我尝试添加一个变量:

DECLARE @GLName NVARCHAR(255)

并将查询更改为:

    SELECT T0.GLCode, @GLName = (SELECT ST0.Name From GL ST0 WHERE ST0.Code = T0.GLCode) AS GLName, 
    (SELECT ST0.Name FROM GL ST0 WHERE ST0.Code = T0.GLCode) AS GLDesc
    FROM Trans T0

我会得到语法错误。 是否可以不使用光标或温度表或编写程序逻辑将第一个子查询的结果获取变量,并将该变量的值用于列GLNAME以及GLDESC?

SP内部我想要的“虚构”查询是这样的:

CREATE PROCEDURE [dbo].[My_Query_Proc]
AS
BEGIN

    DECLARE @GLName NVARCHAR(255)

    SELECT T0.GLCode, @GLName = (SELECT ST0.Name From GL ST0 WHERE ST0.Code = T0.GLCode) AS GLName, 
    @GLName AS GLDesc
    FROM Trans T0

END

是否有与我假想的查询相对应的真实查询语法?

In the below query, the columns GLName and GLDesc fetch the same value, but I need to have the same value with 2 different column names to satisfy the requirement of a predefined Excel template.

SELECT T0.GLCode, (SELECT ST0.Name From GL ST0 WHERE ST0.Code = T0.GLCode) AS GLName, 
(SELECT ST0.Name FROM GL ST0 WHERE ST0.Code = T0.GLCode) AS GLDesc
FROM Trans T0

How can I reuse the result of the 1st subquery to avoid writing the 2nd subquery?
This is a simplified example. In the real query, around 10 subqueries need to be reused in another subquery. In my case, it is difficult to build another table with all the required values from which to fetch the values in main query, as the master tables are huge and the values to be fetched from the master tables are only for specific codes which are based on the filtered records from the Trans table.

EDIT 1:

I am specifically asking about reuse of subquery. I do not want to use JOINs here because the real query with several JOINs takes around 60 seconds, whereas the query with subquery takes about 7-8 seconds. The only inconvenience is that I have to write the same subquery multiple times.

EDIT 2:

The query is inside a Stored Procedure, like this:

CREATE PROCEDURE [dbo].[My_Query_Proc]
AS
BEGIN

    SELECT T0.GLCode, (SELECT ST0.Name From GL ST0 WHERE ST0.Code = T0.GLCode) AS GLName, 
    (SELECT ST0.Name FROM GL ST0 WHERE ST0.Code = T0.GLCode) AS GLDesc
    FROM Trans T0

END

I prefer to use a single query in the Stored Procedure.
I call this Stored Procedure from an Excel File in External Data command. I prefer to encapsulate it in Stored Procedure, instead of writing the query directly in Excel file.

If I try adding a variable:

DECLARE @GLName NVARCHAR(255)

and changing the query to:

    SELECT T0.GLCode, @GLName = (SELECT ST0.Name From GL ST0 WHERE ST0.Code = T0.GLCode) AS GLName, 
    (SELECT ST0.Name FROM GL ST0 WHERE ST0.Code = T0.GLCode) AS GLDesc
    FROM Trans T0

I get syntax error.
Is there a way without using cursors or temp tables or writing procedural logic to fetch the result of 1st subquery into a variable, and use that variable's value for the column GLName as well as GLDesc?

My desired 'imaginary' query inside the SP is like this:

CREATE PROCEDURE [dbo].[My_Query_Proc]
AS
BEGIN

    DECLARE @GLName NVARCHAR(255)

    SELECT T0.GLCode, @GLName = (SELECT ST0.Name From GL ST0 WHERE ST0.Code = T0.GLCode) AS GLName, 
    @GLName AS GLDesc
    FROM Trans T0

END

Is there any real query syntax corresponding to my imaginary query above?

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

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

发布评论

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

评论(2

唔猫 2025-02-07 19:32:46

将您的子查询放入 apply 中可供重复使用:

select t.GLCode, g.GLName, g.GLName GLDesc
from Trans t
outer apply (
  select [Name] GLName
  from GL
  where GL.Code = t.GLCode
)g;

Putting your subquery in an apply makes it available for reuse:

select t.GLCode, g.GLName, g.GLName GLDesc
from Trans t
outer apply (
  select [Name] GLName
  from GL
  where GL.Code = t.GLCode
)g;
早茶月光 2025-02-07 19:32:46

然后使用适当的加入:

SELECT T0.GLCode, ST0.Name AS GLName, ST0.Name AS GLDesc
FROM Trans T0
    INNER JOIN GL ST0 ON T0.GLCode = ST0.Code

Just use proper join then:

SELECT T0.GLCode, ST0.Name AS GLName, ST0.Name AS GLDesc
FROM Trans T0
    INNER JOIN GL ST0 ON T0.GLCode = ST0.Code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文