如何使一个值取代所有其他值,以便无论组合如何始终选择特定值?

How to make a value supersede all other values so that particular value is always selected no matter the combination?

我有一个数据库,其中的文件用特定的数字编码,这样它们就意味着它们在我们的流程中所处的位置。

例如

+-------------+------------+
| Fileno      | Code       |
+-------------+------------+
|     r0099070|         41 |
|     r0099070|         48 |
|     r0099070|        950 |
|     r0099070|         16 |
+-------------+------------+

因此,为此目的,如果有一个组合但其中有 41 个,我希望我的查询总是 select 41 个组合。

问题是有时 41 不在那里,如果是这种情况,我只需提取这些代码并选择日期最近的代码。

我已经拥有的 sql 代码非常庞大,是这样的:

SELECT d.[FILENO]  
        ,CODE as 'Diary Code'
        ,[date]
        ,case when (d.queue = 'QBK' or ((db.bkcy_chapter <> '' or db.bkcy_chapter <> null) and  (db.bkcy_dsmis_date = '' or db.bkcy_dsmis_date is null))) then 'Bankruptcy'
              when m.source = 'HISTORY\HISTORY' or ( [code] in ('900') and (d.queue like 'QC%' or d.queue = 'QCPSOS')) then 'Closed'
              when [code] in ('41') then 'Prep suit'--or [code] in ('41', '950') or [code] in ('41', '48') then 'Prep suit' 
              when [code] in ('900') and d.queue like 'QH%' then 'Hold'           
              when [CODE] IN ('34', '40', '31', '25', '38','37','32','33','30') or (d.queue like 'QVENDOR' or d.queue like 'QPOE%' or d.queue like 'QADDY&') THEN 'Skip'                                  
              when [code] in ('51' ) then 'Unifund 2nd /3rd Letter'
              when [code] in ('44') then 'Prep Letter of Intent'
              when [code] in ('490') then 'Collection Agency'
              when fu.xavcode = 1 then '41 & 45 awaiting affidavit'                                     

              when [code] in ('47', '46' ) then 'Post Prep'
              when [code] like ('%[0-9]%') and d.queue like 'QATTY%' then 'Atty Review'
              when [code] in ('186','624') then 'Reviewed/To Be Filed (IL)' 
              when [code] in ('627', '632', '288') then 'Sent for Filing'                                                              
              when [code] in ('188', '177', '172', '173','478','477','479','464') or (d.QUEUE like 'QCOL%' or d.QUEUE like 'QPAY%')  then 'payments'                                  
              when [code] in ('661') then 'Prep Wage Garn' else 'Other' end as 'diary description'       

            --when {code} in ('661','652','677','678','309','527') "Levy or Garnishment In Progress"
            --when {code} in ('663','664','653') then "Active Levy or Garnishment"

            --when judgment is not empty and skip diary code then "Post Judgment Asset Search"
            --when judgment is empty but suit is not empty and skip diary code then "Pre Judgment Skip Search"
       , case when l.LETTER_DESC is null then j.LETTER_DESC end as 'LETTER_DESC'
         ,[comment] 
       ,d.[QUEUE]

       ,row_number() over(partition by d.fileno order by DATE desc) as 'row check'  
       into #diarycode
  FROM monthly.dbo.diaryint d
       LEFT JOIN LETTERS L ON L.LLCODE4 = 'X' + cast(d.code_alpha as varchar)
       left join letters j on j.[diary_code] = code_alpha
       left join #fuck f on f.fileno = d.fileno and f.XavCode = 1
       left join #fuckU fu on fu.fileno = f.fileno and fu.xavCode = f.xavcode
       left join [Monthly].[dbo].debtor db on db.fileno = d.fileno 
       left join monthly.dbo.master m on m.fileno = d.fileno

这甚至可以做到吗?上面我有 when [code] in ('41') then 'Prep Suit' 但是有没有办法做一个 case/when 如果它在代码 41 and/or 41 存在于任何可能的组合中总是让 41 取代它并选择 41 的代码?

您只需在添加“41”过滤器的位置添加另一个 ROW_NUMBER() 分区函数:

SELECT *
into #diarycode
FROM
    ( SELECT d.[FILENO]  
            ,CODE as 'Diary Code'
            ,[date]
            .......
            ,row_number() over(partition by d.fileno order by DATE desc) as 'row check'
            -- Filter condition(41 or latest date)
            ,row_number() over(partition by d.fileno order by (CASE WHEN CODE = '41' THEN 0 ELSE 1 END) ASC, DATE desc) as [row filter] 

        FROM monthly.dbo.diaryint d
        ....
    ) AS a
WHERE [row filter] = 1 -- Filter on 41 or latest date

由于您不能在 where 子句中直接使用 window 函数的值,因此您需要将查询包装在另一个子句中。

注意: 为简洁起见,我已将您的部分查询替换为“...”