【问题标题】:SQL: Ranking Sections separately of a Rollup over multiple columnsSQL:在多列上分别对汇总的部分进行排名
【发布时间】:2015-07-21 21:26:53
【问题描述】:

我尝试对多列进行汇总,然后在汇总过程的每个阶段/部分应用排名。结果应如下所示:

| ColA | ColB | ColC | RankingCriteria | Ranking |
|------|------|------|-----------------|---------|
| -    | -    | -    | 10              | 1       |
|------|------|------|-----------------|---------|
| A    | -    | -    | 10              | 1       |
| B    | -    | -    | 8               | 2       |
|------|------|------|-----------------|---------|
| A    | a    | -    | 9               | 1       |
| A    | b    | -    | 7               | 2       |
| A    | c    | -    | 5               | 3       |
| A    | d    | -    | 2               | 4       |
|------|------|------|-----------------|---------|
| B    | a    | -    | 8               | 1       |
| B    | c    | -    | 7               | 2       |
| B    | b    | -    | 2               | 3       |
|------|------|------|-----------------|---------|
| A    | a    | x    | 7               | 1       |
| A    | a    | y    | 5               | 2       |
| A    | a    | z    | 4               | 3       |
|------|------|------|-----------------|---------|
| A    | b    | y    | 6               | 1       |
|------|------|------|-----------------|---------|
| A    | c    | w    | 10              | 1       |
| A    | c    | y    | 10              | 1       |
| A    | c    | z    | 8               | 2       |
| A    | c    | x    | 6               | 3       |
|------|------|------|-----------------|---------|
| A    | d    | y    | 4               | 1       |
|------|------|------|-----------------|---------|
| B    | a    | w    | 10              | 1       |
| B    | a    | x    | 8               | 2       |
|------|------|------|-----------------|---------|
| B    | b    | y    | 6               | 1       |
| B    | b    | z    | 5               | 2       |
| B    | b    | w    | 4               | 3       |
|------|------|------|-----------------|---------|
| B    | c    | x    | 6               | 1       |
|------|------|------|-----------------|---------|

如您所见,每个分组集都有自己的排名。

基本的汇总查询很简单,但排名让我头疼,我对如何实现这一点没有任何想法。

Select ColA, ColB, ColC, RankingCriteria
From table
Group By Rollup(ColA, ColB, ColC)

问题是我不能在 (Partition by ...) 上使用普通的 Rank(),因为没有我可以使用的分区来处理整个事情。

【问题讨论】:

  • 你使用的是 TSQL 还是 PLSQL ??

标签: sql rank rollup


【解决方案1】:

我认为这会产生你想要的:

SELECT r.*,
       row_number() over (partition by (case when colb is null and colc is null and cola is not null
                                             then 1 else 0 end),
                                       (case when colb is null and colc is null and cola is not null
                                             then NULL else A end),
                                       (case when colb is null and colc is null and cola is not null
                                             then NULL else B end)
                         order by RankingCriteria desc) as seqnum                           
FROM (Select ColA, ColB, ColC, RankingCriteria
      From table
      Group By Rollup(ColA, ColB, ColC)
     ) r;

我阅读逻辑的方式是 A 和 B 的分区适用于除第二组以外的所有人。这就是为什么这里使用三个 case 语句。

【讨论】:

  • 谢谢,您的回答帮助我完成了任务。我不得不稍微修改一下。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多