我有一个表像这样:SQL服务器 - 显示数据 “向下”
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'
它看起来像:
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
我将如何动态地显示数据如下:
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
我还需要保留类别和子类别的正确顺序。
UNPIVOT想到了,但我不明白我在这里如何应用它,因为数据结构/要求与大多数示例不同。
任何帮助表示赞赏。
对不起,伙计们,我意识到,子类别的排序顺序没有被保留。除了父类别的排序顺序,还有一种方法可以保持子类别的排序顺序吗? – Rivka 2013-03-12 15:05:37