0

私は別の投稿からこのコードを持っています:

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

categoryNameそれは機能しますが、どうすれば単なると以上のものを取得できますかsubCategoryName。すべての列からデータを取得する方法はありますか?

ここに画像の説明を入力してください

私のコード:

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
4

1 に答える 1

0

OMG Poniesが言ったように、最初SELECT FROMはからデータをプルすることfooです。そこにあるCからすべての列を返さない場合、外部クエリはそれらを取得する方法がありません。

次のように、内部クエリの両方の部分を展開して、テーブル全体をキャプチャします。

SELECT * --grab everything you get in the subquery
  FROM (SELECT S.subcategoryname As bar, 
               C.* --note how this grabs EVERYTHING in Category, as you want
               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.*, --this grabs everything again
               0
          FROM category C) foo
ORDER BY CategoryName, RowNum --both of these come from foo - no need to specify it
于 2012-07-15T22:44:45.227 に答える