SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sp_MSforeachtable group by or distinct count?


sp_MSforeachtable group by or distinct count?

Author
Message
paulgill
paulgill
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 38
I need a simple grouped count of a value from each table in a database.
Trying to figure out how to modify the script below to include a group by clause or distinct count with subquery?

DECLARE @TableRowCounts TABLE ([TableName] VARCHAR(128), [RowCount] INT) ;
INSERT INTO @TableRowCounts ([TableName], [RowCount])
EXEC sp_MSforeachtable 'SELECT ''?'' [TableName], COUNT(*) [RowCount] FROM ? ' ;
SELECT [TableName], [RowCount]
FROM @TableRowCounts
ORDER BY [TableName]
GO

I want the COUNT(*) to be something like this...
COUNT(*) FROM (SELECT DISTINCT _key_out FROM ?)
OR COUNT(*) FROM (SELECT _key_out FROM ? GROUP BY _key_out)

This might not be possible using this sproc..everything I see on this topic never references actual fields in the many tables in the DB. Every table in the DB has the exact same fields (output from SSIS routine).
Thanks!
Paul
Steve Jones
Steve Jones
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: Administrators
Points: 64561 Visits: 19118
I'm not completely clear what you're doing. Do you mean you have a _key_out field in every table in the DB?

Are you trying to get

select count( distinct _key_out) from table1




Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
paulgill
paulgill
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 38
Thats correct. I want a distinct count of every _key_out value in each table in the DB.
Steve Jones
Steve Jones
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: Administrators
Points: 64561 Visits: 19118
Does this not work?

CREATE TABLE MyTable
(
_key_out VARCHAR(10)
, myval int
);
INSERT INTO mytable SELECT 'A', 1
INSERT INTO mytable SELECT 'A', 2
INSERT INTO mytable SELECT 'B', 1
INSERT INTO mytable SELECT 'C', 3

CREATE TABLE MyTable2
(
_key_out VARCHAR(10)
, myval int
);
INSERT INTO mytable2 SELECT 'A', 1
INSERT INTO mytable2 SELECT 'B', 2
INSERT INTO mytable2 SELECT 'B', 3
INSERT INTO mytable2 SELECT 'C', 3

EXEC sp_MSforeachtable 'select tablename=''?'', count(distinct _key_out) from ?'




Gives 3.

If I do this:

EXEC sp_MSforeachtable 'select tablename=''?'', count( _key_out) from ?'



I get 4.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
paulgill
paulgill
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 38
Durrr...yes!
Thanks Steve!
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