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

count rows of a query result Expand / Collapse
Author
Message
Posted Thursday, April 09, 2009 1:00 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 09, 2009 1:43 PM
Points: 2, Visits: 1
Hi
Can someone help me with the syntax of finding a rowcount of query set....

something like

select count(*)
from
(
SELECT distinct year(cumdate) from dbo.EX1_FACT_SO
where year(cumdate) in (year(getdate()),year(getdate())-1)
)

The above currently does not work for me with sql server....what's the right way to do this?
Post #694362
Posted Thursday, April 09, 2009 1:03 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 10:04 AM
Points: 15,442, Visits: 9,590
Couple ways to do it. First, you could just add an "alias" to the query you already have. Do that by typing a name for it after the last close-parentheses.

select count(*)
from
(
SELECT distinct year(cumdate) from dbo.EX1_FACT_SO
where year(cumdate) in (year(getdate()),year(getdate())-1)
) AliasName

Another way would be to eliminate the outer query:

SELECT count(distinct year(cumdate))
from dbo.EX1_FACT_SO
where year(cumdate) in (year(getdate()),year(getdate())-1)

Try both, see which one you like better.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #694366
Posted Thursday, April 09, 2009 1:12 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 2:53 PM
Points: 22,529, Visits: 30,301
Well GSquared, you beat me, but one thing. Need to add an alias in the derived table for the column as well:

select count(*)
from
(
SELECT distinct year(cumdate) as YearDt from dbo.EX1_FACT_SO
where year(cumdate) in (year(getdate()),year(getdate())-1)
) dt


My alternative solution was similar as well:

select
count(distinct year(cumdate))
from
dbo.EX1_FACT_SO
where
cumdate >= dateadd(yy, datediff(yy, 0, getdate()) - 1, 0) and
cumdate < dateadd(yy, datediff(yy, 0, getdate()) + 1, 0)




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #694375
Posted Thursday, April 09, 2009 1:33 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 10:04 AM
Points: 15,442, Visits: 9,590
Yep, a column alias will also be needed. Missed that one.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #694389
Posted Thursday, April 09, 2009 1:44 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 09, 2009 1:43 PM
Points: 2, Visits: 1
The first method does not work for me...so...what if i have multiple selects in the inner query...I am selecting like 20 columns in the select...

The second method is not what i want...my inner query is far more complex....I want to count the rows of a result set.
Post #694402
Posted Thursday, April 09, 2009 1:48 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 10:04 AM
Points: 15,442, Visits: 9,590
Either of those methods will work if written correctly.

If they don't do what you need, you need to clarify what you are trying to do.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #694405
Posted Thursday, April 09, 2009 2:15 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 2:53 PM
Points: 22,529, Visits: 30,301
asimkumar.munshi (4/9/2009)
The first method does not work for me...so...what if i have multiple selects in the inner query...I am selecting like 20 columns in the select...

The second method is not what i want...my inner query is far more complex....I want to count the rows of a result set.


You'd get much better answers if you provided the actual requirements instead trying to simplifying and then trying to take that to a more complex environment.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #694429
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse