枢轴上的一列并显示另一列的关联值
我有一个带有这样的数据的表
:
Fund | EffectiveDate | SomeOtherColumns | COST
F123 | 2022-04-25 | something | 345
F123 | 2022-04-24 | fdsdfdff | 340
F123 | 2022-04-20 | hi | 360
F123 | 2022-04-17 | hello | 810
F456 | 2022-04-28 | some other fund | 110
F456 | 2022-04-26 | some other fund | 220
F456 | 2022-04-25 | some other fund | 460
F456 | 2022-04-15 | some other fund | 215
示例定义:
CREATE TABLE [dbo].[MyTable](
[Fund] [NCHAR](10) NOT NULL,
[EffectiveDate] [DATE] NOT NULL,
[SomeOtherColumns] [NVARCHAR](50) NULL,
[Cost] [INT] NOT NULL
) ON [PRIMARY]
GO
对于我的查询,每个基金(在这种情况下为F123和F456)我只想每天只有每一行。我想将每个基金的最新 最新 ,将其与copt
值值 天。因此,例如,结果将是:
Fund | EffectiveDate | SomeOtherColumns | COST | COST DAY BEFORE | COST DAY BEFORE THAT |
F123 | 2022-04-25 | something | 345 | 340 | 360 |
F456 | 2022-04-28 | some other fund | 110 | 220 | 460 |
我知道我应该使用Pivot
,但我什至不知道如何解决它。
我的查询甚至没有编译!
SELECT * FROM (
SELECT TOP(3)
FUND, EffectiveDate, Cost
FROM MyTable
ORDER BY EffectiveDate DESC
) Results
PIVOT (
SUM(Cost)
FOR EffectiveDate
IN (
FUND, EffectiveDate, Cost
)
) AS PivotTable
I have a table
with data like this:
Fund | EffectiveDate | SomeOtherColumns | COST
F123 | 2022-04-25 | something | 345
F123 | 2022-04-24 | fdsdfdff | 340
F123 | 2022-04-20 | hi | 360
F123 | 2022-04-17 | hello | 810
F456 | 2022-04-28 | some other fund | 110
F456 | 2022-04-26 | some other fund | 220
F456 | 2022-04-25 | some other fund | 460
F456 | 2022-04-15 | some other fund | 215
Sample definition like this:
CREATE TABLE [dbo].[MyTable](
[Fund] [NCHAR](10) NOT NULL,
[EffectiveDate] [DATE] NOT NULL,
[SomeOtherColumns] [NVARCHAR](50) NULL,
[Cost] [INT] NOT NULL
) ON [PRIMARY]
GO
For each fund(F123 and F456 in this case) in my result query I want to have only one row for each fund for each day. I want to take the top three latest EffectiveDate
of each fund, pivot it ALONG with the COST
value on that day. So for example the result will be:
Fund | EffectiveDate | SomeOtherColumns | COST | COST DAY BEFORE | COST DAY BEFORE THAT |
F123 | 2022-04-25 | something | 345 | 340 | 360 |
F456 | 2022-04-28 | some other fund | 110 | 220 | 460 |
I know I should use a PIVOT
but I can't even figure out how to solve it.
My query doesn't even compile!
SELECT * FROM (
SELECT TOP(3)
FUND, EffectiveDate, Cost
FROM MyTable
ORDER BY EffectiveDate DESC
) Results
PIVOT (
SUM(Cost)
FOR EffectiveDate
IN (
FUND, EffectiveDate, Cost
)
) AS PivotTable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这不是一个关键的情况,而是排名。
您只想为每条资金提供一条线,最高日期的成本,而在此之前的最后两个则是,对吗?
如果是这样,您可以使用CTE对基金进行排名并生成成本的其他列,然后过滤它以仅带上排名上的第一个值。
这样的:
您可以看到它在此工作 nofollow noreferrer“> db小提琴。
I don't think that's a pivot situation, but a ranking one.
You want for every fund only one line, with the cost for the top date, and the last two before that, right?
If so, you can do it using a CTE to rank the data partitioning by the fund and generate the additional columns for the costs, and then filter it to bring only the first values on the ranking.
Like this:
You can see it working on this DB Fiddle.