1

我有一个table1包含 2 列的表 ( )toypatternadditionalcolor

表格1

toypattern  additionalcolor
FL38        orange
FL38        yellow
LF66        grey
LF66        black

现在我需要编写一个查询来显示toypattern列、1st color列和2nd color列。我需要显示多达 5 个附加颜色选项

期望的输出:

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

如何编写这样的 SQL 查询?

4

4 回答 4

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
于 2012-06-17T11:17:24.507 回答
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

希望这可以帮助。

于 2012-06-17T10:33:22.237 回答
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
于 2012-06-17T11:51:21.717 回答
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 种颜色时,这并不能真正得到人们可能希望的结果。但是,鉴于此答案附带的评论,我不会删除它。

于 2012-06-17T09:26:54.140 回答