Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


count rows of a query result


count rows of a query result

Author
Message
asimkumar.munshi
asimkumar.munshi
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 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?
GSquared
GSquared
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19683 Visits: 9730
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
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32788 Visits: 38339
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)



Cool
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)
GSquared
GSquared
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19683 Visits: 9730
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
asimkumar.munshi
asimkumar.munshi
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 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.
GSquared
GSquared
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19683 Visits: 9730
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
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32788 Visits: 38339
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.

Cool
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)
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