如何在 tsql 中进行排列(基于集合)

发布于 2024-11-14 12:55:23 字数 1299 浏览 1 评论 0原文

我有以下输入输出

PlayerID    MatchPlayed RunsMade
--------    ----------- --------
1           10              200
2           5               100
3           8               24
4           30              50

将为

Combined Players    Combined Match Played   Combined runs Made  
----------------    ---------------------   ------------------  
1                   10                      200         
1,2                 15                      300         
1,3                 18                      224
1,4                 40                      250
1,2,3               23                      324
1,2,4               45                      350
1,3,4               48                      274
1,2,3,4             53                      374
2                   5                       100
2,3                 13                      124
2,4                 35                      150
2,3,4               43                      174
3                   8                       24
3,4                 38                      74
4                   30                      50

组合比赛列是这些球员的比赛列值的总和。例如,对于组合比赛 1,2,组合比赛值是 10 + 5 = 15。

同样,组合得分是各个球员的得分列的总和。例如,对于同一示例,组合运行 MAde 列为 200 +100 =300。

谢谢

I have the below input

PlayerID    MatchPlayed RunsMade
--------    ----------- --------
1           10              200
2           5               100
3           8               24
4           30              50

The output will be

Combined Players    Combined Match Played   Combined runs Made  
----------------    ---------------------   ------------------  
1                   10                      200         
1,2                 15                      300         
1,3                 18                      224
1,4                 40                      250
1,2,3               23                      324
1,2,4               45                      350
1,3,4               48                      274
1,2,3,4             53                      374
2                   5                       100
2,3                 13                      124
2,4                 35                      150
2,3,4               43                      174
3                   8                       24
3,4                 38                      74
4                   30                      50

The Combined Match Played column is the sum of the values of Match Played column of those players. e.g. for Combined Played 1,2 the Combined Match Played value is 10 + 5 = 15.

similarly, Combined Runs Made is the sum of the Runs MAde column of the individual players. e.g. for the same example, the Combined Runs MAde column is 200 +100 =300.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

渔村楼浪 2024-11-21 12:55:23

设置:

create table Input(PlayerId int, MatchPlayed int, RunsMade int)

insert Input
  select 1, 10, 200
  union all select 2, 5, 100
  union all select 3, 8, 24
  union all select 4, 30, 50

查询:

with cte(Combined, PlayerId, MatchPlayed, RunsMade)
as
(
    select cast(PlayerId as varchar(500)), PlayerId, MatchPlayed, RunsMade
    from Input
    union all
    select cast(cte.Combined + ',' + cast(inp.PlayerId as varchar) as varchar(500)), inp.PlayerId, inp.MatchPlayed + cte.MatchPlayed, inp.RunsMade + cte.RunsMade
    from cte
        join Input inp on
            cte.PlayerId < inp.PlayerId
)
select Combined, MatchPlayed, RunsMade
from cte
order by Combined

Setup:

create table Input(PlayerId int, MatchPlayed int, RunsMade int)

insert Input
  select 1, 10, 200
  union all select 2, 5, 100
  union all select 3, 8, 24
  union all select 4, 30, 50

Query:

with cte(Combined, PlayerId, MatchPlayed, RunsMade)
as
(
    select cast(PlayerId as varchar(500)), PlayerId, MatchPlayed, RunsMade
    from Input
    union all
    select cast(cte.Combined + ',' + cast(inp.PlayerId as varchar) as varchar(500)), inp.PlayerId, inp.MatchPlayed + cte.MatchPlayed, inp.RunsMade + cte.RunsMade
    from cte
        join Input inp on
            cte.PlayerId < inp.PlayerId
)
select Combined, MatchPlayed, RunsMade
from cte
order by Combined
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文