'Big Query SQL - i would like the last value in the results which is not null
I have the following script which I am running in big query.
It is supposed to give me the last non zero value when ordered by date. and it works in most cases.
for some reason I am now getting some null values and I would also like these null values to show the last non zero value but can not get it to work.
The script is as follows, if anyone can help I would greatly appreciate it.
Select
a.AccountNumber,
a.LoadDate,
a.TransactionDate,
a.PostDate,
a.VoidDate,
a.TransactionAmount,
a.TransactionCode,
a.NewCustomer
,b.InterestRate
from (
SELECT
Accno AS AccountNumber,
Load_Date AS LoadDate,
trandate AS TransactionDate,
postdate AS PostDate,
voiddate AS VoidDate,
bookamt AS TransactionAmount,
txncode AS TransactionCode,
b.CustomerCreateDate,
case when b.CustomerCreateDate is null then null
when DATE_DIFF( cast (trandate as date), cast(CustomerCreateDate as date), day) <= 90 then 'New'
ELSE 'Existing' end NewCustomer,
DATE_DIFF( cast (trandate as date), cast(CustomerCreateDate as date), day) as Days
FROM `utb-dw-prod.UTB1.vw_Txns` a
left outer join (
select * from
(
select *,
ROW_NUMBER() OVER ( PARTITION BY AccountNumber ORDER BY CustomerCreateDate ASC ) AS rn
from `utb-dw-prod.Reporting.DE003_Deposit_Customer`
)a
where a.rn = 1 ) b on a.Accno = Cast( b.AccountNumber as Int64)
WHERE
txflags <> 7168
and
txflags <> 3072
and
txflags <> 27648
and
txflags <> 31744
and
txflags <> 67140608
and
txflags <> 2048
and
(txflags < 10000000000 or (txflags = 1125899906867209 and bookamt < 0) or (txflags = 1125899906842633 and bookamt < 0))
and
trandate >="2021-01-01"
and
(voiddate = "1900-01-01" or voiddate < trandate)
and
txflags & 16 != 16
and
txflags & 2048 != 2048
) a
--left outer join `utb-dw-prod.BankCoreViews.vw_Deposit` b on a.AccountNumber = cast( b.AccountNumber as int) and Cast (a.TransactionDate as Date) = b.snapshotdate
left outer join (
select t.AccountNumber
,t.SnapshotDate
,case when t.interestRate = 0 then q1.lastValue else t.interestRate end as interestRate,
from `utb-dw-prod.BankCoreViews.vw_Deposit` t
left outer join
(
select
q.AccountNumber
,q.snapshotDate
,q.lastValue
from
(
select t1.* , t2.interestRate lastValue , RANK() over ( partition by t1.AccountNumber, t1.snapshotDate order by t2.snapshotDate desc ) as RANK
from `utb-dw-prod.BankCoreViews.vw_Deposit` t1
left join `utb-dw-prod.BankCoreViews.vw_Deposit` t2 on cast(t1.AccountNumber as int) = cast(t2.AccountNumber as int)--and t1.snapshotDate >= t2.snapshotDate and t1.interestRate=0 and t2.interestRate <>0
where t2.interestRate is not null
and t1.interestRate=0 and t2.interestRate <>0
and t1.snapshotDate >= t2.snapshotDate
) as q where q.rank = 1
) as q1 on q1.AccountNumber = t.AccountNumber and q1.snapshotDate = t.snapshotDate
) as b on a.AccountNumber = cast( b.AccountNumber as int) and Cast (a.TransactionDate as Date) = b.snapshotdate
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
