Solved

Count Question

Posted on 2013-06-07
6
322 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
ID: 39229910
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
ID: 39229921
Row_number () over ()
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39229923
;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
ID: 39229939
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
ID: 39229989
Worked great - knew it was going to be a simple nested select.  Thanks!
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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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