Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


MAX(rownumber) and count issue


MAX(rownumber) and count issue

Author
Message
M A Srinivas
M A Srinivas
SSC Veteran
SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)

Group: General Forum Members
Points: 234 Visits: 668
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
Abu Dina
Abu Dina
Right there with Babe
Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)

Group: General Forum Members
Points: 723 Visits: 3323
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

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
M A Srinivas
M A Srinivas
SSC Veteran
SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)

Group: General Forum Members
Points: 234 Visits: 668
I forgot to mention . This is in SQL 2005 SP2
SQLCJ
SQLCJ
Mr or Mrs. 500
Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)

Group: General Forum Members
Points: 539 Visits: 576
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 ?
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8544 Visits: 18133
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
SQLCJ
SQLCJ
Mr or Mrs. 500
Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)

Group: General Forum Members
Points: 539 Visits: 576
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 ?
twin.devil
twin.devil
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1376 Visits: 2597
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.
SQLCJ
SQLCJ
Mr or Mrs. 500
Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)

Group: General Forum Members
Points: 539 Visits: 576
Thank you for that Service pack was applied and got my Inner peace...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search