1

我在 SQL 中有一个(希望是)小问题,我需要从新列的 3 列中查询单个值。

这是我的桌子的样子:

Name of the issuer     #ofratings    Agency1    Agency2    Agency3
AAA                        3            22         22         24         
BBB                        3            22         24         28 
CCC                        2            16         12         NULL  
DDD                        2            16         16         NULL 
EEE                        1            NULL        3         NULL    

现在我的问题...如果我不清楚,请随时告诉我。

我需要创建一个新列,在该列中我将按照这些规则恢复生成的代理值

  1. 如果我有 3 个机构报价但没有一个是相同的,那么我想要中间一个(示例中的发行人 BBB)。
  2. 如果我有 3 个代理报价并且 2 个相同,那么我希望在新列中使用那个(示例中为发行人 AAA。
  3. 如果我有 2 个代理报价并且它们是相同的,那么我希望将其中一个带回我的新专栏(示例中为发行人 DDD)。
  4. 如果我有 2 个代理报价并且两者都不同,那么我想要 2 个之间的最小值(示例中的发行人 CCC)。
  5. 如果我只有 1 份代理报价,那么我希望带回一份(示例中为发行人 EEE)。

请注意,我可以很容易地让发行人 AAA 中的代理 1 和 3 相同,依此类推。

我搜索了从 3 列中获取最小值并得到了 从几列中选择最小值的最佳方法是什么?

问题是我尝试了给出的 CASE WHEN THEN 示例,但是 SQL 允许在 case 参数中最多嵌套 10 个级别,而我的规则让我破坏了:S

任何帮助将不胜感激。

谢谢

为@Fabien 编辑解决方案

这是有问题的 Select 函数 请注意,在我的示例中,标准普尔是代理 1,穆迪是代理 2,DBRS 是代理 3

   Select       
no.issuer_cd, 
count(distinct(no.Rater_CD)) as 'norating',
SP.rating_rank as 'S&P',
MO.rating_rank as 'Moodys',
DB.rating_rank as 'DBRS'

from        csm_issuer_rating r
left join 
(select     
    no.issuer_cd,
    no.rater_cd 

    from csm_issuer_rating no
    where no.rater_cd in ('M_SP_BOND','M_DBRS_BOND','M_MOODY_BOND')) as no
on          r.issuer_cd = no.issuer_cd
left join 
csm_issuer i on i.issuer_cd = no.issuer_cd

Left join
csm_rater_rating rr on rr.rater_cd = no.rater_cd

left join
(   select  
r.issuer_cd,                            
r.rating_cd,
rr.rating_rank

from        csm_issuer_rating r
left join csm_rater_rating rr on rr.rating_cd = r.rating_cd             

where       rr.rater_cd = 'M_SP_BOND' and r.rater_cd = 'M_SP_BOND'


            ) as SP
on          i.issuer_cd = sp.issuer_cd

left join
            (   select  
r.issuer_cd,                            
r.rating_cd,
rr.rating_rank

from        csm_issuer_rating r
left join csm_rater_rating rr on rr.rating_cd = r.rating_cd             

where       rr.rater_cd = 'M_MOODY_BOND' and r.rater_cd = 'M_MOODY_BOND'


            ) as MO
on          i.issuer_cd = mo.issuer_cd

left join
            (   select  
r.issuer_cd,                            
r.rating_cd,
rr.rating_rank

from        csm_issuer_rating r
left join csm_rater_rating rr on rr.rating_cd = r.rating_cd             

where       rr.rater_cd = 'M_DBRS_BOND' and r.rater_cd = 'M_DBRS_BOND'


            ) as DB
on          i.issuer_cd = db.issuer_cd

group by 
no.issuer_cd,
SP.rating_rank,
MO.rating_rank,
DB.rating_rank)
4

2 回答 2

1

SQL小提琴

MS SQL Server 2008 架构设置

CREATE TABLE Table1
    (issuer_cd varchar(3), norating int, 
     "S&P" varchar(4), Moodys varchar(4), DBRS varchar(4))
;

INSERT INTO Table1
    (issuer_cd, norating, "S&P", Moodys, DBRS)
VALUES
    ('AAA', 3, '22', '22', '24'),
    ('BBB', 3, '22', '24', '28'),
    ('111', 3, '26', '24', '28'),
    ('CCC', 2, '16', '12', NULL),
    ('DDD', 2, '16', '16', NULL),
    ('EEE', 1, NULL, '3', NULL)
;

查询 1

SELECT issuer_cd, 
       CASE when ("S&P" is not null and
                 Moodys is not null) or
                 ("S&P" is not null and
                 DBRS is not null) or
                 (Moodys is not null and
                 DBRS is not null)  then (
                   CASE when "S&P" between isnull(Moodys,-1) and 
                                             DBRS then "S&P"
                        when Moodys between isnull("S&P",-1) and 
                                             DBRS then Moodys
                        when DBRS between isnull("S&P",-1) and 
                                             Moodys then DBRS
                        when "S&P" between isnull(DBRS,-1) and 
                                             Moodys then "S&P"
                        when Moodys between isnull(DBRS,-1) and 
                                             "S&P" then Moodys
                        when DBRS between isnull(Moodys,-1) and 
                                             "S&P" then DBRS
                        when "S&P" = Moodys or 
                             Moodys = DBRS then Moodys
                        when "S&P" = DBRS then DBRS
                   END
                 )
            when ("S&P" is null and
                 Moodys is null) then DBRS
            when (Moodys is null and
                 DBRS is null) then "S&P"
            when ("S&P" is null and
                 DBRS is null) then Moodys
       END
FROM Table1

结果

| ISSUER_CD | COLUMN_1 |
|-----------|----------|
|       AAA |       22 |
|       BBB |       24 |
|       111 |       26 |
|       CCC |       12 |
|       DDD |       16 |
|       EEE |        3 |

编辑 :

也许你可以尝试这样的事情:

IF OBJECT_ID(N'tempdb..#mytemp', N'U') IS NOT NULL 
DROP TABLE #mytemp;

/* Your SELECT */
   Select       
no.issuer_cd, 
count(distinct(no.Rater_CD)) as 'norating',
SP.rating_rank as 'S&P',
MO.rating_rank as 'Moodys',
DB.rating_rank as 'DBRS'
INTO #mytemp
FROM ...

/* My SELECT */
SELECT ....
FROM #mytemp;
于 2013-09-04T15:34:43.227 回答
-1

将 tablename 替换为您的表名:

select *, (case when agency1<>agency2 and agency1<>agency3 then agency2 
           when agency1=agency2 and agency1<>agency3 then agency3
           when agency1<>agency2 and agency3=agency2 then agency1
           when agency3<>agency2 and agency3=agency1 then agency2
           when agency1 is null and agency2=agency3 then agency2
           when agency2 is null and agency1=agency3 then agency1
           when agency3 is null and agency1=agency2 then agency1
           when agency1 is null and agency2<agency3 then agency2
           when agency1 is null and agency2>agency3 then agency3
           when agency2 is null and agency1<agency3 then agency1
           when agency2 is null and agency1>agency3 then agency3
           when agency3 is null and agency1<agency2 then agency1
           when agency3 is null and agency1>agency2 then agency2
           when agency1 is null and agency2 is null then agency3
           when agency2 is null and agency3 is null then agency1
           when agency1 is null and agency3 is null then agency2
           end)columnname
from tablename
于 2013-09-04T15:23:07.407 回答