求教高手一条高难度SQL语句
表1
PO ITEM_NO QTY DEST_NAME _120 _125 _130 ... _330 _XO
11 22 30 AAAA 0 0 15 15 0
11 22 45 BBBB 0 10 20 10 0
11 22 15 DDDD 0 0 0 0 15
字段中_120 _125 _XO等是鞋子的尺寸(有很多的尺寸,这里简写了)
转化成表2
PO ITEM_NO QTY SIZE AAAA BBBB CCCC DDDD
11 22 30 _125 15 0 0 0
11 22 30 _330 15 0 0 0
11 22 45 _125 0 10 0 0
11 22 45 _130 0 20 0 0
11 22 45 _330 0 10 0 0
11 22 15 _XO 0 10 0 15
没有数量的鞋子尺寸不会出现在表2里,同时会列出所有的订货商.
烦请高手帮忙看下,小弟在此感激万分!
------解决方案--------------------select PO, ITEM_NO, QTY,SIZE = '_120 ',
max(case when DEST_NAME = 'AAAA ' then _120 else 0 end) 'AAAA ',
max(case when DEST_NAME = 'bbbb ' then _120 else 0 end) 'bbbb ',
max(case when DEST_NAME = 'cccc ' then _120 else 0 end) 'cccc ',
max(case when DEST_NAME = 'dddd ' then _120 else 0 end) 'dddd '
from tb group by PO, ITEM_NO, QTY
union all
select PO, ITEM_NO, QTY,SIZE = '_125 ',
max(case when DEST_NAME = 'AAAA ' then _125 else 0 end) 'AAAA ',
max(case when DEST_NAME = 'bbbb ' then _125 else 0 end) 'bbbb ',
max(case when DEST_NAME = 'cccc ' then _125 else 0 end) 'cccc ',
max(case when DEST_NAME = 'dddd ' then _125 else 0 end) 'dddd '
from tb group by PO, ITEM_NO, QTY
..............
union all
select PO, ITEM_NO, QTY,SIZE = '_XO ',
max(case when DEST_NAME = 'AAAA ' then _XO else 0 end) 'AAAA ',
max(case when DEST_NAME = 'bbbb ' then _XO else 0 end) 'bbbb ',
max(case when DEST_NAME =