[英]How do i use a selected column as a variable for the next selection of the same sub-query
I am trying write a query to create a report and for the report, I need to get the manager of the user who created a form on our system.我正在尝试编写查询来创建报告,对于报告,我需要获取在我们的系统上创建表单的用户的经理。 Because the system is old and changed a lot, there is no easy way to get the manager of the user or get their manager from the time when they created the form.因为系统老旧且变化很大,所以没有简单的方法来获取用户的经理或从他们创建表单时获取他们的经理。 So, the person before me created a function for the job but it is costly.因此,我之前的人为这项工作创建了 function,但成本很高。 It takes way too long to get a result from the function fn_getUserMan
.从 function fn_getUserMan
获得结果需要很长时间。 I want to call the function once and then use the result on my other selections as a variable and because it is dependent on Frm
, I cannot take it out of the sub-query and have it on the main query and set it on a local variable and sql wont let me set the variable on the sub-query.我想调用 function 一次,然后在我的其他选择中使用结果作为变量,因为它依赖于Frm
,我不能将它从子查询中取出并放在主查询中并将其设置在本地变量和 sql 不会让我在子查询上设置变量。
So my question is this: Is it possible to use FnMAN
in my next selection as i tried in the query.所以我的问题是:我在查询中尝试过的下一个选择中是否可以使用FnMAN
。 It says Invalid column name 'FnMAN'.
它说Invalid column name 'FnMAN'.
when i try it like in the query.当我在查询中尝试它时。
In the code block, it seems like i need it only twice but i actually need it a lot more.在代码块中,似乎我只需要它两次,但实际上我需要它更多。 I need to check the result and if its some person A, i need to make it some person B like i did with John and Jane.我需要检查结果,如果它是某个人 A,我需要像我对 John 和 Jane 所做的那样使它成为某个人 B。 So i really need to make this faster.所以我真的需要让它更快。
select *,(--More selection--)
from
(select
--
--Some selection of 10s of columns
--
(select dbo.fn_getUserMAN((
select M.txtPerson from Frm (nolock) Fm
JOIN FLOWDOCUMENTS(nolock) FD ON Fm.ID = FD.FILEPROFILEID
JOIN FLOWREQUESTS(nolock) FR on FD.PROCESSID = FR.PROCESSID
JOIN MdlFrm (nolock) M on M.ID = FR.EVENTFORMID
where FR.EVENTFORMID !=-1 and F.FrmNo = Fm.FrmNo))
) AS 'FnMAN',
(SELECT TOP 1 (
CASE WHEN
(select txtPerson from Frm_Info (nolock) as BB
inner join FLOWREQUESTS(nolock) FQ on BB.ID = FQ.EVENTFORMID
and FQ.PROCESSID = FD.PROCESSID) is not null
or
(select txtPerson from Frm_Info (nolock) as BB
inner join FLOWREQUESTS(nolock) FQ on BB.ID = FQ.EVENTFORMID
and FQ.PROCESSID = FD.PROCESSID) !=''
THEN
CASE WHEN FnMAN = 'John Doe'
THEN 'Jane Doe'
--More checks and switches on managers
ELSE FnMAN END
WHEN F.txtManager IS NOT NULL THEN F.txtManager END))
from Frm (NOLOCK) F
INNER JOIN FLOWDOCUMENTS(NOLOCK) FD ON FD.FILEPROFILEID = F.ID
INNER JOIN LIVEFLOWS(NOLOCK) LF ON LF.ID = FD.PROCESSID
INNER JOIN FLOWSTATUSES(NOLOCK) FS ON FS.PROCESS = LF.PROCESS AND FS.VERSION =LF.FLOWVERSION AND FS.STATUS = LF.STATUS
WHERE LF.DELETED = 0 and F.FrmNo IS NOT NULL and F.FrmNo != '') T
I found the solution with LEFT JOIN
and added a new column to use as a result of FnMAN
.我使用LEFT JOIN
找到了解决方案,并添加了一个新列以用作FnMAN
的结果。 I dont know whether or not this is a healthy way of having aa solution but it works.我不知道这是否是一种健康的解决方案,但它确实有效。
select *,(--More selection--)
from
(select
--
--Some selection of 10s of columns
--
(SELECT TOP 1 (
CASE WHEN
(select txtPerson from Frm_Info (nolock) as BB
inner join FLOWREQUESTS(nolock) FQ on BB.ID = FQ.EVENTFORMID
and FQ.PROCESSID = FD.PROCESSID) is not null
or
(select txtPerson from Frm_Info (nolock) as BB
inner join FLOWREQUESTS(nolock) FQ on BB.ID = FQ.EVENTFORMID
and FQ.PROCESSID = FD.PROCESSID) !=''
THEN
CASE WHEN FnMAN.MAN = 'John Doe'
THEN 'Jane Doe'
--More checks and switches on managers
ELSE FnMAN.MAN END
WHEN F.txtManager IS NOT NULL THEN F.txtManager END))
from Frm (NOLOCK) F
INNER JOIN FLOWDOCUMENTS(NOLOCK) FD ON FD.FILEPROFILEID = F.ID
INNER JOIN LIVEFLOWS(NOLOCK) LF ON LF.ID = FD.PROCESSID
INNER JOIN FLOWSTATUSES(NOLOCK) FS ON FS.PROCESS = LF.PROCESS AND FS.VERSION =LF.FLOWVERSION AND FS.STATUS = LF.STATUS
LEFT JOIN (
(select Fm.FormID, dbo.fn_getUserMAN(M.txtPerson) MAN
from Frm (nolock) Fm
JOIN FLOWDOCUMENTS(nolock) FD ON Fm.ID = FD.FILEPROFILEID
JOIN FLOWREQUESTS(nolock) FR on FD.PROCESSID = FR.PROCESSID
JOIN MdlFrm (nolock) M on M.ID = FR.EVENTFORMID
where FR.EVENTFORMID !=-1)
) AS 'FnMAN' on Fm.FormID = F.FormID,
WHERE LF.DELETED = 0 and F.FrmNo IS NOT NULL and F.FrmNo != '') T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.