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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to get count of the rows returned ?

Author  Topic 

omkar786
Starting Member

1 Post

Posted - 2011-12-22 : 07:39:37
Hi,

I have to get count of the rows returned from an inner query that uses TOP clause, as in below query.

Used the below query but I am getting NumOccurences as 25 when the inner query returns only 5 rows when @LastNumJobs = 10.

SELECT COUNT(*) AS NumOccurences FROM JOB_HISTORY T1 WITH (NOLOCK),
(SELECT TOP(@LastNumJobs) JOB_ID,DUE_DATE FROM JOB_HISTORY WITH (NOLOCK) WHERE ID = @Id AND VERSION = @PVersion AND
PERFORMED IN (1,3) AND NODE_ID = @NodeId ORDER BY DUE_DATE DESC) T2 WHERE T1.ID = @Id AND T1.VERSION = @PVersion AND T1.NODE_ID = @NodeId AND T1.PERFORMED IN (1,3) AND T1.JOB_ID = T2.JOB_ID

Please let me know if this is the right way or suggest any alternate.

Requirement is to get count of rows out of certain number of rows being fetched, for example there are 5 rows returned for the inner query but the input value of @LastNumJobs is 10. I need to get the count of rows returned from inner query eqaual to same number of @LastNumJobs or less than that.

Thanks in advance
Omkar

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-22 : 09:15:12
Your description of the issue suggests that there is more than one row in the JOB_HISTORY table for a given JOB_ID.

Can you describe with an example what you are trying to calculate? For example, if you are trying to get the Top 5 jobs with the most recent due dates, there are simpler ways to do it.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-22 : 10:21:04
That's easy

It's @LastNumJobs...or less

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-22 : 10:27:56
Is this what you want?


SELECT TOP(@LastNumJobs)
JOB_ID
, DUE_DATE
, COUNT(*) AS JH_COUNT
FROM JOB_HISTORY
WHERE ID = @Id AND VERSION = @PVersion
AND PERFORMED IN (1,3) AND NODE_ID = @NodeId
GROUP BY JOB_ID
, DUE_DATE
ORDER BY DUE_DATE DESC



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-22 : 10:28:18
You do know that this makes very little sense

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -