简体   繁体   中英

I want to combine two sql queries resukt

I want to combine both queries result and final result showing of both queries

I tried with union but it was not work

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 

Msg 156, Level 15, State 1, Line 17 Incorrect syntax near the keyword 'union'.

For union all/union you need to have the same number of column in both queries which are missing in your case - so it throws error

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

In supplement to fa06s answer, because he makes a really good point that your column counts need to be equal between the two queries, this error is caused by your attempt to ORDER BY each query, then do the union:

在此处输入图片说明

You must not ORDER BY each query before you UNION. You do the ORDER BY once, at the end, after all the unions.

Right:

SELECT.. 
FROM ... 
UNION 
SELECT ... 
FROM ... 
ORDER BY ... <-- just order by once. It works on the whole union output

Wrong:

SELECT.. 
FROM ...
ORDER BY ... <-- don't do it here as well
UNION 
SELECT ... 
FROM ... 
ORDER BY ...

ORDER BY is processed last by SQL, after unions have been done. ORDER BY uses the column names defined by the first query in the union:

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

If it helps, think of it as happening like this, internally:

SELECT * FROM
(
  SELECT a 
  UNION 
  SELECT b 
  UNION 
  SELECT c  
) x
ORDER BY x.a

Once you remove the ORDER BY, you'll then be faced with an error about your column counts:

在此处输入图片说明

Queries that are unioned must have the same count of columns because the resultesets are stuck together vertically: 5 columns on query 1 must me matched to 5 columns on query 2, so that the 50 rows from query 1 and 100 rows from query 2 can become a single result set 150 rows long.

If you don't have any values for some columns in query 2 etc, make them null:

  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

Note also that because the first query in a union defines the column names, the only point in aliasing the columns in the second query is to make the code more self documenting. It has no functional purpose

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