sql - TSQL Interleaved sequence without join operations or user defined objects -
is possible create interleaved sequence first , last values going in turns untill data set empty without using joins , user defined functions or procedures?
code type model price 1 hats 1298 700,00 1 shoes 1232 600,00 1 pants 1276 400,00 2 hats 1321 970,00 2 shoes 1121 850,00 2 pants 1433 270,00 3 hats 1750 1200,00 3 shoes 1233 600,00 3 pants 1434 290,00 4 hats 1298 1050,00 4 shoes 1121 850,00 4 pants 1401 150,00 5 hats 1752 1150,00 5 shoes 1121 850,00 5 pants 1408 270,00 6 hats 1298 950,00 6 shoes 1233 950,00 6 pants 1288 400,00 7 shoes 1232 400,00 8 shoes 1232 350,00 9 shoes 1232 350,00 10 shoes 1260 350,00 11 shoes 1233 980,00 12 shoes 1233 970,00
i have added spaces between rows interleaved sequence idea.
you want odd values going lowest coded items (asc) , values highest coded items (desc). want order type hats,shoes , pants.
code type model price 1 hats 1298 700,00 1 shoes 1232 600,00 1 pants 1276 400,00 6 hats 1298 950,00 12 shoes 1233 970,00 6 pants 1288 400,00 2 hats 1321 970,00 2 shoes 1121 850,00 2 pants 1433 270,00 5 hats 1752 1150,00 11 shoes 1233 980,00 5 pants 1408 270,00 3 hats 1750 1200,00 3 shoes 1233 600,00 3 pants 1434 290,00 4 hats 1298 1050,00 10 shoes 1260 350,00 4 pants 1401 150,00 4 shoes 1121 850,00 9 shoes 1232 350,00 5 shoes 1121 850,00 8 shoes 1232 350,00 6 shoes 1233 950,00 7 shoes 1232 400,00
right came solution includes joins looking work without using it.
my solution using joins:
with cteasc ( select row_number() over(order code,charindex(type, 'hatsshoespants'))id ,(row_number() over(partition type order code,charindex(type, 'hatsshoespants')) + 1) / 2 offsetasc ,code,model,price,type mydata ), ctedsc ( select row_number() on (partition type order code desc)id ,code,model,price,type cteasc ) select t1.id ,case when t1.code%2=1 lag(t1.type,t1.code-t1.offsetasc,t1.type)over(partition t1.type order t1.id) else lag(t2.type,t1.code-t1.offsetasc,t1.type)over(partition t1.type order t1.id) end type ,case when t1.code%2=1 lag(t1.model,t1.code-t1.offsetasc,t1.model)over(partition t1.type order t1.id) else lag(t2.model,t1.code-t1.offsetasc,t1.model)over(partition t1.type order t1.id) end model ,case when t1.code%2=1 lag(t1.price,t1.code-t1.offsetasc,t1.price)over(partition t1.type order t1.id) else lag(t2.price,t1.code-t1.offsetasc,t1.price)over(partition t1.type order t1.id) end price cteasc t1 join ctedsc t2 on t1.code = t2.id , t1.type = t2.type
the idea generate 2 sequences:
- the ascending items row numbers 1, 3, 5, ...
- the descending items row numbers 2, 4, 6, ...
then, need union all
sequences , sort them.
with ascitems ( select *, row_number() on (order somecol asc) r * 2 - 1 r t ) , descitems ( select *, row_number() on (order somecol desc) r * 2 r t ) select * ascitems union select * descitems order r
this should require table t available sorted twice (either index or 2xsorting). union all
should manifest cheap merge concat.
Comments
Post a Comment