简体   繁体   中英

SQL 2012 Joining multiple queries from multiple tables with Joins and count into a single table

I have a situation I am not sure if it is even possible to do.
I have two tables I need to join and use a count with.

table 1 = "EPM_ProjectUserview"

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

Table2 = "EpmTask_UserView"

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

this would give me a table that looks like this
NOTE: there is a first column that didn't fit for Project Name

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

The same row for interfaces would be true

the overall % complete and the Overall health come from a summary task
(this is project server)
ProjectUID is a key field for joins.

So I have 5 different queries that all work but, of course I need them in 1 table.

Query 1 - this is the Project name projectUID and 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] 

Query 2 This gives me the total count of each type

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]

Query 3- the next Query gives me the 100% complete column

         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]

Query 4 - the next one gives me the ones less than 100% complete

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]

Query 5 Finally the overall task health and % complete comes from a different row in the table

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]

Can these be put into one table (a union is not working for me) And if so How?

thanks for the help

Jacks answer is very close. This is what I need: 在此输入图像描述

This is what I am getting . You can see it is just repeating for each section. (I chopped off part of it but there are 36 rows. )

在此输入图像描述

thank you all for your comments and help. this is such a great community! So I ended up going in a different direction as Jacks thoughts put me on a path.

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]

The best and the easiest way for you here is .. Using a common table expression.. its not allowing me to paste all your code in this expression but you could do it using the following syntax pretty straight forward.

;with t1 as ( Within these brackets paste your 1st query full ) , t2 as (Within these brackets paste your 2nd query full ), t3 as (Within these brackets paste your 3rd query full ), t4 as (Within these brackets paste your 4th query full ), t5 as (Within these brackets paste your 5th query full )

Now after this is completed write a query and extract any data that you want . FOr example

select t1.abc , t2.abc, t3.xyz, t4.lmn, t5.xyz from t1,t2,t3,t4,t5

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM