February 10, 2014 at 2:53 am
declare @a table( keyid int,keyname varchar(50) )
declare @b-2 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-2 values (1,'100')
insert into @b-2 values (3,'300')
insert into @b-2 values (5,'500')
insert into @b-2 values (6,'600')
insert into @b-2 values (8,'800')
insert into @b-2 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-2 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-2 b
on a.keyid = b.keyid
)
select max(rowasc) totoalrows ,count(*) as totcount
from rowcte
February 10, 2014 at 3:13 am
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
February 10, 2014 at 3:22 am
I forgot to mention . This is in SQL 2005 SP2
February 10, 2014 at 4:26 am
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 ?
February 10, 2014 at 7:08 am
February 10, 2014 at 11:17 pm
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 ?
February 11, 2014 at 1:26 am
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-2 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-2 values (1,'100')
insert into @b-2 values (3,'300')
insert into @b-2 values (5,'500')
insert into @b-2 values (6,'600')
insert into @b-2 values (8,'800')
insert into @b-2 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-2 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.
February 11, 2014 at 5:56 am
Thank you for that Service pack was applied and got my Inner peace...
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply