如何进行多个枢轴
我承认我不是很擅长pivot
(没有双关语),但设法获得了我所需要的一半,但我被困在第二部分。 因此,这是我的表格和一些数据,以及现在的数据外观的屏幕截图:
CREATE TABLE #temptable ([PriceName] VARCHAR(50), [PriceName_Change] VARCHAR(50), [PriceVal] DECIMAL(28, 2), [PriceVal_Change] DECIMAL(28, 2), [Portfolio] NVARCHAR(200), [benchmark] NVARCHAR(200) , [EffectiveDate] DATE);
INSERT INTO #temptable ([PriceName], [PriceName_Change], [PriceVal], [PriceVal_Change], [Portfolio], [benchmark], [EffectiveDate])
VALUES
('OilPrice', 'OilPrice_CHANGE', 1607.00, 3.61, N'PORT45', N'SP500',N'2022-06-02T00:00:00')
,('OilPrice', 'OilPrice_CHANGE', 1607.00, 3.61, N'PORT45', N'SP500',N'2022-06-01T00:00:00')
,('OilPrice', 'OilPrice_CHANGE', 607.00, 12.61, N'PORT45', N'SP500',N'2022-05-31T00:00:00')
so oilprice
与列中的值相关,这就是我要枢转的两件事。 我设法为其中一个写了它,但我无法为他们两个写。 这是我所做的:
SELECT
portfolio
, EffectiveDate
, [OilPrice]
FROM
(
SELECT
SM.portfolio
, SM.PriceVal
, SM.PriceName
, SM.EffectiveDate
FROM #temptable SM
) AS tbl
PIVOT
(
SUM(PriceVal)
FOR PriceName IN ([OilPrice])
) AS pvt;
以下是我期望的结果:
OilPrice | OilPrice_Change | Portfolio | Benchmark | EffectiveDate |
1607.00 | 3.61 | PORT45 | SP500 | 2022-06-02 |
1607.00 | 3.61 | PORT45 | SP500 | 2022-06-01 |
607.00 | 12.61 | PORT45 | SP500 | 2022-05-31 |
I admit I am not very good at PIVOT
(no pun intended) but managed to get half way to what I need but I am stuck at the second part.
So here is my table and some data and also a screen shot of how the data looks right now:
CREATE TABLE #temptable ([PriceName] VARCHAR(50), [PriceName_Change] VARCHAR(50), [PriceVal] DECIMAL(28, 2), [PriceVal_Change] DECIMAL(28, 2), [Portfolio] NVARCHAR(200), [benchmark] NVARCHAR(200) , [EffectiveDate] DATE);
INSERT INTO #temptable ([PriceName], [PriceName_Change], [PriceVal], [PriceVal_Change], [Portfolio], [benchmark], [EffectiveDate])
VALUES
('OilPrice', 'OilPrice_CHANGE', 1607.00, 3.61, N'PORT45', N'SP500',N'2022-06-02T00:00:00')
,('OilPrice', 'OilPrice_CHANGE', 1607.00, 3.61, N'PORT45', N'SP500',N'2022-06-01T00:00:00')
,('OilPrice', 'OilPrice_CHANGE', 607.00, 12.61, N'PORT45', N'SP500',N'2022-05-31T00:00:00')
So OilPrice
is associated with values from column PriceVal
and PriceName_Change
is associated with values from column PriceVal_Change
and that's the two things I want to PIVOT on.
I managed to write it for one of them but I can't get to write it for both of them.
Here is what I did:
SELECT
portfolio
, EffectiveDate
, [OilPrice]
FROM
(
SELECT
SM.portfolio
, SM.PriceVal
, SM.PriceName
, SM.EffectiveDate
FROM #temptable SM
) AS tbl
PIVOT
(
SUM(PriceVal)
FOR PriceName IN ([OilPrice])
) AS pvt;
Below is how I am expecting the result to show:
OilPrice | OilPrice_Change | Portfolio | Benchmark | EffectiveDate |
1607.00 | 3.61 | PORT45 | SP500 | 2022-06-02 |
1607.00 | 3.61 | PORT45 | SP500 | 2022-06-01 |
607.00 | 12.61 | PORT45 | SP500 | 2022-05-31 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
目前尚不清楚您要如何汇总(总和一个
更改
值是很有意义的?)。还不清楚如果还有其他类型的更改,您想要什么。但是您可以使用这样的有条件聚集
It's unclear exactly how you want to aggregate (does it make sense to sum a
Change
value?). Also unclear is what result you want if there are other types of changes.But you can use conditional aggregation like this
db<>fiddle
您可以做一个不分散的,然后旋转该对象。这只是一个猜测,因为我不知道您想要最终输出的外观,但也许...
You could do an UNPIVOT and then PIVOT that object. This is just a guess, because I don't know what you want the final output to look like, but maybe...
您的预期结果是表的最后5列,因此不需要任何旋转。
您要做的就是过滤表,以便您仅获得
priceName ='oilprice'< /code>和
topriceName_change ='OilPrice_Change'
并将列重命名PriceVal
oilprice
和priceval_change
>
> oilprice_change
:请参阅
Your expected results are the last 5 columns of the table, so there is no need for any pivoting.
All you have to do is filter the table so that you get only the rows with
PriceName = 'OilPrice'
andPriceName_Change = 'OilPrice_CHANGE'
and rename the columnsPriceVal
toOilPrice
andPriceVal_Change
toOilPrice_Change
:See the demo.