Solved

Get row count of current SQL query

Posted on 2016-10-04
8
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 34

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 51

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 29

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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
LVL 34

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 29

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 29

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

617 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