在SQL Server中透视非数字数据表

时间:2017-01-26 19:16:17

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

我是SQL Server的新手。我试图在Part_Number上转移此表,以便行和列互换。请注意Part_Number如何重复,IBA_NAME列包含所有属性。

PART_NUMBER IBA_NAME    PART_REVISION   PART_ITERATION  PART_NAME   IBAVALUE
161-83516-0014-M00  SPEED_GRADE A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  JEDEC_TYPE  A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  NV_LEAD_TIME    A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  VOLUME  A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  POW A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  LIBRARY A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  N/A
161-83516-0014-M00  Valid_To    A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  1111-11-11 00:00:00
161-83516-0014-M00  NV_COSTS    A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  ZZ_Item_Type    A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  Part: Mem (Concept)
161-83516-0014-M00  NV_PREFERENCE   A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  0
161-83516-0014-M00  SPARE   A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  ROHS, PROMOS
161-83516-0014-M00  HEIGHT  A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  NAME    A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  PACK_TYPE   A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  QUALIFIER   A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  PTF_NAME    A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  Parts   A   3   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  Part: Mem (Concept)
161-83516-0014-M00  SPEED_GRADE A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  JEDEC_TYPE  A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  NV_LEAD_TIME    A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  VOLUME  A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  POW A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  LIBRARY A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  N/A
161-83516-0014-M00  Valid_To    A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  1111-11-11 00:00:00
161-83516-0014-M00  NV_COSTS    A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  ZZ_Item_Type    A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  Part: Mem (Concept)
161-83516-0014-M00  NV_PREFERENCE   A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  0
161-83516-0014-M00  SPARE   A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  ROHS, PROMOS
161-83516-0014-M00  HEIGHT  A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  NAME    A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  PACK_TYPE   A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  QUALIFIER   A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  PTF_NAME    A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -
161-83516-0014-M00  Parts   A   4   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  Part: Mem (Concept)
161-83516-0014-M00  SPEED_GRADE A   5   MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS  -

我希望数据看起来像这样 - 基于IBA_NAME着色:

PART_NUMBER NAME    PACK_TYPE   QUALIFIER   PTF_NAME    SPEED_GRADE JEDEC_TYPE  NV_LEAD_TIME    VOLUME  POW LIBRARY

到目前为止,我尝试编写以下代码,但无济于事:

SELECT TOP (1000) 
    [PART_NUMBER],
    [PART_NAME], [PART_REVISION], [PART_ITERATION]
FROM 
    [PDPDATAMART].[dbo].[BI_PDP_partclass_d] 
PIVOT
    (MIN([VALUE])   
          FOR [PART_NUMBER] IN ([PART_NAME]) 
    ) AS p

任何人都可以概述,我做错了什么以及如何解决问题。帮助将不胜感激:))

1 个答案:

答案 0 :(得分:0)

这就是你想要的,只需删除你不需要的旋转列,因为我只是做了一个快速而又脏的,并包含了IBA_NAME中的所有值:

示例数据:

List<Document> pipeline = Arrays.asList(new Document("$project", new Document("rank", "$members.membername1.rank").append("_id", false)));
return ApiResponse.withBody(coll.aggregate(pipeline).first().getInteger("rank"));

QUERY:

IF OBJECT_ID('tempdb..#temp') IS NOT NULL
    DROP TABLE #temp;

CREATE TABLE #temp(PART_NUMBER    VARCHAR(50)
              , IBA_NAME       VARCHAR(50)
              , PART_REVISION  VARCHAR(50)
              , PART_ITERATION VARCHAR(50)
              , PART_NAME      VARCHAR(50)
              , IBAVALUE       VARCHAR(50));

INSERT INTO #temp
VALUES
('161-83516-0014-M00','SPEED_GRADE','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','JEDEC_TYPE','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','NV_LEAD_TIME','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','VOLUME','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','POW','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','LIBRARY','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','N/A'),
('161-83516-0014-M00','Valid_To','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','1111-11-11 00:00:00'),
('161-83516-0014-M00','NV_COSTS','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','ZZ_Item_Type','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','Part: Mem (Concept)'),
('161-83516-0014-M00','NV_PREFERENCE','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','0'),
('161-83516-0014-M00','SPARE','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','ROHS, PROMOS'),
('161-83516-0014-M00','HEIGHT','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','NAME','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','PACK_TYPE','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','QUALIFIER','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','PTF_NAME','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','Parts','A',3,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','Part: Mem (Concept)'),
('161-83516-0014-M00','SPEED_GRADE','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','JEDEC_TYPE','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','NV_LEAD_TIME','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','VOLUME','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','POW','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','LIBRARY','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','N/A'),
('161-83516-0014-M00','Valid_To','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','1111-11-11 00:00:00'),
('161-83516-0014-M00','NV_COSTS','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','ZZ_Item_Type','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','Part: Mem (Concept)'),
('161-83516-0014-M00','NV_PREFERENCE','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','0'),
('161-83516-0014-M00','SPARE','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','ROHS, PROMOS'),
('161-83516-0014-M00','HEIGHT','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','NAME','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','PACK_TYPE','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','QUALIFIER','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','PTF_NAME','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-'),
('161-83516-0014-M00','Parts','A',4,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','Part: Mem (Concept)'),
('161-83516-0014-M00','SPEED_GRADE','A',5,'MEM 32MX16 DDR2-533 266MHz 1.8V FBGA-84 PROMOS','-')

结果:

enter image description here