向查询添加其他字段

时间:2015-08-06 19:40:18

标签: sql teradata

我试图将2个字段添加到现有查询中并遇到一些困难。

这是我要添加的2个字段:

paymt_mdia_proc_sys_cde作为结算, prim_acct_frst_six_dgt_nbr为First_Six

来自ft.fin_tran表

我应该加入ft.fin_tran_ref_id字段和ph.refid字段。

我无法让这个工作。我已尝试过加入和不同的地方,无法到达任何地方。我编写SQL并且与它斗争相对较新。此查询是由其他人设计的,我的任务是添加这两个字段。我认为这很简单,但事实并非如此。如果您对我可以尝试的内容有任何想法,我会很感激。谢谢你的帮助......

select mp.mop_desc as "Card Type", 
ph.CR_CARD_NBR  as "CC Last 4", 
ph.name as Card_Holder,
cast(ph.pymt_dt as date format 'mm/dd/yyyy') as PayDt,
ph.pymt_amt, 
s.grp_brn_id as CC_Charge_GPBR,
rb.rnt_agr_nbr as Rent_Agr,
rb.chkoutstn as Renting_GPBR,
rb.chkinstn as Close_GPBR,
cast(rb.co_tmsp as date format 'mm/dd/yyyy') as Check_Out,
cast(rb.ci_tmsp as date format 'mm/dd/yyyy') as Check_In,
rb.ecr_lgcy_resv_nbr as Ralph#,
rb.ecr_ticket_no as ECARS2#,
rb.dvr_frst_name as Driver_First,
rb.dvr_srnm as Driver_Last,
cast(ph.paph_fin_trans_ref_id as decimal(19,0)) as refid

from 
rfs_rv.pre_applied_pymts_hdr ph,
rfs.stns s,
rfs.mthd_of_pymts mp,
rfs_rv.pre_applied_pymts_det pd

left outer join ( select
ra.rnt_agr_nbr,
ra.ecr_ticket_no,
ra.ecre_rent_cntrct_nbr,
ra.ecr_lgcy_resv_nbr,
ra.co_tmsp,
ra.ci_tmsp,
sto.grp_brn_id as ChkOutStn,
sti.grp_brn_id as ChkInStn,
dr.dvr_srnm, 
dr.dvr_frst_name

from rfs_rv.rnt_agrs ra,
rfs.stns sto,
rfs.stns sti,
rfs_rv.dvr_rras dr

where ra.sta_stn_id_orig_co = sto.stn_id
and ra.sta_stn_id_orig_co = sti.stn_id
and ra.rnt_agr_nbr = dr.rdy_rnt_agr_nbr
and dr.main_dvr_flg = 'MR') rb
on pd.ticket_no = rb.ecr_ticket_no

where ph.pymt_stn_id = s.stn_id
and ph.mop_mop_cd = mp.mop_cd
and ph.pymt_id = pd.pap_pymt_id
and mp.mop_desc = ?
and ph.CR_CARD_NBR  = ?
and ph.pymt_dt  between cast(? as date format 'mm/dd/yyyy') and cast(? as date format 'mm/dd/yyyy')
and ph.cust_nbr = ?

1 个答案:

答案 0 :(得分:1)

混合隐式和显式联接并不是一个好主意,因此除了添加所需的新列之外,我还将所有联接更改为显式。

由于您没有提供任何测试数据,这可能有用,或者可能没有。

也许这就是你想要的?

q = (session.query(A.address, func.count(A.id).label("# people"))
    .group_by(A.address)
     ).all()