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

PONumbe LNSKU の異なる組み合わせに基づいて、SizeDescription とカウンターの値を複数の列に変換したい

出力は次のようになります

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

これに対する T-SQL クエリの書き方がわかりません。

ありがとう

4

2 に答える 2

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%'

それはあなたの出発点になるはずです...スキーマについて詳しく教えていただければ、少し詳しく説明できるかもしれません..

あなたのためにそれを少し肉付けしました

于 2011-12-08T21:30:49.047 に答える
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
于 2011-12-09T19:49:34.690 に答える