如何将行数据转换为SQL Server中的不同列?

时间:2011-12-08 21:00:41

标签: sql sql-server-2008 crosstab

PONumbe     LNSKU   LNStyleNo   LNStyl  LNColor counter SizeDescription
164874      1059656 12345678910 LALALA  RED     1       050B
164874      1059656 12345678910 LALALA  RED     2       055B

我想根据PONumbe LNSKU的不同组合将SizeDescription和counter的值转换为多列

输出应该是这样的

PONumbe LNSKU    LNStyleNo    LNStyl    LNColor counter SizeDesc50 SizeDesc50_Count SizeDesc55 SizeDesc55_Count
164874  1059656  12345678910  LALALA    RED     1       050B        1                 055B        2

我无法弄清楚如何为此编写T-SQL查询。

由于

2 个答案:

答案 0 :(得分:1)

SELECT t1.*, t2.counter AS [Size For 050], t3.counter AS [Size for 055]
FROM table t1
    LEFT OUTER JOIN table t2 ON t2.key = t1.key AND SizeDescription LIKE '050%'
    LEFT OUTER JOIN table t3 ON t3.key = t1.key AND SizeDescription LIKE '055%'

那应该给你一个开始...如果你让我更多地了解架构,也许我可以把它充实......

为你充实了一下

答案 1 :(得分:0)

我已经做了以下查询似乎正在运作,但是你能不能找一些潜在的问题。 感谢

SELECT distinct t1.PONumber, t1.LNSKUNumber, sum(t2.pair_count) AS [Size For 050], sum(t3.pair_count) AS [Size for 055],
sum(t4.pair_count) AS [Size for 060],sum(t5.pair_count) AS [Size for 065],sum(t6.pair_count) AS [Size for 070],
sum(t7.pair_count) AS [Size for 075],sum(t8.pair_count) AS [Size for 080],sum(t9.pair_count) AS [Size for 085],
sum(t10.pair_count) AS [Size for 090],sum(t11.pair_count) AS [Size for 095],sum(t12.pair_count) AS [Size for 100],
sum(t13.pair_count) AS [Size for 110],sum(t14.pair_count) AS [Size for 120],sum(t15.pair_count) AS [Size for 130]
FROM #tmptable t1
    LEFT OUTER JOIN #tmptable t2 ON t2.PONumber = t1.PONumber AND t2.LNSKUNumber=t1.LNSKUNumber AND t2.SizeDescription='050B'
    LEFT OUTER JOIN #tmptable t3 ON t3.PONumber = t1.PONumber AND t3.LNSKUNumber=t1.LNSKUNumber AND t3.SizeDescription='055B'
    LEFT OUTER JOIN #tmptable t4 ON t4.PONumber = t1.PONumber AND t4.LNSKUNumber=t1.LNSKUNumber AND t4.SizeDescription='060B'
    LEFT OUTER JOIN #tmptable t5 ON t5.PONumber = t1.PONumber AND t5.LNSKUNumber=t1.LNSKUNumber AND t5.SizeDescription='065B'
    LEFT OUTER JOIN #tmptable t6 ON t6.PONumber = t1.PONumber AND t6.LNSKUNumber=t1.LNSKUNumber AND t6.SizeDescription='070B'
    LEFT OUTER JOIN #tmptable t7 ON t7.PONumber = t1.PONumber AND t7.LNSKUNumber=t1.LNSKUNumber AND t7.SizeDescription='075B'
    LEFT OUTER JOIN #tmptable t8 ON t8.PONumber = t1.PONumber AND t8.LNSKUNumber=t1.LNSKUNumber AND t8.SizeDescription='080B'
    LEFT OUTER JOIN #tmptable t9 ON t9.PONumber = t1.PONumber AND t9.LNSKUNumber=t1.LNSKUNumber AND t9.SizeDescription='085B'
    LEFT OUTER JOIN #tmptable t10 ON t10.PONumber = t1.PONumber AND t10.LNSKUNumber=t1.LNSKUNumber AND t10.SizeDescription='090B'
    LEFT OUTER JOIN #tmptable t11 ON t11.PONumber = t1.PONumber AND t11.LNSKUNumber=t1.LNSKUNumber AND t11.SizeDescription='095B'
    LEFT OUTER JOIN #tmptable t12 ON t12.PONumber = t1.PONumber AND t12.LNSKUNumber=t1.LNSKUNumber AND t12.SizeDescription='100B'
    LEFT OUTER JOIN #tmptable t13 ON t13.PONumber = t1.PONumber AND t13.LNSKUNumber=t1.LNSKUNumber AND t13.SizeDescription='110B'
    LEFT OUTER JOIN #tmptable t14 ON t14.PONumber = t1.PONumber AND t14.LNSKUNumber=t1.LNSKUNumber AND t14.SizeDescription='120B'
    LEFT OUTER JOIN #tmptable t15 ON t15.PONumber = t1.PONumber AND t15.LNSKUNumber=t1.LNSKUNumber AND t15.SizeDescription='130B'


    group by t1.SizeDescription, t1.PONumber, t1.LNSKUNumber