[英]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 ] [Images]表模式[列名: 数据类型 ]
[ImagetId: int ] [ProductId: int ] [url: nvarchr(max) ] [Size50: bit ] [Size100: bit ] [Size200: bit ][Size400: bit ] [Size600: bit ] [Size800: bit ] [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... [ProductId: int ] [名称: nvarchar(50) ]附加列...
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. images表有每个Product的6条记录,Images表中的Bit / Boolean字段指定url指向的图像大小。
I want to get each URL for the 6 sizes of images for each product in a row of data: 我想获取一行数据中每种产品的6种尺寸图像的每个URL:
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:
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. 如果我将连接类型更改为LEFT OUTER JOIN,那么我会得到每个图像大小的记录行,而不是每个产品都像我想要的那样。
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. 我也尝试使用Pivot(我从未使用过),但我无法根据位数据类型计算出如何做到这一点,而且我不需要任何聚合函数。
Can someone help me get each of the 6 image size URLs as a column in a single row for each product ID. 有人可以帮助我将每个6个图像大小的URL作为每行产品ID的一行列。 Also, I need it to be Sql Azure compatible.
另外,我需要它与Sql Azure兼容。
thanks in advance. 提前致谢。
You are looking to PIVOT
your results. 您正在寻找
PIVOT
您的结果。 One option is to use MAX
with CASE
: 一种选择是使用
MAX
和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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.