0

I am pulling a basic query for a new report and have come across this little snag.

I have two tables Buildings (B) and Pictures (P) the key that is in both is the BuildingNumber. I have a simple Inner join between the two tables. The problem that I am having is that I have 183 buildings that are in B, but there are multiple pictures for each building, and the number of pictures is not consistent for each building. So when I get the result set, it is returning about 260 rows. What I would like to do is return just one row for each building and have a column for each picture associated with that building. Keep in mind that I do not have the rights to alter or create tables.

My Data is like this:

Building Table (B):

BuildingNumber   BldgName   Floors   SqFt

  0001           Science       5     50000
  0002           Engineering   4     40000

Picture Table (P):

BuildingNumber     PictureURL

   0001                URL1
   0001                URL2
   0001                URL3  
   0002                URL1 
   0002                URL2

So my desired result set it like this:

BuildingNumber   BldgName   Floors   SqFt   PictureURL1   PictureURL2   PictureURL3

    0001         Science        5     50000       URL1        URL2         URL3
    0002         Engineering    4     40000       URL1        URL2          NULL
4

1 回答 1

4

您可以使用PIVOT函数来获取结果,该函数将您的数据行转换为列。

如果PictureURLs每个建筑物的数量有限,则可以对查询进行硬编码:

select BuildingNumber, bldgname, floors, sqft,
  PictureURL1, PictureURL2, PictureURL3
from
(
  select b.BuildingNumber, b.bldgname, b.floors, b.sqft,
    p.PictureURL,
    col = 'PictureURL'+
            cast(row_number() over(partition by b.BuildingNumber
                                    order by b.BuildingNumber) as varchar(10))
  from building b
  inner join picture p
    on b.BuildingNumber = p.BuildingNumber
) d
pivot
(
  max(PictureURL)
  for col in (PictureURL1, PictureURL2, PictureURL3)
) piv;

请参阅SQL Fiddle with Demo。但是,如果您有未知数量的值,那么您将不得不考虑使用动态 SQL。这将创建一个 sql 字符串,该字符串将被执行以获得最终结果:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME('PictureURL'+
                                                      cast(row_number() over(partition by BuildingNumber
                                                                              order by BuildingNumber) as varchar(10))) 
                    from Picture
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT BuildingNumber, bldgname, floors, sqft,' + @cols + ' 
            from 
            (
              select b.BuildingNumber, b.bldgname, b.floors, b.sqft,
                p.PictureURL,
                col = ''PictureURL''+
                        cast(row_number() over(partition by b.BuildingNumber
                                                order by b.BuildingNumber) as varchar(10))
              from building b
              inner join picture p
                on b.BuildingNumber = p.BuildingNumber
            ) x
            pivot 
            (
                max(PictureURL)
                for col in (' + @cols + ')
            ) p '

execute sp_executesql @query;

请参阅SQL Fiddle with Demo。两者都给出结果:

| BUILDINGNUMBER |    BLDGNAME | FLOORS |  SQFT | PICTUREURL1 | PICTUREURL2 | PICTUREURL3 |
|              1 |     Science |      5 | 50000 |        URL1 |        URL2 |        URL3 |
|              2 | Engineering |      4 | 40000 |        URL1 |        URL2 |      (null) |
于 2013-09-04T15:58:18.557 回答