?
Solved

Count Question

Posted on 2013-06-07
6
Medium Priority
?
329 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
[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
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 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 800 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 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