Solved

Find count within a query

Posted on 2014-04-04
4
248 Views
Last Modified: 2014-04-05
I have the following sql query

SELECT Max(Text4)Text4,Max(Text5)Text5,Max(Combo11)Combo11, Sum(Total)TotalsperStudent FROM AttendanceGrid INNER JOIN tblRegistration ON AttendanceGrid.[ID]=tblRegistration.[ID] Where  AttendanceGrid.Fiscal = '2014'  Group by tblRegistration.ID HAVING Sum(Total)>0 

Open in new window


The output looks like this:

Text4	Text5	Combo11	TotalsperStudent
Guthrie	Tionne	Mid Austin	   129
Rodriguez	Eleany	LOS	52
Meeks	Quiana	Major Adams	   53
Colley	Kayla	Mid Austin	     236

Open in new window


How can I change the query to get the following output.  I want to find out how many records there are per group (Combo11)?

My desired output based on the above results would be:

Mid Austin   2
LOS                1
Major Adams  1

Open in new window

0
Comment
Question by:al4629740
  • 2
4 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39979372
SELECT Combo11, COUNT(DISTINCT Text4+Text5) Recs_Per_Group
FROM AttendanceGrid
INNER JOIN tblRegistration ON AttendanceGrid.[ID]=tblRegistration.[ID]
Where  AttendanceGrid.Fiscal = '2014'  
Group by Combo11
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39979423
SELECT Combo11, 
       COUNT(*) Cnt 
  FROM (SELECT MAX(Combo11)Combo11 
          FROM AttendanceGrid 
               INNER JOIN tblRegistration 
                       ON AttendanceGrid.[ID] = tblRegistration.[ID] 
         WHERE AttendanceGrid.Fiscal = '2014' 
         GROUP BY tblRegistration.ID 
        HAVING SUM(Total) > 0) T1 
 GROUP BY Combo11 

Open in new window

0
 

Author Comment

by:al4629740
ID: 39980593
SELECT Combo11,
       COUNT(*) Cnt

What does Cnt stand for or do?
0
 

Author Comment

by:al4629740
ID: 39980603
nevermind....
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

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.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

760 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

19 Experts available now in Live!

Get 1:1 Help Now