Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Server can't count?

Author  Topic 

jriff
Starting Member

2 Posts

Posted - 2009-06-09 : 05:45:14
Hi all!

I have a little problem with SQL server 2008. When I run this query:

SELECT * FROM (SELECT TOP 1000 * FROM (SELECT distinct TOP 1000 activation_data.*, (select name from subscriptions where id=COALESCE(spec_subscription_id, subscription_id)) as correct_subscription, chains.name as chain_name, activation_types.name as activation_type_name FROM [activation_data] inner join notifications on activation_data.id=notifications.activation_data_id inner join dealers on activation_data.pos=dealers.pos inner join chains on dealers.chain_id=chains.id inner join activation_types on activation_data.activation_type_id=activation_types.id WHERE (((exists (select id from notifications where notifications.activation_data_id=activation_data.id and check_code='Activation found < 4 months earlier, same number, individual' and resolved_at is null)) AND (activation_data.deleted=0)) AND (activation_date>='2009-03-30 00:00:00' and activation_date<='2009-04-05 00:00:00')) ORDER BY activation_date DESC) AS tmp1 ORDER BY [activation_date] ASC) AS tmp2 ORDER BY [activation_date] desc


I get 460 rows returned. If I run this query:

SELECT count(*) as TotalRows from (SELECT TOP 1000000000 activation_data.*, (select name from subscriptions where id=COALESCE(spec_subscription_id, subscription_id)) as correct_subscription, chains.name as chain_name, activation_types.name as activation_type_name FROM [activation_data] inner join notifications on activation_data.id=notifications.activation_data_id inner join dealers on activation_data.pos=dealers.pos inner join chains on dealers.chain_id=chains.id inner join activation_types on activation_data.activation_type_id=activation_types.id WHERE (((exists (select id from notifications where notifications.activation_data_id=activation_data.id and check_code='Activation found < 4 months earlier, same number, individual' and resolved_at is null)) AND (activation_data.deleted=0)) AND (activation_date>='2009-03-30 00:00:00' and activation_date<='2009-04-05 00:00:00')) ORDER BY activation_date DESC) tally


then TotalRows is 1131... Can someone explain that?

Regards,

Jacob

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-09 : 06:43:08
Yes, this is simple.

In your first query you have DISTINCT keyword present, which eliminate all duplicate records.
You don't have that in your second query.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

jriff
Starting Member

2 Posts

Posted - 2009-06-09 : 06:49:04
Damn! I didn't see that one. Thanks for your help - one can really over look the most obvious things

quote:
Originally posted by Peso

Yes, this is simple.

In your first query you have DISTINCT keyword present, which eliminate all duplicate records.
You don't have that in your second query.


E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-09 : 06:50:45
You're welcome!



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -