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 2000 Forums
 Transact-SQL (2000)
 View with all counts

Author  Topic 

SQLCode
Posting Yak Master

143 Posts

Posted - 2007-06-18 : 16:44:21
I need a view in the db with several different table counts. What is the best way to do it?

TIA

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-06-18 : 16:51:15
one way is something like this:

create view YourView
as
select
(select count(*) from table1) as table1Count,
(select count(*) from table2) as table2Count,
(select count(*) from table3) as table3Count



.. etc ..

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SQLCode
Posting Yak Master

143 Posts

Posted - 2007-06-18 : 17:04:42
How do you end this select statement? This is exactly what I need but I don't know how to end this and what goes in FROM clause.

Thanks
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-06-18 : 17:47:26
It's already ended; you don't need a FROM. Try it!

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SQLCode
Posting Yak Master

143 Posts

Posted - 2007-06-18 : 19:49:54
I get this error message

Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'from'.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-06-18 : 20:32:51
This might sound crazy, but maybe if you post your code it would be a little helpful ??

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SQLCode
Posting Yak Master

143 Posts

Posted - 2007-06-19 : 10:58:13
Dr. Cross Join,
Thanks for all your help. I was fat fingering and missing something in the long statement. It works just fine. However if I give certain conditions like

create view YourView
as

select
(select count(*) from table1 where a =1) as table1Count,
(select count(*) from table2 where b is null) as table2Count,
(select count(*) from table3 where bactive =1) as table3Count
(select count(ID),Desc
from fTR Join dRS
on fTR.RKey = dRS.RKey
Group by Desc) as EActions


I get error
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-06-19 : 11:08:28
Yes, that's right. What you are trying to write makes no sense ... how can it return multiple rows into one single row of results? Think logically about what you are asking SQL to do .... That last SELECT doesn't fit in with the others at all. You don't need to do any coding to fix this problem, you need to decide how this view should return its results first. Once you have your specification in place, THEN you can write the SELECT to do it.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -