所以,我有这个shell命令从select创建一个csv,但这并不重要。我遇到的问题是我选择的一个字段是一个整数字段作为日期字段yyyymmdd。所以我想要做的是选择字段/ 100来获取yyyymm并按照它和其他字段进行分组并将最后一个字段相加。
SELECT trim(a.F1)||chr(44)||trim(b.F2)||chr(44)||trim(F3)||chr(44)
||trim(F4)||chr(44)|||CAST(plandate/100 AS INT)||chr(44)
||trim(F5)||chr(44)||trim(F6)||chr(44)||trim(true_dem)
as "F1, F2, F3, F4, YYYYMM, F5, F6, MONTHLY_DEMAND"
from T1 a
join T2 b on a.ASDF = b.ASDF
WHERE PLANDATE > 20180400
所以,正如你现在所看到的那样,我抓住了特定日期之后的所有日期,但我没有按逻辑分组,原因是因为我可以按所有“F”分组“字段,但我不知道该归档的日期是什么,因为它是计算CAST(plandate / 100 AS INT)
我试过这样做
SELECT trim(a.f1)||chr(44)||trim(b.f2)||chr(44)||trim(f3)||chr(44)
||trim(f4)||chr(44)||CAST( plandate/100 AS INT)||chr(44)
||trim(f5)||chr(44)||trim(f6)||chr(44)||sum(true_dem)
as "f1, f2, f3, f4, YYYYMM, f5, f6, MONTHLY_DEMAND"
from t1 a
join t2 b on a.f1 = b.f1
WHERE PLANDATE > 20180400
group by a.f1, f2, f3, f4, f5, f6, (CAST( plandate/100 AS INT))
但这显然不对,因为我在SELECT列表中的[SQL0122]列PLANDATE或表达式无效。错误。几乎只是说这不好。如果可能的话,我真的想保留这个别名,以便我可以将其导出到带有列标题的csv
忽略fx和tx的东西我只想删除公司数据库内容的任何提示。如果重要的是我通过qsh
在I系列DB2上运行它答案 0 :(得分:2)
这也不会根据您的需要输出您的列标题:
SELECT
a.f1, b.f2, f3, f4, CAST( plandate/100 AS INT) as YYYYMM,
f5, f6, sum(true_dem) as MONTHLY_DEMAND
from t1 a join t2 b on a.f1 = b.f1
WHERE PLANDATE > 20180400
group by a.f1, f2, f3, f4, f5, f6, (CAST( plandate/100 AS INT))
如果您认为需要按照自己的方式进行操作,只需使用子选择来进行聚合:
SELECT
trim(f1)||chr(44)||trim(f2)||chr(44)||trim(f3)||chr(44)||trim(f4)||chr(44)
||CAST( plandate/100 AS INT)||chr(44)||trim(f5)||chr(44)||trim(f6)||chr(44)
||sum(true_dem)
as "f1, f2, f3, f4, YYYYMM, f5, f6, MONTHLY_DEMAND"
FROM (
SELECT
a.f1, b.f2, f3, f4, CAST( plandate/100 AS INT) as YYYYMM,
f5, f6, sum(true_dem) as MONTHLY_DEMAND
from t1 a join t2 b on a.f1 = b.f1
WHERE PLANDATE > 20180400
group by a.f1, f2, f3, f4, f5, f6, (CAST( plandate/100 AS INT))
) t