我有3个表,我必须从中获取记录,并且必须在RadGrid中显示。
从表-1中说请求表,我必须获取以下列:
R.RequestID
R.BarcodeNo
R.Company
R.Status
表示 Xml 表,我必须在以下列中获取:
x.ScannedBy
x.ScannedDate
x.VerifiedBy
x.VerifiedDate
表示工作流程表,我必须在以下列中获取:
W.Action
W.UserName
W.CreatedDate
现在,此W.Action
列在工作流程表格中包含以下数据:
如上所示,此W.Action
列可能包含重复数据。
对于每一个" 行动"列,有" UserName "列和" CreatedDate " 工作流程表格中的列。
现在,我必须从上面3个W.Action = 'Submit'
的表中获取记录
和W.Action = 'Update'
此外,在选择查询中,我必须选择W.Action = 'Submit'
作为不同的列,W.Action = 'Update'
作为不同的列,如下格式:
为此,我创建了以下查询:
Select * FROM
(
SELECT distinct
R.RequestID,
R.BarcodeNo,
R.Company,
R.Status,
x.ScannedBy,
x.ScannedDate,
x.VerifiedBy,
x.VerifiedDate,
W.Action, W.UserName as SubmitName, W.CreatedDate as SubmitDate
From [tbl_Request] (NOLOCK) R
Left Join [tbl_Xml](NOLOCK) X On X.XmlID = R.XmlID
Left Join [tbl_Workflow] (NOLOCK) W On W.RequestID = R.RequestID
Where W.Action = 'Submit'
) as A
Left Join
(
SELECT RequestID, W.Action, W.UserName as UpdateName , W.CreatedDate AS UpdateDate
From [tbl_Request] (NOLOCK) R
Left Join [tbl_Xml](NOLOCK) X On X.XmlID = R.XmlID
Left Join [tbl_Workflow] (NOLOCK) W On W.RequestID = R.RequestID
Where Action = 'Update'
) as B
ON A.RequestID=B.RequestID
通过上述查询,我可以成功获得Action = 'Submit'
一个单独的列,Action = 'Update'
作为单独的列'
但我没有得到正确的记录。
如果我单独运行查询的第一部分(即,对于Action =' Submit')
它给了我142747条记录
如果我单独运行查询的第二部分(即,对于Action =' Update')
它给了我73021条记录
但如果我一次运行整个查询,它只给我215774条记录,这是不正确的。
假设给我(142747 + 73021)记录。
请告诉我查询中的错误?
如何获得正确的记录以及Action = Submit& Action =更新为单独的列。
请回复
答案 0 :(得分:0)
SELECT
R.RequestID,
R.BarcodeNo,
R.Company,
R.Status,
x.ScannedBy,
x.ScannedDate,
x.VerifiedBy,
x.VerifiedDate,
max(case when W.Action='Submit' then W.UserName end) as SubmitName,
max(case when W.Action='Submit' then W.CreatedDate end) as SubmitDate,
max(case when W.Action='Update' then W.UserName end) as UpdateName ,
max(case when W.Action='Update' then W.CreatedDate end) as UpdateDate
From [tbl_Request] (NOLOCK) R
Left Join [tbl_Xml](NOLOCK) X On X.XmlID = R.XmlID
Left Join [tbl_Workflow] (NOLOCK) W On W.RequestID = R.RequestID
Where W.Action in ( 'Submit','Update')
group by
R.RequestID,
R.BarcodeNo,
R.Company,
R.Status,
x.ScannedBy,
x.ScannedDate,
x.VerifiedBy,
x.VerifiedDate