如何从2行中获取数据,除了MSSQL中的一行外,所有列中的数据都相同

时间:2018-04-19 12:10:19

标签: sql sql-server sql-server-2008

在我的标题中,我想从2行获取数据但在我的情况下,每个第2行与第一行相比有一个不同的值。

我想把所有常见数据和不同数据作为一行。

在这里,您可以看到每一行在另一行中具有相同的值,但最后一列的第二行除外。

enter image description here

感谢。

编辑结果:

enter image description here

2 个答案:

答案 0 :(得分:1)

我怀疑您有某种排序可以指定您的实际数据排序,如果是,那么您可以使用row_number()函数

select * from (
   select *,
           row_number() over (partition by <common data cols> order by ? desc) Seq  
   from table t
) t
where seq = 1;

编辑:我不相信您的inventort_item_id列,但是您可以使用creation_date进行订购

SELECT 
       EPI.ITEM_CODE, LMP.PROD_DESC, LLPC.COLOC_PROD_PRICE,
       BASE_PATH + '' + EPI.IMAGE_FOLDER_NAME + '/' + EPI.IMAGE_DESCRIPTION AS POPULAR_PRODUCTS_IMAGE_PATHS 
FROM (SELECT *,
             ROW_NUMBER() OVER (PARTITION BY ITEM_CODE ORDER BY creation_date DESC) as Seq 
      FROM ECOM_PRODUCT_IMAGES EPI
     ) EPI
INNER JOIN ECOM_POPULAR_PRODUCTS_MAPPING EPPIM ON EPPIM.ITEM_CODE = EPI.ITEM_CODE
INNER JOIN LOM_MST_PRODUCT LMP ON LMP.PROD_CODE = EPI.ITEM_CODE 
INNER JOIN LOM_LNK_PROD_COMP LLPC ON LLPC.COLOC_PROD_CODE = LMP.PROD_CODE
WHERE EPI.Seq = 1 AND 
      EPPIM.ITEM_STATUS = 'ACTIVE';

编辑2:在这种情况下,您需要将GROUP BY子句与条件聚合一起使用

SELECT 
         EPI.ITEM_CODE, LMP.PROD_DESC, LLPC.COLOC_PROD_PRICE,
         MAX(CASE WHEN EPI.Seq = 2 
                  THEN (BASE_PATH + '' + EPI.IMAGE_FOLDER_NAME + '/' + EPI.IMAGE_DESCRIPTION) 
             END) AS POPULAR_PRODUCTS_IMAGE_PATHS,
         MAX(CASE WHEN EPI.Seq = 1
                  THEN (BASE_PATH + '' + EPI.IMAGE_FOLDER_NAME + '/' + EPI.IMAGE_DESCRIPTION) 
             END) AS PATH_NEW
FROM (SELECT *,
                 ROW_NUMBER() OVER (PARTITION BY ITEM_CODE ORDER BY creation_date DESC) as Seq 
      FROM ECOM_PRODUCT_IMAGES EPI
     ) EPI
INNER JOIN ECOM_POPULAR_PRODUCTS_MAPPING EPPIM ON EPPIM.ITEM_CODE = EPI.ITEM_CODE
INNER JOIN LOM_MST_PRODUCT LMP ON LMP.PROD_CODE = EPI.ITEM_CODE 
INNER JOIN LOM_LNK_PROD_COMP LLPC ON LLPC.COLOC_PROD_CODE = LMP.PROD_CODE
WHERE EPPIM.ITEM_STATUS = 'ACTIVE'
GROUP BY EPI.ITEM_CODE, LMP.PROD_DESC, LLPC.COLOC_PROD_PRICE;

答案 1 :(得分:1)

这是我的方法,也是使用窗口函数。

示例数据

if object_id('tempdb..#x') is not null drop table #x
CREATE TABLE #x (ITEM_CODE VARCHAR(10), PROD_DESC VARCHAR(20), 
                COLOR_PROD_PRICE DECIMAL, POPULAR_PRODUCTS_IMAGE_PATHS VARCHAR(200))
INSERT INTO #X(ITEM_CODE,PROD_DESC,COLOR_PROD_PRICE,POPULAR_PRODUCTS_IMAGE_PATHS) VALUES
('P0001', 'Axe Brand', 88.000, 'some_path_to_img1.jpg'),
('P0001', 'Axe Brand', 88.000, 'some_path_to_img2.jpg'),
('P0002', 'Almond Nuts', 499.000, 'some_path_to_img1.jpg'),
('P0002', 'Almond Nuts', 499.000, 'some_path_to_img2.jpg')

查询 - 只需将#x更改为您的表格即可正常运行

;WITH my_cte as
(
    SELECT *,
         ROW_NUMBER() OVER(PARTITION BY ITEM_CODE ORDER BY POPULAR_PRODUCTS_IMAGE_PATHS) AS 'track_row'
    FROM #x 

)

SELECT a.ITEM_CODE, a.PROD_DESC, a.COLOR_PROD_PRICE,
        a.POPULAR_PRODUCTS_IMAGE_PATHS + ' ' + b.POPULAR_PRODUCTS_IMAGE_PATHS AS 'POPULAR_PRODUCTS_IMAGE_PATHS'
FROM my_cte AS a
     INNER JOIN
     my_cte AS b ON a.ITEM_CODE=b.ITEM_CODE
WHERE a.track_row=1 AND b.track_row=2

输出

ITEM_CODE   PROD_DESC   COLOR_PROD_PRICE    POPULAR_PRODUCTS_IMAGE_PATHS
P0001      Axe Brand    88                 some_path_to_img1.jpg some_path_to_img2.jpg
P0002      Almond Nuts  499                some_path_to_img1.jpg some_path_to_img2.jpg