Solved

Count Question

Posted on 2013-06-07
6
321 Views
Last Modified: 2013-06-07
Given Table structure:
  StudentID, AttDate, AttCode, PeriodNum

I'm looking for T-SQL to return the count of the number of days for each distinct AttCode where it was used on a day for more than 3 periods.

So.....
StudentID   AttDate     AttCode   PeriodNum
12345         4/7/2013   ACT          1
12345         4/7/2013   ACT          2
12345         4/9/2013   PE            1
12345         4/9/2013   PE            2
12345         4/9/2013   PE            3
12345         4/9/2013   PE            4
12345         4/9/2013   PE            5
12345         4/10/2013   ACT            2
12345         4/10/2013   ACT            3
12345         4/10/2013   ACT            4
12345         4/10/2013   ACT            5
12345         4/14/2013   PE            3
12345         4/14/2013   PE            4
12345         4/14/2013   PE            5
12345         4/14/2013   PE            6

The results I'd like would be:
AttCode     TotalDays
ACT            1
PE              2

The first instance of ACT wouldn't count since there were only two rows for that day for that code.

I'm passing in the StudentID as part of the procedures WHERE clause so it doesn't need to be returned.

js
0
Comment
Question by:jswanberg
6 Comments
 
LVL 23

Expert Comment

by:David
Comment Utility
Whether or not this is for a classroom assignment, would you please add what you have already tried, and the results?
0
 
LVL 25

Expert Comment

by:jogos
Comment Utility
Row_number () over ()
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
;With c as (
SELECT AttDate,ATTCode, rn = ROW_NUMBER() over ( partition by AttDate, AttCode Order by PerodNum)
From YourTable
)

SELECT AttCode, Count(Distinct AttCode) cnt
FROM c
WHERE rn > 3
GROUP BY AttDate, AttCode
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 200 total points
Comment Utility
Taking a leap of faith this isn't homework, another version:

select attCode, count(*) from (
      select attCode
      from table
      group by attcode, attdate
      having count(*) > 3
)
group by attCode
/
0
 

Author Closing Comment

by:jswanberg
Comment Utility
Worked great - knew it was going to be a simple nested select.  Thanks!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

10 Experts available now in Live!

Get 1:1 Help Now