2017-04-05 116 views
1

我有一種情況我不確定它是否可以執行。
我有兩個表我需要加入並使用計數。SQL 2012將連接多個表的多個查詢連接起來並統計到一個表中

表1 = 「EPM_ProjectUserview」

[ProjectUID], [ProjectName], [EnterpriseProjectTypeUID](used as a filter) 

表2 = 「EpmTask_UserView」

[ProjectUID],[Task_Significance], [TaskPercentCompleted] then 3 count fields 
[total Count], [Count 100% complete], Count <1005] 

這會給我的表看起來像這樣
注:有一個第一列不適合項目名稱

|Apps|Overall% Comp| Totl Count|100% Compl|Not 100% Comp|Overall Health| 

同一行的接口將是真實的

總體%完整和整體健康來自於摘要任務
(這是項目服務器)
ProjectUID是加入一個重點領域。

所以我有5個不同的查詢,所有的工作,但當然我需要他們在1表。

查詢1 - 這是項目名稱projectUID和EnterpriseprojecttypeUID

SELECT [ProjectName] 
    ,[ProjectUID] 
    ,[EnterpriseProjectTypeUID] 
    FROM [dbo].[MSP_EpmProject_UserView] 
where [projectUID] not like '0000CF75-FB12-4FFC-A404-AEC4F3258A9C' 
And 
[EnterpriseProjectTypeUID] like '76194144-3CDB-E611-9419-00215A9259E8' 
order by [ProjectName] 

查詢2這使我每種類型

SELECT p.[ProjectName] 
    ,T.[Task_Significance] 
-- ,T.[TaskPercentCompleted] 
    ,count (*) as [Total Count] 
    FROM [psmado].[dbo].[MSP_EpmProject_UserView] as P 
    join [PSMADO].[dbo].[MSP_EpmTask_UserView] as T 
    On T.[projectUID] = P.[projectUID] 
    where 
    ([Task_Significance] like 'App' 
    Or 
    [Task_Significance] like 'Interface') 
    Group by [ProjectName], [Task_Significance] 

查詢3-下一查詢的總數給我的100 %完成列

  SELECT p.[ProjectName] 
    ,T.[Task_Significance] 
-- ,T.[TaskPercentCompleted] 
    ,count (*) as [Count] 
FROM [psmado].[dbo].[MSP_EpmProject_UserView] as P 
join [PSMADO].[dbo].[MSP_EpmTask_UserView] as T 
On T.[projectUID] = P.[projectUID] 
    where T.[TaskPercentCompleted] >= 100 
    and 
([Task_Significance] like 'App' 
Or 
[Task_Significance] like 'Interface') 
    Group by [ProjectName], [Task_Significance] 

查詢4 - 下一個給我的那個少一些N 100%完成

SELECT p.[ProjectName] 
    ,T.[Task_Significance] 
-- ,T.[TaskPercentCompleted] 
    ,count (*) as [Count] 
FROM [psmado].[dbo].[MSP_EpmProject_UserView] as P 
join [PSMADO].[dbo].[MSP_EpmTask_UserView] as T 
On T.[projectUID] = P.[projectUID] 
where T.[TaskPercentCompleted] < 100 
and 
([Task_Significance] like 'App' 
Or 
[Task_Significance] like 'Interface') 
Group by [ProjectName], [Task_Significance] 

查詢5最後整個任務的健康和%完全來自不同的行中的表

SELECT [dbo].[MSP_EpmProject_UserView].[ProjectName] 
    ,[dbo].[MSP_EpmTask_UserView].[ProjectUID] 
    ,[dbo].[MSP_EpmTask_UserView].[TaskName] as [Name] 
    ,[dbo].[MSP_EpmTask_UserView].[TaskPercentCompleted] as [% Complete] 
    ,[dbo].[MSP_EpmTask_UserView].[Task Health] as [Health] 
    ,[dbo].[MSP_EpmTask_UserView].[Task_Significance] 
    ,[dbo].[MSP_EpmTask_UserView].[TaskOutlineNumber] 
    FROM [dbo].[MSP_EpmTask_UserView] 
    inner join [PSMADO].[dbo].[MSP_EpmProject_UserView] 
    on [dbo].[MSP_EpmTask_UserView].[projectuid] = [PSMADO].[dbo].[MSP_EpmProject_UserView].[ProjectUID] 
    where [dbo].[MSP_EpmTask_UserView].[projectUID] not like '0000CF75-FB12-4FFC-A404-AEC4F3258A9C' -- the timesheet project null 
    and [Task_Significance] like 'App Summary' 
or [Task_Significance] like 'Interface Summary' 
order by [ProjectName], [TaskOutlineNumber] 

難道這被(工會是不工作的投入一個表我)如果是這樣怎麼樣?

感謝您的幫助

千斤頂的答案非常接近。這是我需要的: enter image description here

這就是我所得到的。你可以看到它只是重複每個部分。 (我砍掉了它的一部分,但也有36行。)

enter image description here

+0

向我們展示db架構,示例數據和預期輸出。 \t請閱讀[**如何提問**](http://stackoverflow.com/help/how-to-ask) \t \t這裏是一個偉大的地方[** START **] (http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/)來了解如何提高您的問題質量並獲得更好的答案。 –

+0

什麼意思'這些可以放在一張桌子上嗎? –

回答

1

最好的和你在這裏最簡單的方法是使用..公用表表達式.. 它不是讓我所有粘貼你在這個表達式中的代碼,但你可以使用下面的語法來做到這一點非常簡單。

;與T1作爲(這些括號內粘貼1日足本), T2因爲(這些括號內粘貼你的第二足本), T3因爲(這些括號內粘貼第三足本), T4作爲(在這些括號內粘貼您的第4個查詢已滿), t5 as(在這些括號內粘貼您的第5個查詢已滿)

現在,在完成此操作後,編寫一個查詢並提取所需的任何數據。例如

選擇t1.abc,t2.abc,t3.xyz,t4.lmn,從T1,T2,T3,T4 t5.xyz,T5

希望這有助於。

+0

謝謝傑克這是非常接近,但我得到了一些結果,但不是我所期望的。我正在編輯我的問題與更新。 – Bob

2

謝謝大家的意見和幫助。這是一個非常棒的社區!所以我最終走向一個不同的方向,傑克思想讓我走上了一條道路。

SELECT pr.[ProjectName] 
,T.[Task_Significance] 

    , sum (case when T.[TaskPercentCompleted] is not null and T.[Task_Significance] ='App' Or T.[TaskPercentCompleted] is not null and T.[Task_Significance] = 'Interface' then 1 else 0 end) as [Total] 
, sum (case when T.[TaskPercentCompleted] < 30 and T.[Task_Significance] ='App' Or T.[TaskPercentCompleted] < 30 and T.[Task_Significance] = 'Interface' then 1 else 0 end) as [Incomplete] 
, sum (case when T.[TaskPercentCompleted] >= 30 and T.[Task_Significance] ='App' Or T.[TaskPercentCompleted] >= 30 and T.[Task_Significance] = 'Interface' then 1 else 0 end) as [Complete] 
    FROM [psmado].[dbo].[MSP_EpmProject_UserView] as Pr 
join [PSMADO].[dbo].[MSP_EpmTask_UserView] as T 
On T.[projectUID] = Pr.[projectUID] 
Where 
T.[Task_Significance] like 'App' 
Or 
T.[Task_Significance] like 'Interface' 

Group by pr.[ProjectName], T.[Task_Significance]