4

来自 Ranges Table 命名范围的示例数据如下所示:

+-----------------+-------------------+----------+----------+
| SectionCategory |     RangeName     | LowerEnd | UpperEnd |
+-----------------+-------------------+----------+----------+
| Sanction        | 0-7 days          |        0 |        7 |
| Sanction        | 8-15 days         |        8 |       15 |
| Sanction        | More than 15 days |       16 |    99999 |
| Disbursal       | 0-7 days          |        0 |        7 |
| Disbursal       | 8-15 days         |        8 |       15 |
| Disbursal       | More than 15 days |       16 |    99999 |
+-----------------+-------------------+----------+----------+

延迟表中的示例数据如下所示:

+-----------+---------------+-----------------+
| Loan No.  | SanctionDelay | Disbursal Delay |
+-----------+---------------+-----------------+
|       247 |             8 |              35 |
|       661 |            18 |              37 |
|      1235 |            12 |               6 |
|      1235 |             8 |              15 |
|      1241 |            28 |               9 |
|      1241 |            11 |               9 |
|      1283 |            22 |              20 |
|      1283 |            28 |              41 |
|      1523 |             1 |              27 |
|      1523 |             6 |              28 |
+-----------+---------------+-----------------+

所需的输出如下所示:

+-----------+-------------------+-------+
|  Section  |       Range       | Count |
+-----------+-------------------+-------+
| Sanction  | 0-7 days          |     2 |
| Sanction  | 8-15 days         |     4 |
| Sanction  | More than 15 days |     4 |
| Disbursal | 0-7 days          |     1 |
| Disbursal | 8-15 days         |     3 |
| Disbursal | More than 15 days |     6 |
+-----------+-------------------+-------+

目前编写了两个单独的查询,并使用 UNION 来整理输出。

从可维护性的角度来看,是否可以在单个查询中执行此操作?(对于 Ranges 表中的 Sanction,应使用 Delays Table 中的 SanctionDelay 列,而对于 Disbursal,应使用 DisbursalDelay 列。)之所以需要,是因为预计贷款生命周期的阶段数会增加,并且越来越多的 UNION需要整理输出。

4

2 回答 2

3

它可以用 a 来完成CROSS JOIN,不确定它的效率如何。

样本数据:

declare @Ranges table (SectionCategory varchar(10) not null,RangeName varchar(20) not null,LowerEnd int not null,UpperEnd int not null)
insert into @Ranges (SectionCategory,RangeName,LowerEnd,UpperEnd) values
('Sanction','0-7 days',0,7),
('Sanction','8-15 days',8,15),
('Sanction','More than 15 days',16,99999),
('Disbursal','0-7 days',0,7),
('Disbursal','8-15 days',8,15),
('Disbursal','More than 15 days',16,99999)

declare @Delays table (LoanNo int not null,SanctionDelay int not null,DisbursalDelay int not null)
insert into @Delays (LoanNo,SanctionDelay,DisbursalDelay) values
( 247, 8,35),
( 661,18,37),
(1235,12, 6),
(1235, 8,15),
(1241,28, 9),
(1241,11, 9),
(1283,22,20),
(1283,28,41),
(1523, 1,27),
(1523, 6,28)

查询(必须与样本数据在同一批次中运行):

select
    r.SectionCategory,
    r.RangeName,
    SUM(CASE
        WHEN r.SectionCategory='Sanction' and d.SanctionDelay BETWEEN r.LowerEnd and r.UpperEnd then 1
        WHEN r.SectionCategory='Disbursal' and d.DisbursalDelay BETWEEN r.LowerEnd and r.UpperEnd then 1
        else 0 end) as Cnt
from @Ranges r
    cross join
    @Delays d
group by
    r.SectionCategory,
    r.RangeName
order by SectionCategory,RangeName

结果:

SectionCategory RangeName            Cnt
--------------- -------------------- -----------
Disbursal       0-7 days             1
Disbursal       8-15 days            3
Disbursal       More than 15 days    6
Sanction        0-7 days             2
Sanction        8-15 days            4
Sanction        More than 15 days    4

从可维护性的角度来看,延迟表中最好有一个延迟列和一个指定延迟类型的附加列。目前,感觉像是某种形式的属性拆分——在 Ranges 表中,类型表示为列值(SanctionDisbursal等),但在延迟表中,相同的“类型”在表元中表示 -数据,根据不同的列名。

您说“预计贷款生命周期的阶段数会增加”,我希望这种交叉(将属性表示为某些表中的数据和其他表中的元数据)会增加编写体面查询的痛苦.

于 2013-06-07T07:16:28.397 回答
2

尝试这个

SELECT 
  SectionCategory
 ,RangeName
 ,CASE 
  WHEN R.SectionCategory='Sanction' THEN
   (SELECT COUNT(1) FROM Delays D WHERE D.Sanction_Delay BETWEEN R.LowerEnd AND R.UpperEnd) 
  WHEN R.SectionCategory='Disbursal' THEN
   (SELECT COUNT(1) FROM Delays D WHERE D.[Disbursal Delay] BETWEEN R.LowerEnd AND R.UpperEnd) 
END as cnt
FROM Ranges R

这是SQLFiddle演示

于 2013-06-07T07:21:03.943 回答