SQL Server 2008 不带聚合的数据透视

发布于 2024-12-29 01:10:16 字数 1030 浏览 3 评论 0原文

我在尝试在桌子上执行枢轴操作时遇到问题。我想要的示例如下所示。

ProductBarcode    ProductID
--------------    ---------
1000              P1
1001              P1
1002              P2
1003              P3
1004              P4
1005              P4

现在我想将上表转换成如下所示。

ProductID    Barcode1    Barcode2
---------    --------    --------
P1           1000        1001
P2           1002        
P3           1003        
P4           1004        1005

我试图用以下查询来解决这个问题,但它没有给出所需的结果:

SELECT 
  [r1].[productID],
  [r1].[Productbarcode] as Barcode1,
  [r2].[ProductBarcode] as Barcode2
FROM products as r1 right JOIN products as r2 on r1.[productID] = r2.[productID]

现在这只是一个示例,在实际情况中,有数百种产品具有多个条形码。

我什至尝试使用以下查询,但我得到的只是两个条形码列中的空值。

SELECT productID,[barcode1],[barcode2]
FROM
(SELECT barcode, productID
FROM products) as TableToBePivoted
PIVOT
(MAX(barcode)
FOR barcode IN ([barcode1], [barcode2])
) AS PivotedTable;

任何帮助将不胜感激。

I am facing a problem trying to perform a pivot on the table. A sample of what I want is as shown below.

ProductBarcode    ProductID
--------------    ---------
1000              P1
1001              P1
1002              P2
1003              P3
1004              P4
1005              P4

Now I want to transform the above table into something as below.

ProductID    Barcode1    Barcode2
---------    --------    --------
P1           1000        1001
P2           1002        
P3           1003        
P4           1004        1005

I was trying to work it out with the following query but it wasn't giving the required results:

SELECT 
  [r1].[productID],
  [r1].[Productbarcode] as Barcode1,
  [r2].[ProductBarcode] as Barcode2
FROM products as r1 right JOIN products as r2 on r1.[productID] = r2.[productID]

Now this is just an example and in actual case, there are hundreds of products which have multiple barcodes.

I have even tried using the following query but all I got was a null in both the barcode columns.

SELECT productID,[barcode1],[barcode2]
FROM
(SELECT barcode, productID
FROM products) as TableToBePivoted
PIVOT
(MAX(barcode)
FOR barcode IN ([barcode1], [barcode2])
) AS PivotedTable;

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枉心 2025-01-05 01:10:16

没有聚合就无法进行 PIVOT。

但这里是如何获得你想要的,输入你想要的任意数量的列(条形码):

CREATE TABLE #table1(
    ProductBarcode VARCHAR(10),
    ProductID  VARCHAR(10)
);

INSERT INTO #table1(ProductBarcode, ProductID)
VALUES
('1000' ,'P1'),
('1001' ,'P1'),
('1002' ,'P2'),
('1003' ,'P3'),
('1004' ,'P4'),
('1005' ,'P4');


WITH T AS(
    SELECT 'Barcode' + RTRIM(LTRIM(STR( ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY  ProductBarcode)))) AS BarcodeNum,
           ProductBarcode, 
           ProductID    
           FROM #table1
) 
SELECT * FROM T
PIVOT(MAX(ProductBarcode) FOR BarcodeNum IN([Barcode1], [Barcode2])) P

结果:

ProductID  Barcode1   Barcode2
---------- ---------- ----------
P1         1000       1001
P2         1002       NULL
P3         1003       NULL
P4         1004       1005

No way to PIVOT without aggregating.

But here is how to get what you want, enter however many columns (barcodes) you want:

CREATE TABLE #table1(
    ProductBarcode VARCHAR(10),
    ProductID  VARCHAR(10)
);

INSERT INTO #table1(ProductBarcode, ProductID)
VALUES
('1000' ,'P1'),
('1001' ,'P1'),
('1002' ,'P2'),
('1003' ,'P3'),
('1004' ,'P4'),
('1005' ,'P4');


WITH T AS(
    SELECT 'Barcode' + RTRIM(LTRIM(STR( ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY  ProductBarcode)))) AS BarcodeNum,
           ProductBarcode, 
           ProductID    
           FROM #table1
) 
SELECT * FROM T
PIVOT(MAX(ProductBarcode) FOR BarcodeNum IN([Barcode1], [Barcode2])) P

Results:

ProductID  Barcode1   Barcode2
---------- ---------- ----------
P1         1000       1001
P2         1002       NULL
P3         1003       NULL
P4         1004       1005
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文