Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get row count of current SQL query

Posted on 2016-10-04
8
Medium Priority
?
96 Views
Last Modified: 2016-10-04
Hi
Can you advise how to get the number of rows returned by the current SQL query?
thanks
Fergal

example code below returns 4 rows, I'd like to add a column called "row count" or similar which has a value of 4 in it:
SELECT geo,Ste,CodeName,ReturnTrimMMNumber,Warranty,SUM(NetSales) AS NetSalesTotal
FROM mytable
WHERE ReturnTrimMMNumber IN('933256')
GROUP BY  geo,Ste,CodeName,ReturnTrimMMNumber,Warranty
0
Comment
Question by:fjkilken
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 36

Expert Comment

by:ste5an
ID: 41827647
You'll get the row count in your front-end. But this depends on your front-end..

Otherwise:

WITH    Grouped
          AS ( SELECT   M.geo ,
                        M.Ste ,
                        M.CodeName ,
                        M.ReturnTrimMMNumber ,
                        M.Warranty ,
                        SUM(M.NetSales) AS NetSalesTotal
               FROM     mytable M
               WHERE    ReturnTrimMMNumber IN ( '933256' )
               GROUP BY M.geo ,
                        M.Ste ,
                        M.CodeName ,
                        M.ReturnTrimMMNumber ,
                        M.Warranty
             )
    SELECT  G.* ,
            C.NumRows
    FROM    Grouped G
            CROSS APPLY ( SELECT    COUNT(*)
                          FROM      Grouped
                        ) C ( NumRows ); 

Open in new window

0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 41827655
Do you want to get the rowcount returned by the SELECT or do you just to want to see it printed somewhere?
0
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41827672
Pls try..

SELECT geo,Ste,CodeName,ReturnTrimMMNumber,Warranty,SUM(NetSales) AS NetSalesTotal, COUNT(*) RowCounts
FROM mytable
WHERE ReturnTrimMMNumber IN('933256')
GROUP BY  geo,Ste,CodeName,ReturnTrimMMNumber,Warranty

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 36

Expert Comment

by:ste5an
ID: 41827760
@Pawan: COUNT(*) retuns the number of rows used in the aggregate (GROUP BY). It does not reflect the number of rows returned by the SELECT.
0
 
LVL 35

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41827766
thanks ste5an. Ohh...let me just change it.

May be this..Dont have anything to test now.

SELECT geo,Ste,CodeName,ReturnTrimMMNumber,Warranty,SUM(NetSales) AS NetSalesTotal, COUNT(*) OVER() RowCounts
FROM mytable
WHERE ReturnTrimMMNumber IN('933256')
GROUP BY  geo,Ste,CodeName,ReturnTrimMMNumber,Warranty

Open in new window

1
 

Author Closing Comment

by:fjkilken
ID: 41827793
Thanks Pawan.
Can you also advise if the COUNT(*) OVER() can be used to get a count of rows for a particular condition?
ie; ReturnTrimMMNumber can have several values, is there a way to count the number of rows for each value using the syntax you supplied?
thanks
Fergal
0
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41827808
Yes we can...

You can use below..Here we are taking count by col1,col2..

COUNT(*) OVER(PARTITION BY col1,col2....etc)
0
 

Author Comment

by:fjkilken
ID: 41827809
excellent!
thanks a lot
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question