Sybase中获取与上月关联的字段

Obtaining a field associated with the previous month in Sybase

在下面的查询中,我试图从 table loans 中获取相同 Uniq_ID 和同一年但前一个月的 BALANCE 值,并且然后将其与当前行 BALANCE 值进行比较。 fdate 列看起来像 3/1/2019。我尝试按以下方式进行操作,但出现错误 "Sybase Database Error: Feature, reference containing a scalar value subquery (defined at line 13) inside a conditional expression (CASE, COALESCE, ARGN, NULLIF, or IF), is not supported"。我怎样才能尝试以其他方式做到这一点?

SELECT h.UNIQUEID, f.num
(select BALANCE from loans b
    where b.UNIQ_ID = h.UNIQ_ID
    and year(b.FDATE) = year(h.FDATE) 
    and MONTH(b.fdate) = MONTH(h.fdate) - 1) AS prev_bal,
(case when prev_bal > 0 and prev_bal >= BALANCE then 1 else 0 end) as  flag
FROM loans h, perform f
where f.uniq_id = h.uniq_id 

编辑:

数据如下所示:

fdate           UNIQUEID              NUM    BALANCE   
3/1/2019       LNSAR17224-00453434    1      16254.1  
4/1/2019       LNSAR17224-00453434    1      15643.2    

我想添加如下所示的列 prev_bal

fdate           UNIQUEID              NUM    BALANCE   prev_bal    
3/1/2019       LNSAR17224-00453434    1      16254.1    {null}     
4/1/2019       LNSAR17224-00453434    1      15643.2    16254.1 

预期输出:

UNIQUEID              NUM   prev_bal    flag
LNSAR17224-00453434    1    {null}       0
LNSAR17224-00453434    1    16254.1      1
SELECT fdate, UNIQUEID, PNUM, BALANCE, lag(BALANCE) over (order by fdate)                
from your_table
order by fdate

假设:

  • 数据库配置为不区分大小写(否则 OP 可以根据需要编辑以将所有列引用设置为 upper() 或 lower())
  • perform table 有 2 个名为 uniq_idUNIQUEID 的列(否则 OP 可以解决拼写错误)
  • by previous month OP 引用了当前月份之前的最后一个条目(即,不一定是前一个 calendar 月份);所以这意味着我们可以使用 max() 函数来查找 previous month [替代方法是在前一个 calendar 月份查找匹配项并将其视为外部连接如果它不存在;当然可以编码,但会等待 OP 输入]
  • OP 已显示一个输出记录的 prev_balanceNULL 值;我认为这意味着可能没有任何 previous month 数据(即,我们需要考虑使用外部连接)

(我相信)解决上述问题的一个查询:

select h.UNIQUEID,
       f.num,
       d1.prev_balance,
       isnull(d1.flag,0) as flag

from   loans h
join   perform f
on     f.uniq_id = h.uniq_id

left
join   (select h2.UNIQ_ID,                                       -- data associated with previous date
               h2.BALANCE as prev_balance,
               case when h2.BALANCE > 0 and h2.BALANCE >= h.BALANCE 
                    then 1 
                    else 0
               end as flag
        from   loans h2
        where  h2.UNIQ_ID = h.UNIQ_ID
        and    h2.FDATE = (select max(h3.FDATE) as prev_fdate    -- find previous date
                           from   loans h3
                           where  h3.UNIQ_ID = h2.UNIQ_ID
                           and    h3.FDATE   < h2.FDATE)
       ) as d1

on     h.uniq_id = d1.uniq_id

或者去掉一级子查询:

select h.UNIQUEID,
       f.num,
       h2.BALANCE as prev_balance,
       isnull(case when h2.BALANCE > 0 and h2.BALANCE >= h.BALANCE
                   then 1
                   else 0 
              end,0) as flag

from   loans h
join   perform f
on     h.uniq_id = f.uniq_id

left
join   loans h2
on     h.uniq_id = h2.uniq_id
and    h2.FDATE = (select max(h3.FDATE) as prev_fdate   -- find previous date
                   from   loans h3
                   where  h3.UNIQ_ID = h2.UNIQ_ID
                   and    h3.FDATE   < h2.FDATE)

注意:没有 table DDL 或示例数据,因此目前无法测试上述内容的 syntax/data 准确性 ...