我试图在我的查询中添加列如下

SELECT Name as [holiday_name] FROM tableMonth t UNPIVOT (ID for Name in (m1,m2,m3,sf1,sf2)) u WHERE ID != 0.0 and D_No ='0700'

此查询运行正常,但是当我添加“sf1”和“sf2”时,它给我错误
"The type of column "sf1" conflicts with the type of other columns specified in the UNPIVOT list."

我将如何使用要更新的列(如“sf1”和“sf2”)运行上述查询

希望给你建议

谢谢

最佳答案

在联波观察团期间创建新栏的一种方法

CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,
    Emp3 int, Emp4 int, Emp5 int);
GO
INSERT INTO pvt VALUES (1,4,3,5,4,4);
INSERT INTO pvt VALUES (2,4,1,5,5,5);
INSERT INTO pvt VALUES (3,4,3,5,4,4);
INSERT INTO pvt VALUES (4,4,2,5,5,4);
INSERT INTO pvt VALUES (5,5,1,5,5,5);
GO


    SELECT VendorID, newcolumn, Orders,
case when len(VendorID) > 1 then ' ' end newcolumn1 ,
case when len(VendorID) > 1 then ' ' end newcolumn2
    FROM
   (SELECT VendorID, emp1,emp2,emp3,emp4,emp5
   FROM pvt) p
UNPIVOT
   (Orders FOR newcolumn IN (emp1,emp2,emp3,emp4,emp5)
)AS unpvt;

我相信这是您的要求。

10-02 01:41