알쓸전컴(알아두면 쓸모있는 전자 컴퓨터)
[AlaSQL] MAX_STRING Aggregators group by 문제 해결 본문
[AlaSQL] MAX_STRING Aggregators group by 문제 해결
select 문에서 grup by 하고 MAX 로 문자열 처리 할때 undefined로 표기 하는 문제가 계속 되어
문자열 MAX는 기존 쿼리문 펑션으로 문제가 있음을 알고 다음과 같이 내용을 정리 합니다.
먼저 Group by 의 Custom func 는 Aggregators 를 정의 합으로 동작 하게 됩니다.
일단 github 예제 코드를 보면
참조 : https://github.com/agershun/alasql/wiki/User-Defined-Functions
Aggregators
To make your own user defined aggregators please follow this example:
// How to implement the SUM() aggregator
alasql.aggr.MYAGGR = function(value, accumulator, stage) {
if(stage == 1) {
// first call of aggregator - for first line
var newAccumulator = value;
return newAccumulator;
} else if(stage == 2) {
// for every line in the group
accumulator = accumulator + value;
return accumulator;
} else if(stage == 3) {
// Post production - please nota that value Will be undefined
return accumulator;
}
}
위와 같이 나와 있습니다 .
우선 3단계를 거치게 됩니다.
첫번째 1번째 단계 sate == 1 는
처음 Group by 할때 value 값을 Accmulator 에 넣어 줍니다.
그럼 stage == 2 에서
처음에 Accmulator 값은 stage == 1 에서 리턴한 값으로 들어 오게 됩니ㅏㄷ.
그리고 stage==2 에서는 group by 에서 가지고 온 모든 값들을 순회? 합니다.
그리고 여기서 Accmulator을 계속 정하게 됩니다.
그리고 stage ==3 에서는 모든 값을 다 순회 하고 나서
결론적으로 어떤 값을 리턴 할껀지 에 대해서 리턴값을 정해 줍니다.
그래서 위와 같이 MAX_STRING 을 하면 잘 작동 하게 됩니다.
example ) select citynumber,MAX_STRING(cityname) from city group by citynumber
위와 같이 사용 하면 됩니다.
'AlaSQL' 카테고리의 다른 글
[AlaSql]Web Brower local in memory DB 소개 (server-less) (0) | 2019.02.27 |
---|