MAX(rownumber) and count issue

  • declare @a table( keyid int,keyname varchar(50) )

    declare @B table( keyid int,keyname varchar(50) )

    insert into @a values (1,'10')

    insert into @a values (1,'10')

    insert into @a values (2,'20')

    insert into @a values (4,'40')

    insert into @a values (6,'60')

    insert into @a values (8,'80')

    insert into @a values (9,'90')

    insert into @B values (1,'100')

    insert into @B values (3,'300')

    insert into @B values (5,'500')

    insert into @B values (6,'600')

    insert into @B values (8,'800')

    insert into @B values (8,'801')

    -- Below query gives totalrows = 7 , if I remark count

    ;with rowcte as

    (

    select a.keyid as akeyid,a.keyname as akeyname , b.keyid as bkeyid ,b.keyname as bkeyname,

    row_number() over( order by a.keyid asc ) as rowasc

    from @a a

    left outer join @B b

    on a.keyid = b.keyid

    )

    select max(rowasc) totoalrows --,count(*) as totcount

    from rowcte

    -- Below query gives totalrows = 8 and totcount = 8 WHY ?

    ;with rowcte as

    (

    select a.keyid as akeyid,a.keyname as akeyname , b.keyid as bkeyid ,b.keyname as bkeyname,

    row_number() over( order by a.keyid asc ) as rowasc

    from @a a

    left outer join @B b

    on a.keyid = b.keyid

    )

    select max(rowasc) totoalrows ,count(*) as totcount

    from rowcte

  • The first query gives me 8 and the second gives me 8, 8?!

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • I forgot to mention . This is in SQL 2005 SP2

  • I got 7 and 8,8 on .. 2008(RTM) developers and enterprise edition ... I'm stumped too... can somebody please explain the reason behind this output ?

  • You should get 8 on both queries. A MAX() on a ROW_NUMBER() without PARTITION BY should give you the same values as COUNT(*).

    The reason is that you have keyids 1 and 8 repeated. You could check it with a simple query.

    SELECT *

    from @a a

    left outer join @B b

    on a.keyid = b.keyid

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Exactly... that is what I tried before posting reply... it clearly shows that it should give 8 as an output but I did not get it... am I missing something here ?

  • MAX() is working upon as per the definition of the base table.

    in your query u are using left join, if you try to use right join u will be even more surprised.

    Once you are done with the surprised run the below query

    declare @a table( keyid int,keyname varchar(50) )

    declare @B table( keyid int,keyname varchar(50) )

    declare @C table( akeyid int,akeyname varchar(50), bkeyid int, bkeyname varchar(50) ,rowasc int )

    insert into @a values (1,'10')

    insert into @a values (1,'10')

    insert into @a values (2,'20')

    insert into @a values (4,'40')

    insert into @a values (6,'60')

    insert into @a values (8,'80')

    insert into @a values (9,'90')

    insert into @B values (1,'100')

    insert into @B values (3,'300')

    insert into @B values (5,'500')

    insert into @B values (6,'600')

    insert into @B values (8,'800')

    insert into @B values (8,'801')

    INSERT into @C

    select a.keyid as akeyid,a.keyname as akeyname , b.keyid as bkeyid ,b.keyname as bkeyname,

    row_number() over( order by a.keyid asc ) as rowasc

    from @a a

    left join @B b on a.keyid = b.keyid

    SELECT MAX(rowasc), COUNT(akeyid)

    from @C

    SELECT MAX(rowasc)

    from @C

    Its a bug in 2005, 2008, 2008R2 .... but its running perfectly on the 2012. :w00t:

    here is the link http://support.microsoft.com/kb/2565683

    kindly upgrade your sql server to latest SPs if possible.

  • Thank you for that Service pack was applied and got my Inner peace...

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply