I have a problem with my SQL. I am trying to order a top expression with a union, but the union prevents me from using order by. Here is my SQL before the union. How do I change this to work with order by?
SELECT top 5 COUNT(*) AS 'TotalEnquiries',
createdbyname as 'Name',
round(100 * CAST(COUNT(*) as float) /
(SELECT COUNT(*) AS Expr1
FROM FilteredLead
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)), 0) AS 'Percentage'
FROM FilteredLead
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
GROUP BY createdbyname
SELECT top 5 COUNT(*) AS 'TotalEnquiries',
createdbyname as 'Name',
round(100 * CAST(COUNT(*) as float) /
(SELECT COUNT(*) AS Expr1
FROM FilteredLead
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)), 0) AS 'Percentage'
FROM FilteredLead
where createdonutc
between DateAdd(Day, DateDiff(Day, 0, GetDate()-6), 0)
And DateAdd(day, DateDiff(Day, 0, GetDate() + 1), 0)
GROUP BY createdbyname