Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

sp_MSforeachtable group by or distinct count? Expand / Collapse
Author
Message
Posted Wednesday, February 12, 2014 9:50 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 10:52 AM
Points: 8, Visits: 37
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

Post #1540872
Posted Wednesday, February 12, 2014 10:43 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 5:30 PM
Points: 31,374, Visits: 15,843
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
Post #1540906
Posted Wednesday, February 12, 2014 10:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 10:52 AM
Points: 8, Visits: 37
Thats correct. I want a distinct count of every _key_out value in each table in the DB.
Post #1540910
Posted Wednesday, February 12, 2014 11:10 AM This worked for the OP Answer marked as solution


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 5:30 PM
Points: 31,374, Visits: 15,843
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
Post #1540919
Posted Wednesday, February 12, 2014 11:21 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 10:52 AM
Points: 8, Visits: 37
Durrr...yes!
Thanks Steve!
Post #1540925
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse