您好我正在尝试从这样的查询创建一个表:(排序)
create table afiexdb.afiext as(
select a.afiaux, a.aficod, a.aficta, a.afidep, b.mb_nom_reg as AFIDEPDSC, a.afimun, d.mb_nom_ciu as AFIMUNDSC, a.afidirchk, a.afifching, a.afidptnac, c.mb_nom_reg as DPTNACDSC, a.afimunnac, e.mb_nom_ciu as MUNNACDSC,
cast(CASE WHEN a.afiestciv = 'S' THEN 'SOLTERO'
WHEN a.afiestciv = 'C' THEN 'CASADO'
WHEN a.afiestciv = 'D' THEN 'DIVORCIADO'
WHEN a.afiestciv = 'V' THEN 'VIUDO'
WHEN a.afiestciv = 'U' THEN 'UNION LIBRE'
END as char(11)) as AFIESTCIV,
a.afifchmue, a.afifchnac, a.afifchprm, a.afifchret, a.afihom, a.afinro, a.afiprof, a.afiserie, a.afisexo, a.afiults, rtrim(a.cli1ernomb)||' '||rtrim(a.cli2donomb)||' '||rtrim(a.cli1erapel)||' '|| rtrim(a.cli2doapel) as AFINOMBRE,
rtrim(a.clidirec1)||' '||rtrim(a.clidirec2)||' '||rtrim(a.clidirec3) as AFIDIR, a.cliidentid, f.afitel1, f.afitel2, f.afitel3,
a.estcod, g.estdsc, h.gracod, h.gradsc, a.grucod, i.grudsc, a.unicod, j.unidsc,
m.pplcod as PAGCOD, m.ppldsc as PAGDSC, afiedad(a.aficod, a.afihom, cast ('20170405' as char(8))) as EDADAFI,
ifnull(n.pricot,0) as PRICOT, ifnull(n.ultcot,0) as ULTCOT, n.totcot as TOTCOT, n.anioscot, cast(null as decimal) as RESLAB
from afiliadbf.afiliado a
left join lisamb30db.regdep b on b.mb_region = a.afidep
left join lisamb30db.regdep c on c.mb_region = a.afidptnac
left join lisamb30db.region d on a.afidep = d.mb_region and a.afimun = d.mb_ciudad
left join lisamb30db.region e on a.afidptnac = e.mb_region and a.afimunnac = e.mb_ciudad
left join afiliadbf.afiadinf f on a.aficod = f.aficod and a.afihom = f.afihom
left join afiliadbf.estado g on a.estcod = g.estcod
left join afiliadbf.grado h on a.gracod = h.gracod
left join afiliadbf.grupo i on a.grucod = i.grucod
left join afiliadbf.unidad j on a.unicod = j.unicod
left join prsipmdb2.pagunida l on a.unicod = l.pagunidad
left join afiliadbf.platip m on l.pagcod = m.pplcod
left join (select a.aficod, a.afihom, MIN(rtrim(a.cotanio)||'-'||case when a.cotmes<10 then '0'||a.cotmes else rtrim(a.cotmes)end cotaniomes) as pricot,
MAX(rtrim(a.cotanio)||'-'||case when a.cotmes<10 then '0'||a.cotmes else rtrim(a.cotmes) end cotaniomes) as ultcot, sum(a.cotmon) as totcot,
cast(count(a.cotanio)/12 as char(2))||' Años '||cast(count(a.cotanio)-(count(a.cotanio)/12)*12 as char(2))||' meses' as anioscot
from afiliadbf.cotiza a
group by a.aficod, a.afihom) n on n.aficod = a.aficod and n.afihom = a.afihom
) with no data
当我包含case子句列时,我得到无效符号。我知道DB2是强类型的,所以这就是为什么我把每个结果值都作为varchar转换为什么我也试图将整个case子句[cast(case()end作为char(10)]强制转换,并且肯定不能有&#39;如果没有某些错误会在某处出现,肯定会弹出。
有关如何根据案例子句列创建此列的任何建议吗?
答案 0 :(得分:0)
尝试像这样修改你的案子(你忘了&#39; ELSE&#39;进入你的案子):
CASE
WHEN a.afiestciv = 'S' THEN 'SOLTERO'
WHEN a.afiestciv = 'C' THEN 'CASADO'
WHEN a.afiestciv = 'D' THEN 'DIVORCIADO'
WHEN a.afiestciv = 'V' THEN 'VIUDO'
WHEN a.afiestciv = 'U' THEN 'UNION LIBRE'
ELSE cast(null as char(11))
END as AFIESTCIV