Anda dapat dengan mudah membagi YP
string menggunakan LEFT()
, RIGHT()
, SUBSTRING()
, dll. Saran saya adalah bagaimana Anda menangani UNPIVOT
. Anda . Sepertinya Anda memiliki banyak kolom untuk UNPIVOT
jadi saran saya mungkin menerapkan SQL dinamis untuk melakukan kueri ini. Anda akan melakukannya dengan cara ini:
declare @colsUnpivot varchar(max),
@query AS NVARCHAR(MAX),
@cols varchar(max)
select @colsUnpivot = stuff((select ','
+quotename(replace(C.name, 'Qty', ''))
from sys.columns as C
where C.object_id = object_id('yourtable') and
C.name like 'Qty%'
for xml path('')), 1, 1, '')
select @cols = stuff((select ','
+quotename(C.name) + ' as ' + replace(C.name, 'Qty', '')
from sys.columns as C
where C.object_id = object_id('yourtable') and
C.name like 'Qty%'
for xml path('')), 1, 1, '')
set @query
= 'select rowid,
left(YP, 1) YP,
cast(right(YP, len(YP) - 1) as int) period,
Val
from
(
select rowid, ' + @cols + '
from yourtable
) x1
unpivot
(
val for YP IN (' + @colsUnpivot + ')
) u'
exec(@query)