어떤 숫자값이 들어있는 2개의 필드가 있는데...
그 두 필드의 값 사이에 들어가 있는 또 다른 필드의 최고값을 구하고 싶습니다.
BETWEEN과 MAX를 어떻게 써야할까요?
댓글 5
-
아카드05
2009.02.27 01:04
-
꼬맹이
2009.02.27 01:23
-
꼬맹이
2009.02.27 01:31
WHERE - BETWEEN
Syntax
... col [NOT] BETWEEN dobj1 AND dobj2 ...
Effect
This expression is true if the content of the column col lies (not) between the values of data objects dobj1 and dobj2 (interval limits enclosed). You can define no column descriptor for the interval limits.
Example
Readout of all flights within the next 30 days.
DATA sflight_tab TYPE TABLE OF sflight.
DATA date TYPE d.
date = sy-datum + 30.
SELECT carrid connid fldate
FROM sflight
INTO CORRESPONDING FIELDS OF TABLE sflight_tab
WHERE fldate BETWEEN sy-datum AND date.
SELECT - aggregate
Syntax
... { MAX( [DISTINCT] col )
| MIN( [DISTINCT] col )
| AVG( [DISTINCT] col )
| SUM( [DISTINCT] col )
| COUNT( DISTINCT col )
| COUNT( * )
| count(*) } ... .
Effect
As many of the specified column labels as you like can be listed in the SELECT command as arguments of the above aggregate expression. In aggregate expressions, a single value is calculated from the values of multiple rows in a column as follows (note that the addition DISTINCT excludes double values from the calculation):
- MAX( [DISTINCT] col ) Determines the maximum value of the value in the column col in the resulting set or in the current group.
- MIN( [DISTINCT] col ) Determines the minimum value of the content of the column col in the resulting set or in the current group.
- AVG( [DISTINCT] col ) Determines the average value of the content of the column col in the resulting set or in the current group. The data type of the column has to be numerical.
- SUM( [DISTINCT] col ) Determines the sum of the content of the column col in the resulting set or in the current group. The data type of the column has to be numerical.
- COUNT( DISTINCT col ) Determines the number of different values in the column col in the resulting set or in the current group.
- COUNT( * ) (or count(*)) Determines the number of rows in the resulting set or in the current group. No column label is specified in this case.
If you are using aggregate expressions, all column labels that are not listed as an argument of an aggregate function are listed after the addition GROUP BY. The aggregate functions evaluate the content of the groups defined by GROUP BY in the database system and transfer the result to the combined rows of the resulting set.
The data type of aggregate expressions with the function MAX, MIN or SUM is the data type of the corresponding column in the ABAP Dictionary. Aggregate expressions with the function AVG have the data type FLTP, and those with COUNT have the data type INT4. The corresponding data object after INTO or APPENDING has to be selected accordingly.
Note the following points when using aggregate expressions:
- If the addition FOR ALL ENTRIES is used in front of WHERE, or if cluster or pool tables are listed after FROM, no other aggregate expressions apart from COUNT( * ) can be used.
- Columns of the type STRING or RAWSTRING cannot be used with aggregate functions.
- When aggregate expressions are used, the SELECT command makes it unnecessary to use SAP buffering.
- Null values are not included in the calculation for the aggregate functions. The result is a null value only if all the rows in the column in question contain the null value.
- If only aggregate expressions are used after SELECT, the results set has one row and the addition GROUP BY is not necessary. If a non-table type target area is specified after INTO, the command ENDSELECT cannot be used together with the addition SINGLE. If the aggregate expression count( * ) is not being used, an internal table can be specified after INTO, and the first row of this table is filled.
- If aggregate functions are used without GROUP BY being specified at the same time, the resulting set also contains a row if no data is found in the database. If count( * ) is used, the column in question contains the value 0. The columns in the other aggregate functions contain initial values. This row is assigned to the data object specified after INTO, and unless count( * ) is being used exclusively, sy-subrc is set to 0 and sy-dbcnt is set to 1. If count( *) is used exclusively, the addition INTO can be omitted and if no data can be found in the database, sy-subrc is set to 4 and sy-dbcnt is set to 0.
- MAX( [DISTINCT] col ) Determines the maximum value of the value in the column col in the resulting set or in the current group.
-
쭈니
2009.02.27 02:00
허 꼬맹이님.. f1스크린을 넣으셨네요?? 전에도 이렇게 답변을 달아주신걸 봤는데..
대단하세요~~
전 영어가 약한지라, 공부중이지만.... 아직도 많이 부족해서 이 답변으로는잘 이해는 못하겠네요.
그러나 f1에서 나오는 설명만큼 좋은게 없다고 들었습니다.
꼬맹이님 정말 최고십니다~짱~`~!!!!!! 어느덧 2월도 마지막주가 되었네요.
-
아카드05
2009.02.27 02:36
꼬맹이님의 따끔한 지적 2연타!
테이블 구조가 어떻게 되있는지, 값들이 어떤식으로 들어가있는지 좀더 구체적으로 올리셔야
원하시는 답변을 얻으실 수 있을거같네요..
언뜻 보기엔 SQL 문장만으로 구현하기는 어려운 내용같은데, Internal table에 담아서 IF로직으로 구현하시는게 어떨까싶습니다.