2014-04-27 3 views
1

내가 이런 일을하려고 오전 :NHibernate에 SQLFunction 그룹 별

SELECT round(song.rating), count(song.song_id) FROM song 
GROUP BY round(song.rating); 

내 QueryOver :

var output = sess.QueryOver<song>() 
       .SelectList(list => list 
        .Select(Projections.SqlFunction("round", NHibernateUtil.Int32, Projections.GroupProperty("rating"))) 
        .SelectCount(s => s.song_id)) 
       .List<object[]>() 
       .Select(prop => new RatingStat 
       { 
        rating = (int)prop[0], 
        count = (int)prop[1] 
       }).ToList<RatingStat>(); 

예상 출력 :

+---------------------------+---------------------------+ 
|       0 |      12 | 
|       1 |       1 | 
|       3 |       1 | 
|       4 |       6 | 
|       5 |       3 | 
|       6 |       6 | 
|       7 |      12 | 
|       8 |       7 | 
|       9 |       9 | 
|      10 |       2 | 
+---------------------------+---------------------------+ 

실제 출력 :

0       12 
1       1 
3       1 
4       1 
4       3 
4       1 
4       1 
5       1 
5       1 
5       1 
6       2 
6       1 
6       3 
7       2 
7       9 
7       1 
8       1 
8       3 
8       2 
8       1 
9       1 
9       3 
9       1 
9       4 
10      2 

내 MySQL dialect가 라운드 기능을 지원하지 않기 때문에 MySQL5Dialect에서 상속받은 내 자신의 dialect를 사용하고 있습니다. 내 사투리 얼마나 라운드 기능을 정의 여기 입니다 :

RegisterFunction("round", new StandardSafeSQLFunction("round", NHibernateUtil.Int32,1)); 

내 질문은, 왜이 같은 평가 값의 여러 그룹을해야합니까? 반올림 된 값은 구별되어야합니다. 라운드 기능이 제대로 작동하지 않을 수 있습니까? 편집 : 추가 genetated SQL 문

SELECT round(this_.rating) as y0_, count(this_.song_ID) as y1_ FROM song this_ GROUP BY this_.rating 
+0

나는 이것이 아마 바보 같은 의견입니다 알고 있지만 그것 제대로하여 그룹을 적용하지처럼 보이는 실제 생성 된 SQL 문은 무엇인가 . – Rippo

답변

3

발견 솔루션 :

var t = Projections.SqlFunction("round", NHibernateUtil.Int32, Projections.GroupProperty("rating")); 
var output = sess.QueryOver<Song>() 
     .SelectList(list => list 
     .Select(Projections.SqlFunction("round", NHibernateUtil.Int32, Projections.GroupProperty(t))) 
     .SelectCount(s => s.song_id)) 
     .List<object[]>() 
     .Select(prop => new RatingStat 
     { 
      rating = (int)prop[0], 
      count = (int)prop[1] 
     }).ToList<RatingStat>();