在同一结果行中从SQL获取一条记录和SIMILAR记录

时间:2019-02-05 21:23:23

标签: sql sql-server

这很棘手,我被困住了。我需要从具有库存记录的表中接收SKU详细信息,并添加具有基于相同大小,相同品牌等匹配的另一个SKU的列。

所以我正在运行的查询是这样:

SELECT h.orders_header_id,
       h.order_number,
       CONVERT(VARCHAR,h.order_date, 102) AS order_date,
       h.first_sku,
       h.first_title,
       i.size,
       i.brand,
       i.pic,
       i.channel,
       h.c_email,
       h.delivery_status
FROM orders_header h
LEFT JOIN
  (SELECT sku,
          SIZE,
          brand,
          imageserver + sku + 'A.JPG' AS pic,
          channel
   FROM inv_ALL) i ON h.first_sku = i.sku
WHERE h.delivery_status LIKE '%|Delivered|%'
  AND h.delivery_status NOT LIKE '%Arrival in Destination Country%'
  AND h.delivery_status LIKE (CONVERT(VARCHAR, GETDATE(), 112) +'%')
  AND i.pic IS NOT NULL

我需要的是这些列:

S1,S1title,S1pic,S2,S2title,S2pic,S3,S3title,S3pic

其中S1是从inv_ALL表中提取的匹配SKU,其大小和品牌与h.first_sku相同,且i.availablequantity非零。 S1title将是标题,S1pic将是图片(如上)。 S2和S3相同。并且S1 / S2 / S3必须是first_sku以外的SKU。

这些应该是“热门推荐”,并在推荐时向客户展示。

从昨天开始我一直在尝试,但是想不出一种方法来使其将这些记录与添加的列放在同一行。

(Microsoft SQL Server 2014-12.0.2000.8(X64)     2014年2月20日20:04:26     版权所有(c)Microsoft Corporation     Windows NT 6.1(内部版本7601:Service Pack 1)上的Developer Edition(64位)

以下是当前结果的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

因此,经过长时间的奋斗,我已经做好了工作。结果恰好是我想要的结果,但是我很确定这远不是节省资源的。

我创建了一个临时表变量,并将具有匹配属性的项目放在此处。但是要选择3个匹配项,我在同一张表上使用JOIN三次(我敢打赌可以用更好的方法代替),因为我想根据排名在每列中选择不同的项。

DECLARE @skus TABLE
(rk INT, sku VARCHAR(50), brandsize VARCHAR(300), title VARCHAR(500), pic VARCHAR(500))
INSERT INTO @skus
    SELECT ROW_NUMBER() OVER(PARTITION BY channel + '_' + brand + '_' + size ORDER BY sku) AS rk, sku, channel + '_' + brand + '_' + size, title, imageserver + sku + 'A.JPG'
    FROM inv_ALL
    WHERE availablequantity > 0
    AND channel + '_' + brand + '_' + size IS NOT NULL
    ORDER BY channel + '_' + brand + '_' + size

SELECT h.orders_header_id, h.order_number, CONVERT(VARCHAR,h.order_date,102) AS order_date, h.site, h.first_sku, h.first_title, i.size, i.brand, i.pic, i.channel, h.c_email, h.c_name, h.c_address1, h.c_address2, h.c_address3, h.c_city, h.c_state, h.c_zip, h.c_country, h.c_phone, h.delivery_status, s1.t1, s1.p1, s2.t2, s2.p2, s3.t3, s3.p3
FROM chanadv.dbo.orders_header h
LEFT JOIN (SELECT sku, size, brand, imageserver + sku + 'A.JPG' AS pic, channel FROM casql.dbo.inv_ALL) i ON h.first_sku = i.sku
LEFT JOIN (SELECT brandsize, title AS t1, pic AS p1 FROM @skus WHERE rk = 1) s1 ON i.channel + '_' + i.brand + '_' + i.size = s1.brandsize
LEFT JOIN (SELECT brandsize, title AS t2, pic AS p2 FROM @skus WHERE rk = 2) s2 ON i.channel + '_' + i.brand + '_' + i.size = s2.brandsize
LEFT JOIN (SELECT brandsize, title AS t3, pic AS p3 FROM @skus WHERE rk = 3) s3 ON i.channel + '_' + i.brand + '_' + i.size = s3.brandsize
WHERE h.delivery_status LIKE '%|Delivered|%'
AND h.delivery_status NOT LIKE '%Arrival in Destination Country%'
AND h.delivery_status LIKE (CONVERT(VARCHAR, GETDATE(), 112) +'%')
AND h.c_country = ''
AND i.pic IS NOT NULL