我有一种情况我不确定是否有可能这样做。
我有两个表需要加入并使用计数。
表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]
这会给我一张看起来像这样的表 注意:第一列不适合项目名称
|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 - 下一个给出了不到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]
这些可以放在一张桌子上(工会对我不起作用)如果是这样的话怎么样?
感谢您的帮助
这就是我所得到的。你可以看到它只是重复每个部分。 (我把它的一部分切掉了但是有36排。)
答案 0 :(得分: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]
答案 1 :(得分:1)
这里最好和最简单的方法是..使用公用表表达式.. 它不允许我将所有代码粘贴到此表达式中,但您可以使用以下语法直接执行此操作。
;将t1作为(在这些括号内粘贴您的第一个查询已满), t2 as(在这些括号内粘贴第二个查询完整), t3 as(在这些括号内粘贴你的第三个查询全部), t4 as(在这些括号中粘贴你的第四个查询全部), t5 as(在这些括号内粘贴你的第5个查询)
完成此操作后,编写查询并提取所需的任何数据。例如
从t1,t2,t3,t4,t5选择t1.abc,t2.abc,t3.xyz,t4.lmn,t5.xyz
希望这有帮助。