Can somebody please tell me what is wrong with this SQL? I am getting the following error whenever I add the final line :
'ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator'
select * from
(
SELECT top 5 COUNT(*) AS 'TotalEnquiries',
f.createdbyname as 'Name',
round(100 * CAST(COUNT(*) as float) /
(SELECT COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'), 0) AS 'Percentage'
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'
GROUP BY f.createdbyname
order by totalenquiries desc) as d
union
select
(
SELECT SUM(T2.Expr1) FROM
(
(SELECT COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'
GROUP BY f.createdbyname)
) t2) -
(SELECT SUM(T1.Expr1) FROM
(
(SELECT top 5 COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'
GROUP BY f.createdbyname order by count(*) desc)) t1)
, 'Other',
round(100*(cast(((SELECT SUM(T2.Expr1) FROM
(
(SELECT COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'
GROUP BY f.createdbyname)
) t2) -
(SELECT SUM(T1.Expr1) FROM
(
(SELECT top 5 COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'
GROUP BY f.createdbyname order by count(*) desc)) t1)) as float))/
(SELECT COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'), 0)
ORDER BY CASE WHEN Name = 'Other' Then 1 ELSE 0 END, percentage
'ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator'
select * from
(
SELECT top 5 COUNT(*) AS 'TotalEnquiries',
f.createdbyname as 'Name',
round(100 * CAST(COUNT(*) as float) /
(SELECT COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'), 0) AS 'Percentage'
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'
GROUP BY f.createdbyname
order by totalenquiries desc) as d
union
select
(
SELECT SUM(T2.Expr1) FROM
(
(SELECT COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'
GROUP BY f.createdbyname)
) t2) -
(SELECT SUM(T1.Expr1) FROM
(
(SELECT top 5 COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'
GROUP BY f.createdbyname order by count(*) desc)) t1)
, 'Other',
round(100*(cast(((SELECT SUM(T2.Expr1) FROM
(
(SELECT COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'
GROUP BY f.createdbyname)
) t2) -
(SELECT SUM(T1.Expr1) FROM
(
(SELECT top 5 COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'
GROUP BY f.createdbyname order by count(*) desc)) t1)) as float))/
(SELECT COUNT(*) AS Expr1
from systemuser s
inner join filteredlead f
on s.systemuserid = f.ownerid
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
and s.businessunitidname <> 'External Agencies'), 0)
ORDER BY CASE WHEN Name = 'Other' Then 1 ELSE 0 END, percentage