如何将两列透视到预定义表中?

时间:2018-09-19 20:18:25

标签: sql-server tsql pivot

我见过this questionthisthis,但是这些不是我想要的。

请不要关闭我的问题,因为它不是重复的。对我来说真的很重要。

我设法旋转了一个表,但这不是期望的结果: enter image description here

所需的结果如下: enter image description here

我的示例数据是(这只是一个示例,OperatorXOC列应为50倍):

DECLARE @OperatorPrice TABLE (ID INT NOT NULL, OperatorId INT NULL, Price 
NUMERIC(18,3) NULL, FName VARCHAR(50) NULL)

INSERT INTO @OperatorPrice (
ID, OperatorId, Price, FName
)
VALUES
  (226, 996, 22954,'Operator1')
, (266, 1016, 79011.2,   'Operator3')
, (112, 1029, 14869,     'Operator4')
, (93,   1031, 10568.96, 'Operator5')


DECLARE @TR TABLE 
(
 ID INT NULL , 
    Operator1  DECIMAL(18,3) NULL, OC1  DECIMAL(18,3) NULL, Operator2  DECIMAL(18,3) NULL, 
    OC2  DECIMAL(18,3) NULL, Operator3  DECIMAL(18,3) NULL, OC3  DECIMAL(18,3) NULL, 
    Operator4  DECIMAL(18,3) NULL, OC4  DECIMAL(18,3) NULL, Operator5  DECIMAL(18,3) NULL, 
    OC5  DECIMAL(18,3) NULL
)

示例代码:

INSERT @TR
(ID , 
Operator1, OC1, Operator2, OC2,  Operator3, OC3,  Operator4, OC4,  
Operator5, OC5)
SELECT ID , 
    Operator1, OC1, Operator2, OC2,  Operator3, OC3,  Operator4, OC4,  
Operator5, OC5
FROM 
(SELECT Price, id, FName 
FROM @OperatorPrice) AS SourceTable 
PIVOT 
( 
   sum(Price)
   FOR FName IN (Operator1, OC1, Operator2, OC2,  Operator3, OC3,  
   Operator4, OC4,  Operator5, OC5) 
) AS PivotTable

SELECT * FROM @TR

如何将数据插入OC列?

2 个答案:

答案 0 :(得分:1)

使用示例表和数据,通过条件聚合非常容易。虽然您不确定如何确定哪个操作员编号,但还不太清楚。希望您比从值中解析数字更好,但是谁知道呢。

var axios = require('axios');
var otp = ['bitcoin', 'cat', 'orange', 'mango', 'bats', 'california'];

const promises = otp.map(info => axios.get(`https://www.iot.com/api/?query=${info}`).then(res => res.data));
const newPromise = Promise.all(promises).then(stateData => 
    Promise.all(stateData.map(nextData => axios.get(`https://www.crypto.com/api/?query=${nextData.uuid}`).then(res => res.data)))
);

newPromise.then((res) => {
    console.log(res)
}, err => {
    console.log(err)
});

答案 1 :(得分:1)

也许是这样的。

在您的别名SOURCETABLE中,我们只为UNION ALL值的ID添加了NULL可能的组合。在这种情况下,MIN(ID)和值1-50

只需确保

1)用列Operator1,OC1,..,Operator50,OC50定义@TR <<<< / strong> OC#可以是INT

2)在FOR Item IN(Operator1,OC1,..,Operator50,OC50)

示例-编辑已更正,以允许> 9个运算符

INSERT @TR
SELECT *
FROM  (
        Select A.ID
              ,B.*
         From  @OperatorPrice A
         Cross Apply ( values (FName,Price)
                             ,('OC'+replace(FName,'Operator',''),OperatorID)
                     ) B (Item,Value)
        Union All
        Select ID=(select min(ID) From @OperatorPrice)
              ,B.*
         From ( Select Top 50 N=Row_Number() Over (Order By (Select NULL)) From master..spt_values n1 ) A
         Cross Apply ( values (concat('Operator',N),NULL)
                             ,(concat('OC',N),NULL)
                     ) B (Item,Value)
       ) AS SourceTable 
PIVOT  ( sum(Value) FOR Item IN (Operator1, OC1, Operator2, OC2,  Operator3, OC3, Operator4, OC4,  Operator5, OC5) ) AS PivotTable

Select * from  @TR

返回-通知操作员2

enter image description here