枢轴上的一列并显示另一列的关联值

发布于 2025-02-05 05:43:46 字数 1441 浏览 3 评论 0原文

我有一个带有这样的数据的

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

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

发布评论

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

评论(1

与他有关 2025-02-12 05:43:49

我认为这不是一个关键的情况,而是排名。

您只想为每条资金提供一条线,最高日期的成本,而在此之前的最后两个则是,对吗?

如果是这样,您可以使用CTE对基金进行排名并生成成本的其他列,然后过滤它以仅带上排名上的第一个值。

这样的:

CREATE TABLE [dbo].[MyTable](
    [Fund] [NCHAR](10) NOT NULL,
    [EffectiveDate] [DATE] NOT NULL,
    [SomeOtherColumns] [NVARCHAR](50) NULL,
    [Cost] [INT] NOT NULL
) ON [PRIMARY]

insert into MyTable(Fund  ,  EffectiveDate , SomeOtherColumns , COST) values
('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)
;with rankingCte as (
    select *,
    LAG(Cost) over(order by EffectiveDate ASC) [COST DAY BEFORE],
    LAG(Cost,2) over(order by EffectiveDate ASC) [COST DAY BEFORE THAT],
    rank() over(partition by Fund order by EffectiveDate desc) as dateRanking
    from MyTable
)
select Fund, EffectiveDate, SomeOtherColumns, Cost, [COST DAY BEFORE],
[COST DAY BEFORE THAT]
from rankingCte
where dateRanking=1
资金成本前一天的某些HotherColumns成本之前
在F1232022-04-25成本345340360
F4562022-04-28其他一些其他基金110220460

您可以看到它在此工作 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:

CREATE TABLE [dbo].[MyTable](
    [Fund] [NCHAR](10) NOT NULL,
    [EffectiveDate] [DATE] NOT NULL,
    [SomeOtherColumns] [NVARCHAR](50) NULL,
    [Cost] [INT] NOT NULL
) ON [PRIMARY]

insert into MyTable(Fund  ,  EffectiveDate , SomeOtherColumns , COST) values
('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)
;with rankingCte as (
    select *,
    LAG(Cost) over(order by EffectiveDate ASC) [COST DAY BEFORE],
    LAG(Cost,2) over(order by EffectiveDate ASC) [COST DAY BEFORE THAT],
    rank() over(partition by Fund order by EffectiveDate desc) as dateRanking
    from MyTable
)
select Fund, EffectiveDate, SomeOtherColumns, Cost, [COST DAY BEFORE],
[COST DAY BEFORE THAT]
from rankingCte
where dateRanking=1
FundEffectiveDateSomeOtherColumnsCostCOST DAY BEFORECOST DAY BEFORE THAT
F1232022-04-25something345340360
F4562022-04-28some other fund110220460

You can see it working on this DB Fiddle.

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