1

I have a table like so:

CREATE TABLE #Categories (CategoryText VARCHAR(50), CategoryUrl VARCHAR(50), SubCategoryText VARCHAR(50), SubCategoryUrl VARCHAR(50))
INSERT INTO #Categories SELECT 'Lighting', 'http://lighting.com', 'Chandeliers', 'http://chandeliers.com' 
INSERT INTO #Categories SELECT 'Lighting', 'http://lighting.com', 'Lamps', 'http://lamps.com' 
INSERT INTO #Categories SELECT 'Hardware', 'http://hardware.com', 'Hooks', 'http://hooks.com' 
INSERT INTO #Categories SELECT 'Hardware', 'http://hardware.com', 'Hinges', 'http://hinges.com' 

which looks like:

CategoryText                                       CategoryUrl                                        SubCategoryText                                    SubCategoryUrl
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------------------------
Lighting                                           http://lighting.com                                Chandeliers                                        http://chandeliers.com
Lighting                                           http://lighting.com                                Lamps                                              http://lamps.com
Hardware                                           http://hardware.com                                Hooks                                              http://hooks.com
Hardware                                           http://hardware.com                                Hinges                                             http://hinges.com

How would I dynamically display the data as follows:

Type        Text                                               Url
----------- -------------------------------------------------- --------------------------------------------------
Category    Lighting                                           http://lighting.com
SubCategory Chandeliers                                        http://chandeliers.com
SubCategory Lamps                                              http://lamps.com
Category    Hardware                                           http://hardware.com
SubCategory Hinges                                             http://hinges.com
SubCategory Hooks                                              http://hooks.com

I also need to retain the correct order of categories and subcategories.

UNPIVOT came to mind, but I don't see how I can apply it here as the data structure/requirements are different than most examples out there.

Any help is appreciated.

4

4 回答 4

2

Try this:

select distinct
  Type = 'Category',
  Parent = CategoryText,
  Text = CategoryText,
  Url = CategoryUrl
from #Categories
union all
select 
  Type = 'SubCategory',
  Parent = CategoryText,
  Text = SubCategoryText,
  Url = SubCatgoryUrl
from  #Categories
order by Parent,Type,Text
于 2013-03-12T14:30:02.790 回答
2

This should work for you...using a UNION with a ranking function...

select CASE WHEN SubCategoryText IS NULL THEN 'Category' ELSE 'SubCategory' END as Type,
        CASE WHEN SubCategoryText IS NULL THEN CategoryText ELSE SubCategoryText END as Type,
        CategoryUrl
from (      
select *, RANK() OVER (ORDER BY CategoryText, SubCategoryText) AS Rank
from (
select CategoryText, null as SubCategoryText, CategoryUrl
from #Categories
union 
select CategoryText, SubCategoryText, SubCategoryUrl
from #Categories
)a)b
order by Rank
于 2013-03-12T14:33:26.260 回答
1
    ;WITH Main as
    (
        SELECT 'Category' AS [Type], CategoryText AS ordering, CategoryText, CategoryUrl
        FROM #Categories
    ),
        Sub AS 
    (
        SELECT 'SubCategory' AS [Type], CategoryText AS ordering, SubCategoryText, SubCategoryUrl
        FROM #Categories    
    )   
    SELECT DISTINCT Type, CategoryText, CategoryUrl, ordering
    FROM Main
    UNION ALL
    SELECT Type, SubCategoryText, SubCategoryUrl,ordering
    FROM Sub
    ORDER BY ordering, [Type], categorytext
于 2013-03-12T14:40:40.153 回答
0

I was able to get this working, with retaining the order of categories and subcategories.

I used @SliverNinja's CASE statement to get the correct type and text.

For the sorting, I added 2 columns CategorySortOrder and SubCategorySortOrder. I combined the category and subcategory sortorder to create a new single SortOrder column. I then grouped and ordered by that column to get my final result.

CREATE TABLE #Categories (CategoryText VARCHAR(50), CategoryUrl VARCHAR(50), CategorySortOrder INT,SubCategoryText VARCHAR(50), SubCategoryUrl VARCHAR(50), SubCategorySortOrder INT)
INSERT INTO #Categories SELECT 'Lighting', 'http://lighting.com', 1, 'Chandeliers', 'http://chandeliers.com',1
INSERT INTO #Categories SELECT 'Lighting', 'http://lighting.com', 1, 'Lamps', 'http://lamps.com' , 2
INSERT INTO #Categories SELECT 'Hardware', 'http://hardware.com', 2 ,'Hooks', 'http://hooks.com' ,1
INSERT INTO #Categories SELECT 'Hardware', 'http://hardware.com', 2,'Hinges', 'http://hinges.com' ,2

SELECT
    Type
    , Text
    , Url
    , MIN(SortOrder)[SortOrder]
INTO 
    #cats
FROM
    (
    SELECT 
        CASE WHEN SubCategoryText IS NULL THEN 'Category' ELSE 'SubCategory' END [Type]
        , CASE WHEN SubCategoryText IS NULL THEN CategoryText ELSE SubCategoryText END [Text]
        , [Url]
        , CASE WHEN SubCategoryText IS NULL THEN CAST(CAST(CategorySortOrder AS VARCHAR) + CAST(SubCategorySortOrder AS VARCHAR) AS NUMERIC)  ELSE CAST(CAST(CategorySortOrder AS VARCHAR) + CAST(SubCategorySortOrder AS VARCHAR) AS NUMERIC) + 1 END[SortOrder]
    FROM
        (
            SELECT
                CategoryText
                , NULL [SubCategoryText]
                , CategoryUrl [Url]
                , CategorySortOrder
                , SubCategorySortOrder
            FROM
                #Categories
            UNION ALL
            SELECT
                CategoryText
                , SubCategoryText
                , SubCategoryUrl
                , CategorySortOrder
                , SubCategorySortOrder
            FROM
                #Categories
        )t
    )t
GROUP BY
    Type
    , Text
    , Url
ORDER BY
    SortOrder 

SELECT 
    Type
    , Text
    , Url
FROM
    #Cats
于 2013-03-13T15:18:18.573 回答