从联接表中获取列作为行而不是相邻列

时间:2019-06-26 12:14:21

标签: sql sql-server join

我有三个表A_PRODUCT,A_MODEL和A_AREA。这些表可以使用PRODUCT_ID进行联接。我想要得到一个输出,以便产品的A_PRODUCT表中的行之后是A_MODEL表中的行,然后是A_AREA表中的行。以下是创建示例数据的SQL:

CREATE TABLE A_PRODUCT
(
PRODUCT_ID NVARCHAR(30),
PODUCT_NAME NVARCHAR(30),
);

INSERT INTO A_PRODUCT (PRODUCT_ID, PODUCT_NAME)
VALUES ('P-1', 'LONG-SLEEVE LOGO JERSEY');

INSERT INTO A_PRODUCT (PRODUCT_ID, PODUCT_NAME)
VALUES ('P-2', 'GLASSES');


CREATE TABLE A_MODEL
(
MODEL_ID NVARCHAR(30),
MODEL_NAME NVARCHAR(30),
PRODUCT_ID NVARCHAR(30)
);

INSERT INTO A_MODEL (MODEL_ID, MODEL_NAME, PRODUCT_ID)
VALUES ('M-1', 'OLD JERSEY', 'P-1');

INSERT INTO A_MODEL (MODEL_ID, MODEL_NAME, PRODUCT_ID)
VALUES ('M-2', 'RAY-BAN', 'P-2');

INSERT INTO A_MODEL (MODEL_ID, MODEL_NAME, PRODUCT_ID)
VALUES ('M-3', 'ARMANI', 'P-2');

CREATE TABLE A_AREA
(
AREA_ID NVARCHAR(30),
AREA_NAME NVARCHAR(30),
PRODUCT_ID NVARCHAR(30)
);

INSERT INTO A_AREA (AREA_ID, AREA_NAME, PRODUCT_ID)
VALUES ('A-1', 'SOUTH', 'P-1');

INSERT INTO A_AREA (AREA_ID, AREA_NAME, PRODUCT_ID)
VALUES ('A-2', 'NORTH', 'P-1');

INSERT INTO A_AREA (AREA_ID, AREA_NAME, PRODUCT_ID)
VALUES ('A-3', 'EAST', 'P-1');

INSERT INTO A_AREA (AREA_ID, AREA_NAME, PRODUCT_ID)
VALUES ('A-4', 'WEST', 'P-1');

INSERT INTO A_AREA (AREA_ID, AREA_NAME, PRODUCT_ID)
VALUES ('A-5', 'SOUTH-WEST', 'P-2');

我的预期输出是:

|PRODUCT|P-1|LONG-SLEEVE LOGO JERSEY|
|MODEL|M-1|OLD JERSEY|
|AREA|A-1|SOUTH|
|AREA|A-2|NORTH|
|AREA|A-3|EAST|
|AREA|A-4|WEST|
|PRODUCT|P-2|GLASSES|
|MODEL|M-2|RAY-BAN|
|MODEL|M-3|ARMANI|
|AREA|A-5|SOUTH-WEST|

我尝试了以下查询,但未提供所需的输出:

;WITH CTE AS 
(
SELECT CONCAT('|PRODUCT|', P.PRODUCT_ID,'|',P.PODUCT_NAME,'|') AS Col1,
        CONCAT('|MODEL|', M.MODEL_ID,'|',M.MODEL_NAME,'|') AS COl2,
        CONCAT('|AREA|', A.AREA_ID,'|',A.AREA_NAME,'|') AS COl3      
    FROM A_PRODUCT P
    JOIN A_MODEL M
    ON P.PRODUCT_ID = M.PRODUCT_ID
    JOIN A_AREA A
    ON P.PRODUCT_ID = A.PRODUCT_ID
 )
 SELECT Result
 FROM CTE
 CROSS APPLY (VALUES (Col1,'Col1'),(Col2,'Col2'),(Col3,'Col3')
              )DT (Result,Names);

2 个答案:

答案 0 :(得分:2)

您真的不想在这里JOIN。您需要使用PRODUCT_IDUNION的结果进行排序,并且需要基于信息所来自的表来强制进行其他排序。

使用子查询将数据按所需方式进行分组,并使用外部SELECT以获得导出数据所需的格式。

SELECT
  CONCAT('|',d.DetailCol1,'|',d.DetailCol2,'|', d.DetailCol3,'|') AS DelimitedList
FROM
  (
    SELECT
      PRODUCT_ID AS SortCol1,
      1 AS SortCol2,
      'PRODUCT' AS DetailCol1,
      PRODUCT_ID AS DetailCol2,
      PRODUCT_NAME AS DetailCol3
    FROM @A_PRODUCT
    UNION ALL
    SELECT
      PRODUCT_ID AS SortCol1,
      2 AS SortCol2,
      'MODEL' AS DetailCol1,
      MODEL_ID AS DetailCol2,
      MODEL_NAME AS DetailCol3
    FROM @A_MODEL
    UNION ALL
    SELECT
      PRODUCT_ID AS SortCol1,
      3 AS SortCol2,
      'AREA' AS DetailCol1,
      AREA_ID AS DetailCol2,
      AREA_NAME AS DetailCol3
    FROM @A_AREA
  ) AS d
ORDER BY 
  d.SortCol1,
  d.SortCol2;
+---------------------------------------+
|             DelimitedList             |
+---------------------------------------+
| |PRODUCT|P-1|LONG-SLEEVE LOGO JERSEY| |
| |MODEL|M-1|OLD JERSEY|                |
| |AREA|A-1|SOUTH|                      |
| |AREA|A-2|NORTH|                      |
| |AREA|A-3|EAST|                       |
| |AREA|A-4|WEST|                       |
| |PRODUCT|P-2|GLASSES|                 |
| |MODEL|M-2|RAY-BAN|                   |
| |MODEL|M-3|ARMANI|                    |
| |AREA|A-5|SOUTH-WEST|                 |
+---------------------------------------+

答案 1 :(得分:0)

您可以使用以下查询获取所需的输出-

   git commit 

输出为-(您可以根据需要删除值之间的空格)

SELECT '| PRODUCT | '+PRODUCT_ID+' | '+PODUCT_NAME +' |' FROM A_PRODUCT
UNION ALL
SELECT '| AREA | '+AREA_ID+' | '+AREA_NAME +' |' FROM A_AREA
UNION ALL
SELECT '| MODEL | '+MODEL_ID+' | '+MODEL_NAME +' |' FROM A_MODEL