SQL 2012 将来自多个表的多个查询与连接和计数合并到一个表中
Posted
技术标签:
【中文标题】SQL 2012 将来自多个表的多个查询与连接和计数合并到一个表中【英文标题】:SQL 2012 Joining multiple queries from multiple tables with Joins and count into a single table 【发布时间】:2017-04-05 20:55:55 【问题描述】:我有一种情况,我不确定它是否可以做到。 我有两个表需要加入并使用计数。
表 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行。)
【问题讨论】:
向我们展示数据库架构、示例数据和预期输出。请阅读How-to-Ask 这里是START 了解如何提高问题质量并获得更好答案的好地方。 什么意思Can these be put into one table
??
【参考方案1】:
感谢大家的 cmets 和帮助。这是一个很棒的社区!所以我最终走向了不同的方向,因为杰克斯的想法让我走上了一条道路。
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]
【讨论】:
【参考方案2】:对您来说最好和最简单的方法是..使用公用表表达式.. 它不允许我将所有代码粘贴到此表达式中,但您可以使用以下语法非常直接地进行操作。
;with t1 as (在这些括号内粘贴你的第一个查询完整), t2 as(在这些括号内粘贴您的第二个查询 full ), t3 as(在这些括号内粘贴您的第三个查询 full ), t4 as (在这些括号内粘贴您的第 4 个查询 full ), t5 as(在这些括号内粘贴您的第 5 个查询)
现在完成后编写一个查询并提取您想要的任何数据。例如
从 t1,t2,t3,t4,t5 中选择 t1.abc, t2.abc, t3.xyz, t4.lmn, t5.xyz
希望这会有所帮助。
【讨论】:
谢谢 Jack,这非常接近,但我得到了一些结果,但并不完全符合我的预期。我正在使用更新编辑我的问题。以上是关于SQL 2012 将来自多个表的多个查询与连接和计数合并到一个表中的主要内容,如果未能解决你的问题,请参考以下文章