是否可以使用 headers 作为列并使用值作为另一列来反转 sql 服务器 table?
is possible to unpivot a sql server table using headers as a column and values as another column?
我有一个 table 这样的:
TableName
dates
ModelName
BaseUnitPerPallet
pallet
Calendar
June
Null
4
1
Country
June
Null
2
6
Product
June
DOWNSTREAM
Null
8
ProductBOM
June
DOWNSTREAM
9
9
我想要这样的 table:
Columns
values
TableName
Calendar
TableName
Country
TableName
Product
TableName
ProductBOM
其中 columns 字段是前一个 table 的 headers,values 是逆透视方式的值。
我一直在尝试逆轴逻辑但没有成功:
SELECT Columns, Values
FROM
(
SELECT TableName, dates, ModelName, BaseUnitPerPallet, pallet
FROM Database
as source_query
)
UNPIVOT
(
Values FOR Columns IN ( TableName, dates, ModelName, BaseUnitPerPallet, pallete)
)
as pivot_results
任何建议或指导都会很棒。
另外,有什么资源可以做到这一点吗?并在不写列名的情况下应用逻辑?
提前致谢¡
我建议使用 APPLY 来取消你的 table
使用 APPLY 反透视
DROP TABLE IF EXISTS #YourTable
CREATE TABLE #YourTable (
ID INT IDENTITY(1,1) PRIMARY KEY
,TableName VARCHAR(100)
,Dates Varchar(25)
,ModelName VARCHAR(100)
,BaseUnitPerPallet TINYINT
,Pallet TINYINT
)
INSERT INTO #YourTable
VALUES
('Calendar','June',NULL,4,1)
,('Country','June',NULL,2,6)
,('Product','June','DOWNSTREAM',NULL,8)
,('ProductBOM','June','DOWNSTREAM',9,9)
SELECT A.ID,B.*
FROM #YourTable AS A
CROSS APPLY
(VALUES
('TableName',A.TableName)
,('Dates',A.Dates)
,('ModelName',A.ModelName)
,('BaseUnitPerPallet',CAST(A.BaseUnitPerPallet AS Varchar(100)))
,('Pallet',CAST(A.Pallet AS Varchar(100)))
) AS B(ColumnName,Val)
--WHERE B.Val IS NOT NULL /*Optional in case you want to ignore NULLs*/
ORDER BY A.ID,B.ColumnName
我有一个 table 这样的:
TableName | dates | ModelName | BaseUnitPerPallet | pallet |
---|---|---|---|---|
Calendar | June | Null | 4 | 1 |
Country | June | Null | 2 | 6 |
Product | June | DOWNSTREAM | Null | 8 |
ProductBOM | June | DOWNSTREAM | 9 | 9 |
我想要这样的 table:
Columns | values |
---|---|
TableName | Calendar |
TableName | Country |
TableName | Product |
TableName | ProductBOM |
其中 columns 字段是前一个 table 的 headers,values 是逆透视方式的值。
我一直在尝试逆轴逻辑但没有成功:
SELECT Columns, Values
FROM
(
SELECT TableName, dates, ModelName, BaseUnitPerPallet, pallet
FROM Database
as source_query
)
UNPIVOT
(
Values FOR Columns IN ( TableName, dates, ModelName, BaseUnitPerPallet, pallete)
)
as pivot_results
任何建议或指导都会很棒。 另外,有什么资源可以做到这一点吗?并在不写列名的情况下应用逻辑?
提前致谢¡
我建议使用 APPLY 来取消你的 table
使用 APPLY 反透视
DROP TABLE IF EXISTS #YourTable
CREATE TABLE #YourTable (
ID INT IDENTITY(1,1) PRIMARY KEY
,TableName VARCHAR(100)
,Dates Varchar(25)
,ModelName VARCHAR(100)
,BaseUnitPerPallet TINYINT
,Pallet TINYINT
)
INSERT INTO #YourTable
VALUES
('Calendar','June',NULL,4,1)
,('Country','June',NULL,2,6)
,('Product','June','DOWNSTREAM',NULL,8)
,('ProductBOM','June','DOWNSTREAM',9,9)
SELECT A.ID,B.*
FROM #YourTable AS A
CROSS APPLY
(VALUES
('TableName',A.TableName)
,('Dates',A.Dates)
,('ModelName',A.ModelName)
,('BaseUnitPerPallet',CAST(A.BaseUnitPerPallet AS Varchar(100)))
,('Pallet',CAST(A.Pallet AS Varchar(100)))
) AS B(ColumnName,Val)
--WHERE B.Val IS NOT NULL /*Optional in case you want to ignore NULLs*/
ORDER BY A.ID,B.ColumnName