J King
J King

Reputation: 4434

T-SQL (azure compatible) get values from one column as a row of data

I am having trouble getting a query from a table. I have a Product Table and an Images table

[Images] table schema [Column name:data type]

[ImagetId:int] [ProductId:int] [url:nvarchr(max)] [Size50:bit] [Size100:bit] [Size200:bit][Size400:bit] [Size600:bit] [Size800:bit]

[Product] table schema

[ProductId:int] [Name:nvarchar(50)] additional columns...

The images table has 6 records for each Product and the Bit/Boolean fields from the Images table specify what size of image the url points to.

WHAT I WANT

I want to get each URL for the 6 sizes of images for each product in a row of data:

Desired Query Schema

         [ProductId]  [url50]      [url100]   [url200]   [url400]   [url600]   [url800]
 --------------------------------------------------------------------------------------
 row 1  |     1      | http:...  | http:    | http:    | http:    | http:    | http:
 --------------------------------------------------------------------------------------
 row 2  |     2      | http:...  | http:    | http:    | http:    | http:    | http:
 --------------------------------------------------------------------------------------
 row 3  |     3      | http:...  | http:    | http:    | http:    | http:    | http:
 --------------------------------------------------------------------------------------
 row 4  |     4      | http:...  | http:    | http:    | http:    | http:    | http:

What I've Tried

I tired to use inner joins on the image table for each image size like the following:

SELECT  KrisisStore_Images.ImageId
        , KrisisStore_Images.PortalId
        , KrisisStore_Images.ProductId
        , KrisisStore_Images.Name
        , KrisisStore_Images.Description
        , KrisisStore_Images.PrimaryImage
        , Thumb50.Url AS UrlThumb50
        , Thumb100.Url AS UrlThumb100
FROM    KrisisStore_Images 
        INNER JOIN (SELECT ImageId, Url FROM KrisisStore_Images AS KrisisStore_Images_1 WHERE (Thumb50 = 1)
                    ) AS Thumb50 ON KrisisStore_Images.ImageId = Thumb50.ImageId 
        INNER JOIN (SELECT ImageId, Url FROM KrisisStore_Images AS KrisisStore_Images_2 WHERE (Thumb100 = 1)
                    ) AS Thumb100 ON KrisisStore_Images.ImageId = Thumb100.ImageId

But that generates no resutls

If I change the join type to LEFT OUTER JOIN then I get a record row for each image size, not each product Like I want.

I also tried using Pivot (which I have never used) but I could not figure our how to do it based on the bit data type and I don't need any aggregate functions.

QUESTION

Can someone help me get each of the 6 image size URLs as a column in a single row for each product ID. Also, I need it to be Sql Azure compatible.

thanks in advance.

Upvotes: 0

Views: 98

Answers (1)

sgeddes
sgeddes

Reputation: 62831

You are looking to PIVOT your results. One option is to use MAX with CASE:

SELECT P.ProductId, 
   MAX(CASE WHEN I.Size50 = 1 THEN I.Url END) url50,
   MAX(CASE WHEN I.Size100 = 1 THEN I.Url END) url100,
   MAX(CASE WHEN I.Size200 = 1 THEN I.Url END) url200,
   MAX(CASE WHEN I.Size400 = 1 THEN I.Url END) url400,
   MAX(CASE WHEN I.Size600 = 1 THEN I.Url END) url600,
   MAX(CASE WHEN I.Size800 = 1 THEN I.Url END) url800
FROM Product P 
   JOIN Image I ON P.ProductId = I.ProductId
GROUP BY P.ProductID

Upvotes: 2

Related Questions