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
 General SQL Server Forums
 New to SQL Server Programming
 All columns via count

Author  Topic 

GRAYWOLF
Posting Yak Master

106 Posts

Posted - 2009-03-12 : 00:11:09
I am trying to pull up the whole table based on count(). I can do it where it pulls up what I am counting, but how do I get the rest of the info to show?

This will return the counted rows:
SELECT [server], [database], COUNT(*)
FROM dbo.FileGroups
group by [server], [database] having count(*) >2

How can I do it so that it shows every column? This gives an error "Incorrect syntax near the keyword 'SELECT'."

select * from GlobalDBAServer.dbo.FileGroups where SELECT [server], [database], COUNT(*)
FROM dbo.FileGroups
group by [server], [database] having count(*) > 2

Putting the second SELECT in () causes an error at ")".


really, I only want 2 more columns (logical & physical) but I don't want them as part of the count

---------------------------

Working until "the morning sun sets the midnight sky on fire"!

shaggy
Posting Yak Master

248 Posts

Posted - 2009-03-12 : 00:53:29
select "all_column_name" from GlobalDBAServer.dbo.FileGroups where SELECT [server], [database], COUNT(*)
FROM dbo.FileGroups
group by "all_column_name"
having count(*) > 2
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-12 : 02:11:20
quote:
Originally posted by shaggy

select "all_column_name" from GlobalDBAServer.dbo.FileGroups where SELECT [server], [database], COUNT(*)
FROM dbo.FileGroups
group by "all_column_name"
having count(*) > 2


Incorrect

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-12 : 02:12:21
quote:
Originally posted by GRAYWOLF

I am trying to pull up the whole table based on count(). I can do it where it pulls up what I am counting, but how do I get the rest of the info to show?

This will return the counted rows:
SELECT [server], [database], COUNT(*)
FROM dbo.FileGroups
group by [server], [database] having count(*) >2

How can I do it so that it shows every column? This gives an error "Incorrect syntax near the keyword 'SELECT'."

select * from GlobalDBAServer.dbo.FileGroups where SELECT [server], [database], COUNT(*)
FROM dbo.FileGroups
group by [server], [database] having count(*) > 2

Putting the second SELECT in () causes an error at ")".


really, I only want 2 more columns (logical & physical) but I don't want them as part of the count

---------------------------

Working until "the morning sun sets the midnight sky on fire"!


Try

SELECT f.* FROM GlobalDBAServer.dbo.FileGroups as f inner join 
(
SELECT [server], [database], COUNT(*)
FROM dbo.FileGroups
group by [server], [database] having count(*) > 2
) as t on f.[server]=t.[server] and f.[database]=t.[database]


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-12 : 13:10:50
quote:
Originally posted by madhivanan

quote:
Originally posted by GRAYWOLF

I am trying to pull up the whole table based on count(). I can do it where it pulls up what I am counting, but how do I get the rest of the info to show?

This will return the counted rows:
SELECT [server], [database], COUNT(*)
FROM dbo.FileGroups
group by [server], [database] having count(*) >2

How can I do it so that it shows every column? This gives an error "Incorrect syntax near the keyword 'SELECT'."

select * from GlobalDBAServer.dbo.FileGroups where SELECT [server], [database], COUNT(*)
FROM dbo.FileGroups
group by [server], [database] having count(*) > 2

Putting the second SELECT in () causes an error at ")".


really, I only want 2 more columns (logical & physical) but I don't want them as part of the count

---------------------------

Working until "the morning sun sets the midnight sky on fire"!


Try

SELECT f.*,t.DBCount FROM GlobalDBAServer.dbo.FileGroups as f inner join 
(
SELECT [server], [database], COUNT(*) AS DBCount
FROM dbo.FileGroups
group by [server], [database] having count(*) > 2
) as t on f.[server]=t.[server] and f.[database]=t.[database]


Madhivanan

Failing to plan is Planning to fail


small modification
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-12 : 13:12:27
if sql 2005 or later,just use


SELECT *,COUNT(1) OVER (PARTITION BY [server], [database]) AS DBCount
FROM GlobalDBAServer.dbo.FileGroups
Go to Top of Page

GRAYWOLF
Posting Yak Master

106 Posts

Posted - 2009-03-13 : 02:42:36
Thanks guys.

---------------------------

Working until "the morning sun sets the midnight sky on fire"!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-13 : 12:47:28
welcome
Go to Top of Page
   

- Advertisement -