I have this code from another post :
SELECT bar
FROM (SELECT S.subcategoryname As bar,
C.CategoryName AS SortName,
ROW_NUMBER() OVER (PARTITION BY C.CategoryName
ORDER BY subcategoryname) As RowNum
FROM category C
LEFT JOIN subCategory S ON C.CategoryID = S.CategoryID
UNION ALL
SELECT C.CategoryName As bar,
C.CategoryName AS SortName,
0
FROM category C) foo
ORDER BY foo.SortName, RowNum
It works, but how can I retrieve more than just categoryName and subCategoryName. Is there a way to retrieve data from all columns?

my code:
select * from (select s.titlu_subcerinta as bar,c.titlu_Cerinta As SortName, ROW_NUMBER() over(partition by c.titlu_Cerinta order by titlu_subcerinta) as RowNum
from cerinteProiect c left join subcerinteProiect s on c.id_cerinta = s.id_cerinta
UNION ALL
select c.titlu_Cerinta As bar, c.titlu_Cerinta As SortName,0
from cerinteProiect c
)
foo
order by
foo.SortName,RowNum
As OMG Ponies said, the first
SELECT FROMis pulling data fromfoo. If you don’t return all columns from C in there, your outer query has no way to get them.Expand both parts of your inner query to capture the whole table, like as follows: