如何在sql中获取最新的一年

时间:2016-03-31 00:36:06

标签: sql sql-server sql-server-2008 tsql

sql = "select scn,name,imp_flg,cg_desc from"
                    +" (select  vesvoy.scn, vessel.name,'I' as imp_flg,cargotype.description as cg_desc  from vesvoy"
                    +" join vessel on vesvoy.vessel_id = vessel.id "
                    +" join vesvoy_mf on vesvoy.id = vesvoy_mf.id_1"
                    +" join mf_bl on vesvoy_mf.id_2 = mf_bl.id_1"
                    +" join bl on mf_bl.id_2 = bl.id "
                    +" join blitem on blitem.master_id = bl.id"
                    +" join cargotype on blitem.cg_type_id = cargotype.id"
                    + " where bl.inv_flg<>'1' and year(vesvoy.exp_arr_dt_tm) in year(CURRENT_TIMESTAMP) , year(CURRENT_TIMESTAMP)-1"
                    +" union"
                    +" select   vesvoy.scn, vessel.name,'E' as imp_flg,cargotype.description as cg_desc"
                    +" from vesvoy"
                    +" join vessel on vesvoy.vessel_id = vessel.id "
                    +" join vesvoy_so on vesvoy.id = vesvoy_so.id_1"
                    +" join so_sso on vesvoy_so.id_2 = so_sso.id_1"
                    +" join sso on so_sso.id_2 = sso.id "
                    +" join ssoitem on ssoitem.master_id = sso.id"
                    +" join cargotype on ssoitem.cg_type_id = cargotype.id"
                    + " where sso.inv_flg<>'1' and  year(vesvoy.exp_arr_dt_tm) in year(CURRENT_TIMESTAMP) , year(CURRENT_TIMESTAMP)-1) as a ";

我做了上面的查询。

说错误

  

发现了意外的令牌“年(CURRENT_TIMESTAMP),年”   “oy.exp_arr_dt_tm”in。预期的令牌可能包括.SQLSTATE = 42601“

2 个答案:

答案 0 :(得分:2)

您需要定义IN

的括号
sql = "select scn,name,imp_flg,cg_desc from"
                +" (select  vesvoy.scn, vessel.name,'I' as imp_flg,cargotype.description as cg_desc  from vesvoy"
                +" join vessel on vesvoy.vessel_id = vessel.id "
                +" join vesvoy_mf on vesvoy.id = vesvoy_mf.id_1"
                +" join mf_bl on vesvoy_mf.id_2 = mf_bl.id_1"
                +" join bl on mf_bl.id_2 = bl.id "
                +" join blitem on blitem.master_id = bl.id"
                +" join cargotype on blitem.cg_type_id = cargotype.id"
                + " where bl.inv_flg<>'1' and year(vesvoy.exp_arr_dt_tm) in (year(CURRENT_TIMESTAMP) , year(CURRENT_TIMESTAMP)-1)"
                +" union"
                +" select   vesvoy.scn, vessel.name,'E' as imp_flg,cargotype.description as cg_desc"
                +" from vesvoy"
                +" join vessel on vesvoy.vessel_id = vessel.id "
                +" join vesvoy_so on vesvoy.id = vesvoy_so.id_1"
                +" join so_sso on vesvoy_so.id_2 = so_sso.id_1"
                +" join sso on so_sso.id_2 = sso.id "
                +" join ssoitem on ssoitem.master_id = sso.id"
                +" join cargotype on ssoitem.cg_type_id = cargotype.id"
                + " where sso.inv_flg<>'1' and  year(vesvoy.exp_arr_dt_tm) in (year(CURRENT_TIMESTAMP) , year(CURRENT_TIMESTAMP)-1) as a"

答案 1 :(得分:2)

在SQL Server中,当前日期时间由GetDate()调用。此外,使用IN时,列表需要括在括号中,因此它看起来像

and year(vesvoy.exp_arr_dt_tm) in (year(GETDATE()) , year(GETDATE())-1)