简体   繁体   English

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

[英]Reuse a subquery result in another column in main query

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.在下面的查询中,列 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

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.在实际查询中,大约有 10 个子查询需要在另一个子查询中重用。 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.就我而言,很难用所有必需的值构建另一个表,以便从中获取主查询中的值,因为主表很大,并且要从主表中获取的值仅适用于基于的特定代码在 Trans 表中过滤的记录上。

EDIT 1:编辑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.我不想在这里使用 JOIN,因为带有多个 JOIN 的实际查询大约需要 60 秒,而带有子查询的查询大约需要 7-8 秒。 The only inconvenience is that I have to write the same subquery multiple times.唯一的不便是我必须多次编写相同的子查询。

EDIT 2:编辑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.我从外部数据命令中的 Excel 文件中调用此存储过程。 I prefer to encapsulate it in Stored Procedure, instead of writing the query directly in Excel file.我更喜欢将其封装在存储过程中,而不是直接在 Excel 文件中编写查询。

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?有没有办法不使用游标或临时表或编写程序逻辑来将第一个子查询的结果提取到变量中,并将该变量的值用于列 GLName 和 GLDesc?

My desired 'imaginary' query inside the SP is like this:我在 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

Is there any real query syntax corresponding to my imaginary query above?是否有任何与我上面想象的查询相对应的实际查询语法?

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;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM