我正在使用数据集交易扫描,并尝试根据活跃日期和到期日期合并给定公司的 tranche_amount,因此我有给定期间的总和。
我使用了左连接
过程sql;
创建表loancumulative为 选择 a.startdate, sum(a.tranche_amount) 作为 tranche_amount 来自 Dealscan as a left join dealscan as b 在 a.startdate >= b.startdate 和 a.startdate < b.enddate group by a.startdate order by a.startdate;
退出;
但是还需要按 companyid 添加,因为我正在多个公司运行此操作。
有什么意见吗?
问候,
塞布
我在 tranche_amount 中添加了 perm_id 作为公司标识符,这里有些问题吗?
问候
根据 GROUP BY 语法,除了聚合字段之外,select 中包含的所有字段都必须出现在 group by 子句中。
例如:SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
就您而言,我认为您的查询应该是:
create table loancumulative as select a.startdate, a.perm_id, sum(a.tranche_amount) as tranche_amount from dealscan as a left join dealscan as b on a.startdate >= b.startdate and a.startdate < b.enddate group by a.startdate, a.perm_id order by a.startdate, a.perm_id;