内部选择语句帮助 - 子查询?

时间:2016-06-01 16:33:48

标签: sql tsql select join

我一直在努力为我们的业务需求创建一个简单的生产力仪表板报告,并遇到如何完成这些工作的问题。

用户需要在订单中完成7个任务。我想显示一行,显示每个已完成的日期,但它们显示为不同的订单项。

如何将其显示为一个订单项?这是我建立的代码:

select 

o.Number as 'OrderNo',
s.FullName as 'CPC Emp',
case when t.description='Loan Package to Lender' then t.CompletedDate else NULL end as 'Loan Pck To Lender',
case when t.description='Recording Audit' then t.CompletedDate else NULL end as 'Recording Audit',
case when t.description='Recorded Docs' then rt.RequestedDate else NULL end as 'Recorded Docs Requested',
case when t.description='Recorded Docs to Lender' then t.CompletedDate else NULL end as 'Recorded Docs to Lender',
case when t.description='Recorded Docs to Purchaser' then t.CompletedDate else null end as 'Recorded Docs to Purchaser',
case when t.description='Title Policy to Lender' then t.CompletedDate else NULL end as 'TP to Lender',
case when t.description='Title Policy to Purchaser' then t.CompletedDate else NULL end as 'TP Purchaser'

FROM pf.OrderInfo oi
    INNER JOIN pfm.[Order] o
            on (o.RootId# = oi.rootid)
    INNER JOIN core.Profile op
        ON (oi.OwningProfileID = op.ID)
    INNER JOIN zref.OrderStatus os
        ON (oi.OrderStatus = os.ID)
    INNER JOIN zref.ProductType pt
            on (o.ProductTypeID = pt.ID and pt.ID <>'15')
    INNER JOIN pfm.Task t
        ON (t.RootId# = oi.RootID 
        and (t.Description in ('Loan Package to Lender','Recording Audit','Recorded Docs to Lender',
        'Recorded Docs to Purchaser', 'Title Policy to Lender','Title Policy to Purchaser','Recorded Docs')     
        ) )         
    left outer JOIN pfm.RequestedTask rt    
        ON (rt.RootId# = t.RootId# and rt.Id#=t.Id# and rt.LastId# = t.LastId#)
    left outer JOIN core.SecurityIdentity s
        ON (s.ID = t.CompletedByID)

WHERE 
(op.Name like 'BH104%' -- Profiles Begin.
or op.name like 'WO115%' --Profiles End.
)
and t.CompletedDate between '2016-1-1' and '2016-4-30'
and s.fullname is not null

group by s.fullname, o.Number, t.Description, t.CompletedDate, rt.RequestedDate

order by 2 desc

排名前10的结果显示如下: result

如何让它在这些红框结果中显示一个项目?任何帮助都会非常感激..我尝试了内部选择语句和子查询,但无法正常工作..

提前致谢!

3 个答案:

答案 0 :(得分:1)

如果订单的每一行只包含一列的值,那么结果集将变为稀疏矩阵,可以通过MAX等聚合函数来减少:

select 

o.Number as 'OrderNo',
s.FullName as 'CPC Emp',
MAX(case when t.description='Loan Package to Lender' then t.CompletedDate else NULL end as) 'Loan Pck To Lender',
MAX(case when t.description='Recording Audit' then t.CompletedDate else NULL end) as 'Recording Audit',
MAX(case when t.description='Recorded Docs' then rt.RequestedDate else NULL end) as 'Recorded Docs Requested',
MAX(case when t.description='Recorded Docs to Lender' then t.CompletedDate else NULL end) as 'Recorded Docs to Lender',
MAX(case when t.description='Recorded Docs to Purchaser' then t.CompletedDate else null end) as 'Recorded Docs to Purchaser',
MAX(case when t.description='Title Policy to Lender' then t.CompletedDate else NULL end as) 'TP to Lender',
MAX(case when t.description='Title Policy to Purchaser' then t.CompletedDate else NULL end) as 'TP Purchaser'

FROM pf.OrderInfo oi
    INNER JOIN pfm.[Order] o
            on (o.RootId# = oi.rootid)
    INNER JOIN core.Profile op
        ON (oi.OwningProfileID = op.ID)
    INNER JOIN zref.OrderStatus os
        ON (oi.OrderStatus = os.ID)
    INNER JOIN zref.ProductType pt
            on (o.ProductTypeID = pt.ID and pt.ID <>'15')
    INNER JOIN pfm.Task t
        ON (t.RootId# = oi.RootID 
        and (t.Description in ('Loan Package to Lender','Recording Audit','Recorded Docs to Lender',
        'Recorded Docs to Purchaser', 'Title Policy to Lender','Title Policy to Purchaser','Recorded Docs')     
        ) )         
    left outer JOIN pfm.RequestedTask rt    
        ON (rt.RootId# = t.RootId# and rt.Id#=t.Id# and rt.LastId# = t.LastId#)
    left outer JOIN core.SecurityIdentity s
        ON (s.ID = t.CompletedByID)

WHERE 
(op.Name like 'BH104%' -- Profiles Begin.
or op.name like 'WO115%' --Profiles End.
)
and t.CompletedDate between '2016-1-1' and '2016-4-30'
and s.fullname is not null

group by s.fullname, o.Number

order by 2 desc

答案 1 :(得分:0)

这应该这样做:

.htaccess

答案 2 :(得分:0)

在我看来,您只想按订单编号进行分组,并将聚合添加到不在该组中的字段