2016-06-01 7 views
0

私は、ビジネスニーズに合わせた簡単な生産性ダッシュボードレポートを作成し、これをどうやって解決するかについての問題に取り組んでいます。内部Select文ヘルプ - サブクエリ?

ユーザーが順番に完了する必要がある7つのタスクがあります。 1つの行を表示して、完了した各日付を表示したいが、それらは異なる行項目として表示されている。

1つの広告申込情報として表示するにはどうすればよいですか?ここで私が構築されたコードだ:

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

どのように私はそれがそれらの赤いボックス結果に1行の項目を表示するのですか?どんな助けでも大いに感謝しています。私は内側のselect文とサブクエリを試しましたが、それを得ることができませんでした。

ありがとうございます!

答えて

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 
+0

ワウ、知っているのは素晴らしいです!これは私が必要なものでなければなりません..ありがとう! – user3571153

0

これはそれを行う必要があります。

;WITH CTE 
    AS (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) 
    SELECT CTE.OrderNo 
     , CTE.[CPC Emp] 
     , [Loan Pck To Lender]=MAX(CTE.[Loan Pck To Lender]) 
     , [Recording Audit]=MAX(CTE.[Recording Audit]) 
     , [Recorded Docs Requested]=MAX(CTE.[Recorded Docs Requested]) 
     , [Recorded Docs to Lender]=MAX(CTE.[Recorded Docs to Lender]) 
     , [Recorded Docs to Purchaser]=MAX(CTE.[Recorded Docs to Purchaser]) 
     , [TP to Lender]=MAX(CTE.[TP to Lender]) 
     , [TP Purchaser]=MAX(CTE.[TP Purchaser]) 
    FROM CTE 
    GROUP BY [OrderNo] 
      , [CPC Emp] 
    ORDER BY [CPC Emp]; 
0

をあなたが唯一の注文番号でグループ化すると、各行のための場合