迭代sql server 2005中的记录以格式化输出

时间:2010-11-24 13:24:26

标签: sql-server sql-server-2005 tsql

我有一个像这样的列的表格图像和datalike这个

现在我需要在运行查询时显示这样的数据

id  maintitle subtitle  imagename
  1  ram       raman     abc.jpg
  2  manu      kiran     a2.jpg
  2  manoj     kumar     a3.jpg
  2  chiru     kumar     a4.jpg
  3  anu       anitha    a4.jpg
  3  anupam    aruna     a4.jpg

需要检查id是否存在它存在的多行,然后取2

行图像名称添加一个新列并在此处显示

需要这样的输出

 id  maintitle subtitle  imagename  imagename2 imagename3
  1  ram       raman     abc.jpg     null       null
  2  part1     kiran     a2.jpg      a3.jpg     a4.jpg
  3  anu       anitha    a4.jpg      a4.jpg     null

例如:我有这样的数据

INSERT @YourTable VALUES (2,'manu'  ,'kiran' ,'a2.jpg')
INSERT @YourTable VALUES (2,'manoj' ,'kumar' ,'a3.jpg')
INSERT @YourTable VALUES (2,'chiru' ,'kumar' ,'a4.jpg')
INSERT @YourTable VALUES (2,'manu'  ,'kumar' ,'a5.jpg')
INSERT @YourTable VALUES (2,'manoj' ,'kumar' ,'a6.jpg')
INSERT @YourTable VALUES (2,'chiru' ,'kumar' ,'a7.jpg')  

这里我有6行所以我必须创建六列。如果它有4行,则应该动态创建列,希望我的问题清楚

任何人都可以告诉我如何解决这个问题

任何解决方案都会很棒

1 个答案:

答案 0 :(得分:2)

您的样本数据与您的示例输出不一致。此代码使用您的示例数据,请尝试以下操作:

DECLARE @YourTable table (id int, maintitle varchar(10), subtitle varchar(10), imagename varchar(10))
INSERT @YourTable VALUES (1,'ram'   ,'raman' ,'abc.jpg')
INSERT @YourTable VALUES (2,'manu'  ,'kiran' ,'a2.jpg')
INSERT @YourTable VALUES (2,'manoj' ,'kumar' ,'a3.jpg')
INSERT @YourTable VALUES (2,'chiru' ,'kumar' ,'a4.jpg')
INSERT @YourTable VALUES (3,'anu'   ,'anitha','a4.jpg')
INSERT @YourTable VALUES (3,'anupam','aruna' ,'a4.jpg')

;WITH YourTableOrdered AS
(SELECT
     *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY id,imagename, maintitle,subtitle) AS RowNumber
     FROM @YourTable
)
SELECT
    t1.id,t1.maintitle,t1.subtitle,t1.imagename
        ,t2.imagename AS imagename2
        ,t3.imagename AS imagename3
    FROM YourTableOrdered                t1
        LEFT OUTER JOIN YourTableOrdered t2 ON t1.id=t2.id and t2.RowNumber=2
        LEFT OUTER JOIN YourTableOrdered t3 ON t1.id=t3.id and t3.RowNumber=3
    WHERE t1.RowNumber=1

输出:

id          maintitle  subtitle   imagename  imagename2 imagename3
----------- ---------- ---------- ---------- ---------- ----------
1           ram        raman      abc.jpg    NULL       NULL
2           manu       kiran      a2.jpg     a3.jpg     a4.jpg
3           anu        anitha     a4.jpg     a4.jpg     NULL

(3 row(s) affected)

编辑根据OP的问题编辑提及每个ID的许多/未知数量的文件。

你有三个选择:

1)对具有足够LEFT JOINS的固定查询进行编码,如:

;WITH YourTableOrdered AS
(SELECT
     *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY id,imagename, maintitle,subtitle) AS RowNumber
     FROM @YourTable
)
SELECT
    t1.id,t1.maintitle,t1.subtitle,t1.imagename
        ,t2.imagename AS imagename2
        ,t3.imagename AS imagename3
        ,t4.imagename AS imagename4
        ,t5.imagename AS imagename5
        ,t6.imagename AS imagename6
        ,t7.imagename AS imagename7
        ,t8.imagename AS imagename8
    FROM YourTableOrdered                t1
        LEFT OUTER JOIN YourTableOrdered t2 ON t1.id=t2.id and t2.RowNumber=2
        LEFT OUTER JOIN YourTableOrdered t3 ON t1.id=t3.id and t3.RowNumber=3
        LEFT OUTER JOIN YourTableOrdered t4 ON t1.id=t4.id and t4.RowNumber=4
        LEFT OUTER JOIN YourTableOrdered t5 ON t1.id=t5.id and t5.RowNumber=5
        LEFT OUTER JOIN YourTableOrdered t6 ON t1.id=t6.id and t6.RowNumber=6
        LEFT OUTER JOIN YourTableOrdered t7 ON t1.id=t7.id and t7.RowNumber=7
        LEFT OUTER JOIN YourTableOrdered t8 ON t1.id=t8.id and t8.RowNumber=8
    WHERE t1.RowNumber=1

2)获取每个id的最大文件数,然后构建包含该连接数的动态SQL语句。使用它来获取最大文件数:

DECLARE @MaxFiles  int
SELECT
    @MaxFiles=MAX(CountOf)
    FROM (SELECT
              id,COUNT(*) AS CountOf
              FROM @YourTable
              GROUP BY id
         ) dt

3)将所有文件名合并到一个逗号分隔的列表中,例如:

DECLARE @YourTable table (id int, maintitle varchar(10), subtitle varchar(10), imagename varchar(10))
INSERT @YourTable VALUES (1,'ram'   ,'raman' ,'abc.jpg')
INSERT @YourTable VALUES (2,'manu'  ,'kiran' ,'a2.jpg')
INSERT @YourTable VALUES (2,'manoj' ,'kumar' ,'a3.jpg')
INSERT @YourTable VALUES (2,'chiru' ,'kumar' ,'a4.jpg')
INSERT @YourTable VALUES (2,'manu'  ,'kumar' ,'a5.jpg')
INSERT @YourTable VALUES (2,'manoj' ,'kumar' ,'a6.jpg')
INSERT @YourTable VALUES (2,'chiru' ,'kumar' ,'a7.jpg')  
INSERT @YourTable VALUES (3,'anu'   ,'anitha','a4.jpg')
INSERT @YourTable VALUES (3,'anupam','aruna' ,'a4.jpg')    


;WITH YourTableOrdered AS
(SELECT
     *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY id,imagename, maintitle,subtitle) AS RowNumber
     FROM @YourTable
)
SELECT
    t1.id,t1.maintitle,t1.subtitle
        ,STUFF(
                   (SELECT
                        ', ' + t2.imagename
                        FROM @YourTable t2
                        WHERE t2.id=t1.id
                        ORDER BY t2.id,t2.imagename,t2.maintitle,t2.subtitle
                        FOR XML PATH(''), TYPE
                   ).value('.','varchar(max)')
                   ,1,2, ''
              ) AS imagenames
    FROM YourTableOrdered t1
    WHERE t1.RowNumber=1
    ORDER BY t1.id

输出:

id          maintitle  subtitle   imagenames
----------- ---------- ---------- -------------------------------------------------
1           ram        raman      abc.jpg
2           manu       kiran      a2.jpg, a3.jpg, a4.jpg, a5.jpg, a6.jpg, a7.jpg
3           anu        anitha     a4.jpg, a4.jpg

(3 row(s) affected)