我要结合两个查询结果和两个查询的最终结果显示
我尝试了工会,但是没有用
Select Distinct PersonNameWorked, COUNT(ProcessInstanceAppianId)as Num_Of_Jobs, sum(ExpectedTaskEffort) as Workunits,
cast((cast((sum(TaskInstanceEffort) / 60.00) as Decimal(5,2)))/count (Distinct cast(TaskInstanceCompleted as date) ) as Decimal(5,2)) as WorkingHours,
ROUND((1.00- sum(CAST(TaskInstanceEffort AS FLOAT))/sum(CAST(ExpectedTaskEffort AS FLOAT)))*100,2) as EfforVariance
from pathfinder..PFTask
where TaskInstanceCompleted between '2019-9-03 00:30:00' and '2019-9-04 00:30:00'
and KeyProcessStream in (26769,26768,28788,26760,26761,26755,29529)
and KeyPerson in (1347718,1332622,619682)
--and ProcessInstanceAppianId='-2136985491'
--and TaskInstanceCompleted
group by PersonNameWorked, KEYPERSON
order by PersonNameWorked asc
union all
select PersonNameWorked, cast(sum(PFTaskInstanceRating)*1.00/count(distinct ProcessInstanceAppianId) as decimal (5,2))as Rating
from pathfinder..PFTask
where TaskInstanceCompleted between '2019-9-03 00:30:00' and '2019-9-04 00:30:00'
and KeyPerson in (1347718,1332622,619682)
and PFTaskInstanceRating is not null
group by PersonNameWorked, KEYPERSON
order by PersonNameWorked asc
第156条消息,第15级,状态1,第17行 关键字“联合”附近的语法不正确。
答案 0 :(得分:1)
对于union all/union
,您需要在两个查询中使用相同数量的列,而这两种情况都不存在-因此会引发错误
Select Distinct PersonNameWorked, COUNT(ProcessInstanceAppianId)as Num_Of_Jobs, sum(ExpectedTaskEffort) as Workunits,
cast((cast((sum(TaskInstanceEffort) / 60.00) as Decimal(5,2)))/count (Distinct cast(TaskInstanceCompleted as date) ) as Decimal(5,2)) as WorkingHours,
ROUND((1.00- sum(CAST(TaskInstanceEffort AS FLOAT))/sum(CAST(ExpectedTaskEffort AS FLOAT)))*100,2) as EfforVariance
from pathfinder..PFTask
where TaskInstanceCompleted between '2019-9-03 00:30:00' and '2019-9-04 00:30:00'
and KeyProcessStream in (26769,26768,28788,26760,26761,26755,29529)
and KeyPerson in (1347718,1332622,619682)
--and ProcessInstanceAppianId='-2136985491'
--and TaskInstanceCompleted
group by PersonNameWorked, KEYPERSON
union all
select PersonNameWorked, null,null,null,cast(sum(PFTaskInstanceRating)*1.00/count(distinct ProcessInstanceAppianId) as decimal (5,2))as Rating
from pathfinder..PFTask
where TaskInstanceCompleted between '2019-9-03 00:30:00' and '2019-9-04 00:30:00'
and KeyPerson in (1347718,1332622,619682)
and PFTaskInstanceRating is not null
group by PersonNameWorked, KEYPERSON
答案 1 :(得分:0)
作为fa06s答案的补充,因为他非常清楚地指出两个查询之间的列数必须相等,所以该错误是由您尝试对每个查询进行ORDER BY,然后执行并集引起的:
在UNION之前,您不得对每个查询进行ORDER BY。在所有联合之后,您一次执行一次ORDER BY。
右:
SELECT..
FROM ...
UNION
SELECT ...
FROM ...
ORDER BY ... <-- just order by once. It works on the whole union output
错误:
SELECT..
FROM ...
ORDER BY ... <-- don't do it here as well
UNION
SELECT ...
FROM ...
ORDER BY ...
联合完成后,ORDER BY由SQL最后处理。 ORDER BY使用联合中第一个查询定义的列名:
SELECT a FROM table1
UNION
SELECT b FROM table2
UNION
SELECT c FROM table3
ORDER BY a <--the column is named after whatever the first query called it
如果有帮助,请在内部将其视为这样:
SELECT * FROM
(
SELECT a
UNION
SELECT b
UNION
SELECT c
) x
ORDER BY x.a
删除ORDER BY后,您将面临有关列数的错误:
已合并的查询必须具有相同的列数,因为结果集是垂直粘合在一起的:查询1的5列必须与查询2的5列匹配,因此查询1的50行和查询100的行2可以成为一个150行长的单个结果集。
如果查询2等中的某些列没有任何值,请将其设置为空:
SELECT 1 as a, 2 as b
UNION
SELECT 2, null <-- there are no values for column b for this query,
so just use a constant value like 0, 1, null..
whatever is appropriate
ORDER BY a
还要注意,因为联合中的第一个查询定义了列名,所以在第二个查询中为列加上别名的唯一目的是使代码更具自记录性。它没有功能目的