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.
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 YourViewasselect (select count(*) from table1) as table1Count, (select count(*) from table2) as table2Count, (select count(*) from table3) as table3Count.. etc ..- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
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 |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
SQLCode
Posting Yak Master
143 Posts |
Posted - 2007-06-18 : 19:49:54
|
I get this error messageServer: Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'from'. |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
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 likecreate view YourViewasselect(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),Descfrom fTR Join dRSon fTR.RKey = dRS.RKeyGroup by Desc) as EActionsI get error Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. |
 |
|
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.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
|
|
|
|