在INNER JOIN
之后,我得到6行,相同的location_id
,相同的location_name
但不同的图像值,
每个location_id
都有6个相应的图片,as this image
但我想将图片行转换为列(img1, img2, img3 ... img6
)
所以最终结果表将是[location_id, location_name, img1, img2, img3, img4, img5, img6]
如何构建结果?
答案 0 :(得分:4)
假设您最多只有6张图片
select
location_id, location_name,
[1] as img1,
[2] as img2,
[3] as img3,
[4] as img4,
[5] as img5,
[6] as img6
from
(
select
location_id, location_name,img_name,
row_number() over(partition by location_id order by img_name) rn
from innrjointbl
)s
PIVOT
(MAX(img_name) for rn in ([1],[2],[3],[4],[5],[6])) p
示例SQL小提琴: http://sqlfiddle.com/#!3/e89d94/2