如何进行多个枢轴

发布于 2025-02-10 10:38:12 字数 1669 浏览 2 评论 0原文

我承认我不是很擅长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:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄意 2025-02-17 10:38:13

目前尚不清楚您要如何汇总(总和一个更改值是很有意义的?)。还不清楚如果还有其他类型的更改,您想要什么。

但是您可以使用这样的有条件聚集

SELECT
  OilPrice = SUM(CASE WHEN PriceName = 'OilPrice' THEN PriceVal END), 
  OilPrice_CHANGE = SUM(CASE WHEN PriceName = 'OilPrice' THEN PriceVal_Change END),
  t.Portfolio,
  t.benchmark,
  t.EffectiveDate
FROM #temptable t
GROUP BY
  t.Portfolio,
  t.benchmark,
  t.EffectiveDate;

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

SELECT
  OilPrice = SUM(CASE WHEN PriceName = 'OilPrice' THEN PriceVal END), 
  OilPrice_CHANGE = SUM(CASE WHEN PriceName = 'OilPrice' THEN PriceVal_Change END),
  t.Portfolio,
  t.benchmark,
  t.EffectiveDate
FROM #temptable t
GROUP BY
  t.Portfolio,
  t.benchmark,
  t.EffectiveDate;

db<>fiddle

·深蓝 2025-02-17 10:38:13

您可以做一个不分散的,然后旋转该对象。这只是一个猜测,因为我不知道您想要最终输出的外观,但也许...

SELECT *
FROM
    (select PriceName, Portfolio, benchmark, 
       CONVERT(VARCHAR(20), PriceVal) AS [PriceVal],
       CONVERT(VARCHAR(20), PriceName_Change) AS [PriceName_Change]from temptable) 
as PVT
UNPIVOT
(
  Item for Items in (PriceVal, PriceName_Change)
) AS unpvt
GO

“在此处输入映像”

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...

SELECT *
FROM
    (select PriceName, Portfolio, benchmark, 
       CONVERT(VARCHAR(20), PriceVal) AS [PriceVal],
       CONVERT(VARCHAR(20), PriceName_Change) AS [PriceName_Change]from temptable) 
as PVT
UNPIVOT
(
  Item for Items in (PriceVal, PriceName_Change)
) AS unpvt
GO

enter image description here

旧人九事 2025-02-17 10:38:12

您的预期结果是表的最后5列,因此不需要任何旋转。

您要做的就是过滤表,以便您仅获得priceName ='oilprice'< /code>和priceName_change ='OilPrice_Change'并将列重命名PriceVal oilpricepriceval_change to > > oilprice_change

SELECT PriceVal AS OilPrice, 
       PriceVal_Change AS OilPrice_Change, 
       Portfolio, benchmark, EffectiveDate
FROM #temptable
WHERE PriceName = 'OilPrice' AND PriceName_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' and PriceName_Change = 'OilPrice_CHANGE' and rename the columns PriceVal to OilPrice and PriceVal_Change to OilPrice_Change:

SELECT PriceVal AS OilPrice, 
       PriceVal_Change AS OilPrice_Change, 
       Portfolio, benchmark, EffectiveDate
FROM #temptable
WHERE PriceName = 'OilPrice' AND PriceName_Change = 'OilPrice_CHANGE';

See the demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文