我想合并两个 sql 查询结果

I want to combine two sql queries resukt

我想合并两个查询的查询结果和最终结果显示

我试过联合,但没用

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 行 关键字 'union'.

附近的语法不正确

对于 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

作为对 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

另请注意,因为联合中的第一个查询定义了列名,所以在第二个查询中为列设置别名的唯一目的是使代码更加自文档化。它没有功能性目的