2013-04-02 3 views
0

모든 유형의 장치 개수 :나는 테이블이 주

idint(11) NOT NULL 
type varchar(64) NOT NULL 
created_on datetime NOT NULL 

내가 하나의 쿼리를 사용 doctrine2에 주 모든 유형 장치 (ISO, 안드로이드 ...)를 셀 수 있습니까?

샘플 출력 :

Week iOS Android Other 
1  4 5  6 
2  3 5  9 

답변

1

이렇게하면 기재된 출력하지만 용이하지 Output Sum of some column in week intervals throughout a year, week dates consistent with day에 그 상세한 유사한 방법을 이용하여 전적으로 가능하다. 이를 처리하는 가장 좋은 방법은 WEEK 및 TYPE을 그룹화하여 SELECT를 수행하는 것입니다. 나는.

SELECT 

    -- Get the week and month of each row 
    YEAR(created_on) AS Year, 
    WEEK(created_on) AS Week, 
    type 

    -- Count how many rows are in this group 
    COUNT(*) AS frequency 

FROM yourTable AS yt 

-- Order by month and then week, so that week 4 in Jan comes before week 4 in Feb 
GROUP BY 
    Year ASC, 
    Week ASC, 
    type ASC 

이 출력주는 것 같은

Year Week Type  frequency 
2013 1  'iOS'  4 
2013 1  'Android' 5 
2013 1  'Other' 6 
2013 2  'iOS'  3 
2013 2  'Android' 5 
2013 2  'Other' 9