用于 IBM i (AS400) 的 DB2 - SQL
DB2 for IBM i (AS400) - SQL
两个 tables A(每日贷款交易 table)和 B(每日 eod 贷款余额 table)。
尝试在 DB2 AS400 中编写 select 语句以显示 selected 日期范围内的每日贷款交易、eod 贷款余额和期初贷款余额(前一天)。
下面的查询会给我每天的贷款交易和 eod 贷款余额。但是需要一些帮助来修改下面的查询以计算日期范围为 03/01/2015 到 03/31/2015 selected.[=12 的期初贷款余额(截至 02/28/2015 的余额) =]
select A.*, -- daily loan transactions
B.EOD_Loan_Balance
from A
inner join B
on A.date_id = B.date_id
and A.Loan_num = B.Loan_num
where a.date_id between 03/01/2015 to 03/31/2015
请注意,前一天应该是工作日,因此如果 02/28/2015 不是工作日,那么我们需要提取前一天的 eod 余额,这应该是工作日。
如有任何想法,我们将不胜感激。
谢谢!
这对你有用吗?
select A.*, -- daily loan transactions
B.EOD_Loan_Balance,
C.EOD_Loan_Balance,
from A
inner join B
on A.date_id = B.date_id
and A.Loan_num = B.Loan_num
LEFT OUTER join C
on A.Loan_num = C.Loan_num
where a.date_id between '2015-03-01' to '2015-03-31'
and c.date_id = CASE
WHEN DAYOFWEEK('2015-03-01') = 1 THEN '2015-03-01' - 2 days 'Sunday --> Friday
WHEN DAYOFWEEK('2015-03-01') = 2 THEN '2015-03-01' - 3 days 'Monday --> Friday
ELSE '2015-03-01' - 1 day ' Previous day
END
未经测试,因为我手边没有 DB2 实例。
编辑 已修改以允许周末,如评论中所述。
这是使用 "calender" 或 "dates" table...
可以轻松完成的众多任务之一
select D.*,
B.EOD_Loan_Balance,
E.EOD_Loan_Balance,
from MY_CALENDAR_TABLE C
join daily_trans D on c.date = d.date
join eod_bal B on c.date - 1 day= b.date and d.loan_num = b.loan_num
join eod_bal E on c.date = e.date and d.loan_num = e.loan_num
where c.date between 2015-03-01 and 2015-03-31
and c.is_business_day = 1;
--编辑--
回过头来看,我意识到它不太管用;从头开始的加入余额不会给你之前的工作日。
幸运的是,日历的灵活性 table 可以帮上忙。只需在您的日历 table.
中包含一个 PRIOR_BUSINESS_DAY
列
然后 SQL 变成:
select D.*,
B.EOD_Loan_Balance,
E.EOD_Loan_Balance,
from MY_CALENDAR_TABLE C
join daily_trans D on c.date = d.date
join eod_bal B on c.prior_buisiness_day = b.date
and d.loan_num = b.loan_num
join eod_bal E on c.date = e.date
and d.loan_num = e.loan_num
where c.date between 2015-03-01 and 2015-03-31
and c.is_business_day = 1;
两个 tables A(每日贷款交易 table)和 B(每日 eod 贷款余额 table)。
尝试在 DB2 AS400 中编写 select 语句以显示 selected 日期范围内的每日贷款交易、eod 贷款余额和期初贷款余额(前一天)。
下面的查询会给我每天的贷款交易和 eod 贷款余额。但是需要一些帮助来修改下面的查询以计算日期范围为 03/01/2015 到 03/31/2015 selected.[=12 的期初贷款余额(截至 02/28/2015 的余额) =]
select A.*, -- daily loan transactions
B.EOD_Loan_Balance
from A
inner join B
on A.date_id = B.date_id
and A.Loan_num = B.Loan_num
where a.date_id between 03/01/2015 to 03/31/2015
请注意,前一天应该是工作日,因此如果 02/28/2015 不是工作日,那么我们需要提取前一天的 eod 余额,这应该是工作日。
如有任何想法,我们将不胜感激。
谢谢!
这对你有用吗?
select A.*, -- daily loan transactions
B.EOD_Loan_Balance,
C.EOD_Loan_Balance,
from A
inner join B
on A.date_id = B.date_id
and A.Loan_num = B.Loan_num
LEFT OUTER join C
on A.Loan_num = C.Loan_num
where a.date_id between '2015-03-01' to '2015-03-31'
and c.date_id = CASE
WHEN DAYOFWEEK('2015-03-01') = 1 THEN '2015-03-01' - 2 days 'Sunday --> Friday
WHEN DAYOFWEEK('2015-03-01') = 2 THEN '2015-03-01' - 3 days 'Monday --> Friday
ELSE '2015-03-01' - 1 day ' Previous day
END
未经测试,因为我手边没有 DB2 实例。
编辑 已修改以允许周末,如评论中所述。
这是使用 "calender" 或 "dates" table...
可以轻松完成的众多任务之一select D.*,
B.EOD_Loan_Balance,
E.EOD_Loan_Balance,
from MY_CALENDAR_TABLE C
join daily_trans D on c.date = d.date
join eod_bal B on c.date - 1 day= b.date and d.loan_num = b.loan_num
join eod_bal E on c.date = e.date and d.loan_num = e.loan_num
where c.date between 2015-03-01 and 2015-03-31
and c.is_business_day = 1;
--编辑--
回过头来看,我意识到它不太管用;从头开始的加入余额不会给你之前的工作日。
幸运的是,日历的灵活性 table 可以帮上忙。只需在您的日历 table.
中包含一个PRIOR_BUSINESS_DAY
列
然后 SQL 变成:
select D.*,
B.EOD_Loan_Balance,
E.EOD_Loan_Balance,
from MY_CALENDAR_TABLE C
join daily_trans D on c.date = d.date
join eod_bal B on c.prior_buisiness_day = b.date
and d.loan_num = b.loan_num
join eod_bal E on c.date = e.date
and d.loan_num = e.loan_num
where c.date between 2015-03-01 and 2015-03-31
and c.is_business_day = 1;