April 9, 2009 at 1:00 pm
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?
April 9, 2009 at 1:03 pm
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
April 9, 2009 at 1:12 pm
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)
April 9, 2009 at 1:33 pm
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
April 9, 2009 at 1:44 pm
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.
April 9, 2009 at 1:48 pm
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
April 9, 2009 at 2:15 pm
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.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply