June 9, 2010 at 5:06 pm
I have this query which I am using for an asp report app I made:
SELECT somecolumn
COUNT(somecolumn) AS NumOccurrences
FROM sometable
GROUP BY somecolumn
HAVING ( COUNT(somecolumn) >= 0 )
This returns results something like this:
somecolumn, numoccurrences
value1, 5
value2, 4
value3, 2
I need to do two things:
1- I need to be able to add up all the numoccurrences and return that (in this case, the value would be 11)
2- I need to be able to just count up the number of rows returned (in this case, the value would be 3)
So this might be a silly question, but can I do a select statement for count or sum on my existing select statement? Is there a better way to do that?
June 9, 2010 at 5:09 pm
Looks like the query is working, is there something that it is not doing that you would like it to do?
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
June 9, 2010 at 5:15 pm
Sorry, I accidentally posted before I was finished. First time in the forum
June 9, 2010 at 5:23 pm
Now I'm done with my question (I edited my post). Thanks!
June 9, 2010 at 5:23 pm
Are you truly on SQL 2000?
Here is a solution for SQL 2005 (sorry just noticed the forum group after having worked the solution - will post a sql 2000 solution in a bit)
--Your query
SELECT Table_Name
,COUNT(Table_Name) AS NumOccurrences
FROM INFORMATION_SCHEMA.Columns
GROUP BY Table_Name
HAVING ( COUNT(Table_Name) >= 0 );
--query to meet requirements
With CounterCTE as (
SELECT Table_Name
,COUNT(Table_Name) AS NumOccurrences
FROM INFORMATION_SCHEMA.Columns
GROUP BY Table_Name
HAVING ( COUNT(Table_Name) >= 0 )
)
Select Count(Table_Name) as ColCount, Sum(NumOccurrences) as TotalOccurrences
From CounterCte
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
June 9, 2010 at 5:26 pm
Here is a SQL 2000 version
Declare @CounterTable Table (Table_Name Varchar(50), NumOccurrences int)
Insert into @CounterTable (Table_Name, NumOccurrences)
SELECT Table_Name
,COUNT(Table_Name) AS NumOccurrences
FROM INFORMATION_SCHEMA.Columns
GROUP BY Table_Name
HAVING ( COUNT(Table_Name) >= 0 )
Select Count(Table_Name) as ColCount, Sum(NumOccurrences) as TotalOccurrences
From @CounterTable
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
June 9, 2010 at 5:28 pm
hmm. no I'm on 2005 too. Sorry. I guess i didn't notice that either!
June 9, 2010 at 5:30 pm
audracmckay (6/9/2010)
hmm. no I'm on 2005 too. Sorry. I guess i didn't notice that either!
That's all good. Solutions for both types are posted. The SQL 2000 version will work on 2005 as well.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
June 9, 2010 at 5:32 pm
That worked beautifully!! Thanks so much!
June 9, 2010 at 5:42 pm
You're welcome.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
June 9, 2010 at 9:22 pm
CirquedeSQLeil (6/9/2010)
Here is a SQL 2000 version
Declare @CounterTable Table (Table_Name Varchar(50), NumOccurrences int)
Insert into @CounterTable (Table_Name, NumOccurrences)
SELECT Table_Name
,COUNT(Table_Name) AS NumOccurrences
FROM INFORMATION_SCHEMA.Columns
GROUP BY Table_Name
HAVING ( COUNT(Table_Name) >= 0 )
Select Count(Table_Name) as ColCount, Sum(NumOccurrences) as TotalOccurrences
From @CounterTable
You can eliminate the table variable, and do this as a sub-query:
SELECT Count(Table_Name) as ColCount, Sum(NumOccurrences) as TotalOccurrences
FROM (SELECT Table_Name,
COUNT(Table_Name) AS NumOccurrences
FROM INFORMATION_SCHEMA.Columns
GROUP BY Table_Name
HAVING ( COUNT(Table_Name) >= 0 )) TableAlias
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 9, 2010 at 10:29 pm
Thanks Wayne.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
June 9, 2010 at 11:06 pm
As a sidebar and extension of the problem... lookup WITH ROLLUP and WITH CUBE as associated with GROUP BY. Powerful tools there.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 8, 2010 at 10:41 pm
You can use a derived table. Like this:
SELECT
SUM(DerivedTableName.NumOfOccurences) SumOccurences
, COUNT(DerivedTableName.NumOfOccurences) CountOccurences
FROM
(SELECT somecolumn
COUNT(somecolumn) AS NumOccurrences
FROM sometable
GROUP BY somecolumn
HAVING ( COUNT(somecolumn) >= 0 )
) DerivedTableName
Viewing 14 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy