Solved

Get row count of current SQL query

Posted on 2016-10-04
8
36 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 32

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 45

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 18

Expert Comment

by:Pawan Kumar Khowal
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 32

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Accepted Solution

by:
Pawan Kumar Khowal 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 18

Expert Comment

by:Pawan Kumar Khowal
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

762 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

20 Experts available now in Live!

Get 1:1 Help Now