SQL Server/DB2: Same query returns different results? -
summary
i'm working on project have query against underlying database engine last changes of records represents users accesses.
each user may, , not mandatory to, have children accounts. children accounts stored within same data table reference parent through id_pusr
table field. when account primary, id_pusr null
each time acces changed, new record created in database users table, last update date (dt_updt
).
data sample
please consider following:
create table users ( id_users int // primary key , ln_user varchar(128) , fn_user varchar(128) , cd_user varchar(128) , dt_updt datetime , id_pusr int // foreign key users.id_users. ) id_users cd_user ln_user fn_user dt_updt id_pusr ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 808 t_pei00 ln_user_00 fn_user_00 2011-01-01-00.00.00.000000 null 809 t_pei00 ln_user_00 fn_user_00 2010-01-01-00.00.00.000000 null 810 t_pei00 ln_user_00 fn_user_00 2009-01-01-00.00.00.000000 null 811 t_pei00 ln_user_00 fn_user_00 2008-01-01-00.00.00.000000 null 812 t_pei00 ln_user_00 fn_user_00 2007-01-01-00.00.00.000000 null 813 t_pei00a ln_user_00 fn_user_00 2011-01-01-00.00.00.000000 808 814 t_pei00a ln_user_00 fn_user_00 2010-01-01-00.00.00.000000 809 815 t_pei00a ln_user_00 fn_user_00 2009-01-01-00.00.00.000000 810 816 t_pei00a ln_user_00 fn_user_00 2008-01-01-00.00.00.000000 811 817 t_pei00a ln_user_00 fn_user_00 2007-01-01-00.00.00.000000 812 818 t_maw00 ln_user_01 fn_user_01 2010-01-01-00.00.00.000000 null 819 t_maw00 ln_user_01 fn_user_01 2009-01-01-00.00.00.000000 null 820 t_maw00 ln_user_01 fn_user_01 2008-01-01-00.00.00.000000 null 821 t_maw00 ln_user_01 fn_user_01 2007-01-01-00.00.00.000000 null 822 t_vem08 ln_user_08 fn_user_08 2009-01-01-00.00.00.000000 null 823 t_vem08 ln_user_08 fn_user_08 2008-01-01-00.00.00.000000 null 824 t_vem08 ln_user_08 fn_user_08 2007-01-01-00.00.00.000000 null 825 t_lac99 ln_user_99 fn_user_99 2008-01-01-00.00.00.000000 null 826 t_lac99 ln_user_99 fn_user_99 2007-01-01-00.00.00.000000 null
i double-checked data table content within both database servers , can certify identical records.
sql/db2 query
this query compatible both sql server , db2 database engines:
with upg ( select id_users , cd_user , ln_user , fn_user , dt_updt , id_pusr , row_number() on (partition cd_user order cd_user desc) rownum users ) select id_users , cd_user , ln_user , fn_user , dt_updt , id_pusr , rownum upg rownum = 1 order cd_user
different results!
despite fact i'm running same exact identical query against above-mentioned rdbms, obtain different results follows:
ibm db2
id_users cd_user ln_user fn_user dt_updt id_pusr rownum ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 826 t_lac99 ln_user_99 fn_user_99 2007-01-01-00.00.00.000000 null 1 821 t_maw00 ln_user_01 fn_user_01 2007-01-01-00.00.00.000000 null 1 808 t_pei00 ln_user_00 fn_user_00 2011-01-01-00.00.00.000000 null 1 814 t_pei00a ln_user_00 fn_user_00 2010-01-01-00.00.00.000000 809 1 822 t_vem08 ln_user_08 fn_user_08 2009-01-01-00.00.00.000000 null 1
these results appears until can see difference between 2 database engines. notice date values in dt_updt
field.
sql server
id_users cd_user ln_user fn_user dt_updt id_pusr rownum ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 727 t_lac99 ln_user_99 fn_user_99 2008-01-01 00:00:00.000 null 1 720 t_maw00 ln_user_01 fn_user_01 2010-01-01 00:00:00.000 null 1 710 t_pei00 ln_user_00 fn_user_00 2011-01-01 00:00:00.000 null 1 715 t_pei00a ln_user_00 fn_user_00 2011-01-01 00:00:00.000 710 1 724 t_vem08 ln_user_08 fn_user_08 2009-01-01 00:00:00.000 null 1
these results here in sql server 1 shall come in db2. represent "good" datum. id_users
, id's, matters dates.
questions
- how can same query returns different results using 2 sql ansi capable engines?
- is datum don't seem see?
- how
with...as ()
interpreted db2 shall differ sql server?
nota benne: simple select * users order cd_user
reveals same data.
your query undeterministic ties.
row_number() on (partition cd_user order cd_user desc) rownum
doesn't define particular row_numbering within each partition. if want both rdbms's return same results order unique there no ties , deterministic results.
Comments
Post a Comment