SQL Server:列到行

时间:2012-06-17 09:06:39

标签: sql sql-server

我有一个包含2列table1toypattern的表格additionalcolor}:

表1

toypattern  additionalcolor
FL38        orange
FL38        yellow
LF66        grey
LF66        black

现在我需要编写一个查询来显示toypattern列,1st color列和2nd color列。我需要显示最多5个additionalColor选项

期望的输出:

toypattern  1st color   2nd color     3rd color   4th color   5th color
  FL38        orange      yellow
  LF66        grey        black

如何编写这样的SQL查询?

4 个答案:

答案 0 :(得分:2)

试试这些:

;with cte as
(select toypattern, additionalcolor,
  row_number() over(partition by toypattern order by additionalcolor) As rn
from Table1)
select toypattern, 
  max(case when rn = 1 then additionalcolor end) as additionalcolor1,
  max(case when rn = 2 then additionalcolor end) as additionalcolor2
from cte
group by toypattern

返回:

toypattern  additionalcolor1    additionalcolor2
FL38        Orange              Yellow
LF66        Black               Grey

这样做的缺点是你需要为新的附加颜色添加新的MAX(...)行。

////

另一种解决方案是将其他颜色设为CSV:

SELECT DISTINCT T1.toypattern,
STUFF((SELECT TOP 100 PERCENT ',' + s2.additionalcolor FROM Table1 AS s2 WHERE s2.toypattern = T1.toypattern ORDER BY ',' + s2.additionalcolor FOR XML PATH('')), 1, 1, '') AS Colours
FROM Table1 AS T1
ORDER BY T1.toypattern

返回:

toypattern  Colours
FL38    Orange,Yellow
LF66    Black,Grey

希望这有帮助。

答案 1 :(得分:2)

确定。不确定这个问题是否与这个问题有关(http://stackoverflow.com/questions/11070509/sql-query-issue-on-left-join):

但是这里有一个可能就是你所追求的解决方案 - 拿了一些03Usr的代码(特别是row_number并添加了旋转):

;with cte as
(select toypattern, additionalcolor,
  rowno =  row_number() over(partition by toypattern order by additionalcolor)
from Table1)
SELECT
  ToyPattern
  --, ToyName
  , 'color 1' = [1]
  , 'color 2' = [2]
  , 'color 3' = [3]
  , 'color 4' = [4]
  , 'color 5' = [5]
FROM (  
  SELECT
  ToyPattern
  --, ToyName
  , rowno
  , additionalcolor
  FROM
    cte
) AS tac PIVOT (
  MAX (additionalcolor) FOR rowno in ([1],[2],[3],[4],[5])
) AS PivotTable

答案 2 :(得分:0)

SELECT 
   first.toypattern,
   first.additionalColor,
   second.additionalColor
FROM
   Table1 first
   INNER JOIN Table2 second ON first.toypattern = second.toypattern
WHERE
   first.additionalColor < second.additionalColor

问题是,如果每个模式有两种以上的颜色,你会得到几个匹配。

编辑:这并没有真正找回搜索超过2种颜色时可能希望获得的结果。但是,鉴于此答案附有评论,我不会删除它。

答案 3 :(得分:0)

您使用的是什么Sql Server版本? (我无法添加注释)像上面发布的那样的Pivot将是最好的,但是如果您使用的是旧的Sql Server版本(如2000),您可以使用CURSOR执行此操作:

drop table #temp
create table #temp
(
   toypattern varchar(20),
   additionalcolor1 varchar(20),
   additionalcolor2 varchar(20),
   additionalcolor3 varchar(20),
   additionalcolor4 varchar(20),
   additionalcolor5 varchar(20)
)

declare @toypattern as varchar(20)
declare @additionalcolor as varchar(20)     
declare @Counter integer 
declare @StrSql varchar(200)    
declare @LastToyPattern as varchar(20)

set @LastToyPattern = ''

declare MyCursor cursor for  select toypattern, additionalcolor from table1 order by toypattern
open MyCursor
fetch next from MyCursor into @toypattern, @additionalcolor 

   while @@fetch_status = 0     
   begin
      if (@LastToyPattern <> @toypattern)
      begin
         if (@LastToyPattern <> '') 
         begin
            set @StrSql = @StrSql + replicate(',null', 5 - @Counter)    
            exec(@StrSql)
         end
         set @Counter = 0
         set @StrSql = 'insert into #temp select ''' + @toypattern + ''''
      end
      set @StrSql = @StrSql + ', ''' + @additionalcolor + ''''  
      set @Counter = @Counter + 1
      set @LastToyPattern = @toypattern
      fetch next from MyCursor into @toypattern, @additionalcolor
   end
set @StrSql = @StrSql + replicate(',null', 5 - @Counter)
exec(@StrSql)
close MyCursor
deallocate MyCursor
select * from #temp