Solved

Get row count of current SQL query

Posted on 2016-10-04
8
45 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 33

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 46

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 24

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
 
LVL 33

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 24

Accepted Solution

by:
Pawan Kumar earned 500 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 24

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now