I am getting the following error when I user Order by with UNION.
"The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified."
declare @results table (hdate date, name varchar(100), id1 int)
insert into @results select cast('10-01-2015' as date), 'val1 1', 1
insert into @results select cast('10-02-2015' as date), 'val2 2', 2
insert into @results select cast('10-03-2015' as date), 'thanks 1', 3
insert into @results select cast('10-04-2015' as date), 'thanks 2', 3
select (DATENAME(dw, hdate) + ', ' +
DATENAME(mm, hdate) + ' ' +
DATENAME(dd, hdate)) AS h_date, name AS h_name from @results where id1 in (1,2)
order by id1
UNION
SELECT (STUFF((
SELECT ', ' + (DATENAME(dw, hdate) + ', ' +
DATENAME(mm, hdate) + ' ' +
DATENAME(dd, hdate))
FROM @results
WHERE id1 = 3
FOR XML PATH('')
), 1, 2, '')
) AS h_date, 'Giving Day' AS h_name