delphi联合查询的SQL语句,如何在查询后的数据前加入自动序列

delphi联合查询的SQL语句,如何在查询后的数据前加入自动序列

with WMG_DM.Gnl_ADOQuery do  begin    close;      sql.Clear;       sql.Add('(select pod,workid as 工作证号,zgname as 姓名,csrq as 出生日期,datediff(year,csrq,getdate()) as 年龄,      ');   sql.Add('cjgzsj as 参加工作时间,datediff(year,cjgzsj,getdate())+1 as 工龄, (datediff(year,cjgzsj,getdate())+1)*5  as 工龄工资 from jbxxtab where datediff(year,cjgzsj,getdate())<=10    )  union all     ');   sql.Add('(select pod,workid as 工作证号,zgname as 姓名,csrq as 出生日期,datediff(year,csrq,getdate()) as 年龄,      ');   sql.Add('cjgzsj as 参加工作时间,datediff(year,cjgzsj,getdate())+1 as 工龄, (datediff(year,cjgzsj,getdate())-9)*6+50  as 工龄工资 from jbxxtab where datediff(year,cjgzsj,getdate())>10 and datediff(year,cjgzsj,getdate())<=20   )  union all   ');   sql.Add('(select pod,workid as 工作证号,zgname as 姓名,csrq as 出生日期,datediff(year,csrq,getdate()) as 年龄,      ');   sql.Add('cjgzsj as 参加工作时间,datediff(year,cjgzsj,getdate())+1 as 工龄, (datediff(year,cjgzsj,getdate())-19)*8+110  as 工龄工资 from jbxxtab where datediff(year,cjgzsj,getdate())>20 and datediff(year,cjgzsj,getdate())<=30  )  union all   ');   sql.Add('(select pod,workid as 工作证号,zgname as 姓名,csrq as 出生日期,datediff(year,csrq,getdate()) as 年龄,      ');   sql.Add('cjgzsj as 参加工作时间,datediff(year,cjgzsj,getdate())+1 as 工龄, (datediff(year,cjgzsj,getdate())-29)*10+190  as 工龄工资 from jbxxtab where datediff(year,cjgzsj,getdate())>30   )  order by id  ');                     open;               end;

数据库是SQLserver2005
用ROW_NUMBER() OVER (order by id) as RecNo好像不能加吧?有 union all
请大神们指导下,这种的怎么在查询结果后加自动序号呀?

select q.*,row_number() over(order by q.id) as RecNo
from (...) q
--把你上面的查询放到括号中,如果你原来查询中没加上字段id,加上这个字段就可以了。

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有