2009-11-12 5 views
2

첫 번째 적용 이후 한 달에 한 번씩 나뉘어 진 학생의 신청 수를 계산하고 싶습니다. 당신은 모든 응용 프로그램, 모든 학생들에게 SQL에서이 작업을 수행하려면 어떻게날짜 범위로 분할 된 Sql 쿼리

Student  Month  Applications 
-------  -----  ------------ 
Barry  1/2009 3 
Barry  2/2009 2 
Barry  3/2009 1 

:

Student  ApplicationDate 
-------  --------------- 
Barry  2009-01-01 
Barry  2009-01-20 
Barry  2009-01-23 
Barry  2009-02-01 
Barry  2009-02-15 
Barry  2009-03-01 

나는의 라인을 따라 뭔가를 싶습니다

다음과 같이 나는 테이블 구조를 가지고 말 ? 내가 제대로 이해하면

+0

당신은, 테이블의 스키마를하시기 바랍니다 제공 할 수 있을까요? –

답변

3
SELECT 
    student, 
    DATE_FORMAT(ApplicationDate,'%m/%Y') as Month 
    count(id) as Applications 
from YourTable 
group by ApplicationDate 
+0

확실히 내 월이 아닌 날짜별로 값을 얻을 수 있습니까? – Murph

0
select Student, month(ApplicationDate) ApplicationMonth, count(*) 
from table_name 
group by Student, ApplicationMonth 
3

, 이것은에 의해 그룹으로 수행 할 수 있습니다 : 당신이 지정한 정확한 출력을 제공하기 위해

select 
    student, 
    year(ApplicationDate), 
    month(ApplicationDate), 
    count(*) as Applications 
from YourTable 
group by student, year(ApplicationDate), month(ApplicationDate) 
3

, 나는 ...이 작업을 거라고 생각

select Student, 
     DATE_FORMAT(ApplicationDate,'%m/%Y') as 'Month', 
     count(*) as 'Applications' 
from tableName 
group by Student, month(ApplicationDate), year(AppilcationDate) 
order by year(ApplicationDate), month(ApplicationDate), Student 

편집 : Stanq에서 제안한대로 DATE_FORMAT 함수를 사용하도록 변경되었습니다.

3
select 
    student, 
    year(ApplicationDate), 
    month(ApplicationDate), 
    count(*) as Applications 
from YourTable 
group by student, year(ApplicationDate), month(ApplicationDate)