Unverified 提交 b27b12be authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1704 from katzyn/aggregate

Fix return type of PERCENTILE_CONT and MEDIAN
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*/ */
package org.h2.expression.aggregate; package org.h2.expression.aggregate;
import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
...@@ -322,8 +323,8 @@ public class Aggregate extends AbstractAggregate { ...@@ -322,8 +323,8 @@ public class Aggregate extends AbstractAggregate {
if (v == ValueNull.INSTANCE) { if (v == ValueNull.INSTANCE) {
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
} }
double arg = v.getDouble(); BigDecimal arg = v.getBigDecimal();
if (arg >= 0d && arg <= 1d) { if (arg.signum() >= 0 && arg.compareTo(BigDecimal.ONE) <= 0) {
return Percentile.getFromIndex(session, orderByList.get(0).expression, type.getValueType(), return Percentile.getFromIndex(session, orderByList.get(0).expression, type.getValueType(),
orderByList, arg, aggregateType == AggregateType.PERCENTILE_CONT); orderByList, arg, aggregateType == AggregateType.PERCENTILE_CONT);
} else { } else {
...@@ -332,7 +333,7 @@ public class Aggregate extends AbstractAggregate { ...@@ -332,7 +333,7 @@ public class Aggregate extends AbstractAggregate {
} }
} }
case MEDIAN: case MEDIAN:
return Percentile.getFromIndex(session, on, type.getValueType(), orderByList, 0.5d, true); return Percentile.getFromIndex(session, on, type.getValueType(), orderByList, Percentile.HALF, true);
case ENVELOPE: case ENVELOPE:
return ((MVSpatialIndex) AggregateDataEnvelope.getGeometryColumnIndex(on)).getBounds(session); return ((MVSpatialIndex) AggregateDataEnvelope.getGeometryColumnIndex(on)).getBounds(session);
default: default:
...@@ -402,8 +403,8 @@ public class Aggregate extends AbstractAggregate { ...@@ -402,8 +403,8 @@ public class Aggregate extends AbstractAggregate {
if (v == ValueNull.INSTANCE) { if (v == ValueNull.INSTANCE) {
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
} }
double arg = v.getDouble(); BigDecimal arg = v.getBigDecimal();
if (arg >= 0d && arg <= 1d) { if (arg.signum() >= 0 && arg.compareTo(BigDecimal.ONE) <= 0) {
return Percentile.getValue(session.getDatabase(), array, type.getValueType(), orderByList, arg, return Percentile.getValue(session.getDatabase(), array, type.getValueType(), orderByList, arg,
aggregateType == AggregateType.PERCENTILE_CONT); aggregateType == AggregateType.PERCENTILE_CONT);
} else { } else {
...@@ -416,7 +417,8 @@ public class Aggregate extends AbstractAggregate { ...@@ -416,7 +417,8 @@ public class Aggregate extends AbstractAggregate {
if (array == null) { if (array == null) {
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
} }
return Percentile.getValue(session.getDatabase(), array, type.getValueType(), orderByList, 0.5d, true); return Percentile.getValue(session.getDatabase(), array, type.getValueType(), orderByList, Percentile.HALF,
true);
} }
case MODE: case MODE:
return getMode(session, data); return getMode(session, data);
...@@ -589,9 +591,23 @@ public class Aggregate extends AbstractAggregate { ...@@ -589,9 +591,23 @@ public class Aggregate extends AbstractAggregate {
break; break;
case MIN: case MIN:
case MAX: case MAX:
case MEDIAN:
break; break;
case PERCENTILE_CONT: case PERCENTILE_CONT:
type = orderByList.get(0).expression.getType();
//$FALL-THROUGH$
case MEDIAN:
switch (type.getValueType()) {
case Value.BYTE:
case Value.SHORT:
case Value.INT:
case Value.LONG:
case Value.DECIMAL:
case Value.DOUBLE:
case Value.FLOAT:
type = TypeInfo.TYPE_DECIMAL_DEFAULT;
break;
}
break;
case PERCENTILE_DISC: case PERCENTILE_DISC:
case MODE: case MODE:
type = orderByList.get(0).expression.getType(); type = orderByList.get(0).expression.getType();
......
...@@ -32,11 +32,7 @@ import org.h2.value.CompareMode; ...@@ -32,11 +32,7 @@ import org.h2.value.CompareMode;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueDate; import org.h2.value.ValueDate;
import org.h2.value.ValueDecimal; import org.h2.value.ValueDecimal;
import org.h2.value.ValueDouble;
import org.h2.value.ValueFloat;
import org.h2.value.ValueInt;
import org.h2.value.ValueInterval; import org.h2.value.ValueInterval;
import org.h2.value.ValueLong;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
import org.h2.value.ValueTime; import org.h2.value.ValueTime;
import org.h2.value.ValueTimestamp; import org.h2.value.ValueTimestamp;
...@@ -47,6 +43,11 @@ import org.h2.value.ValueTimestampTimeZone; ...@@ -47,6 +43,11 @@ import org.h2.value.ValueTimestampTimeZone;
*/ */
final class Percentile { final class Percentile {
/**
* BigDecimal value of 0.5.
*/
static final BigDecimal HALF = BigDecimal.valueOf(0.5d);
private static boolean isNullsLast(Index index) { private static boolean isNullsLast(Index index) {
IndexColumn ic = index.getIndexColumns()[0]; IndexColumn ic = index.getIndexColumns()[0];
int sortType = ic.sortType; int sortType = ic.sortType;
...@@ -106,22 +107,22 @@ final class Percentile { ...@@ -106,22 +107,22 @@ final class Percentile {
* @return the result * @return the result
*/ */
static Value getValue(Database database, Value[] array, int dataType, ArrayList<SelectOrderBy> orderByList, static Value getValue(Database database, Value[] array, int dataType, ArrayList<SelectOrderBy> orderByList,
double percentile, boolean interpolate) { BigDecimal percentile, boolean interpolate) {
final CompareMode compareMode = database.getCompareMode(); final CompareMode compareMode = database.getCompareMode();
Arrays.sort(array, compareMode); Arrays.sort(array, compareMode);
int count = array.length; int count = array.length;
boolean reverseIndex = orderByList != null && (orderByList.get(0).sortType & SortOrder.DESCENDING) != 0; boolean reverseIndex = orderByList != null && (orderByList.get(0).sortType & SortOrder.DESCENDING) != 0;
double fpRow = (count - 1) * percentile; BigDecimal fpRow = BigDecimal.valueOf(count - 1).multiply(percentile);
int rowIdx1 = (int) fpRow; int rowIdx1 = fpRow.intValue();
double factor = fpRow - rowIdx1; BigDecimal factor = fpRow.subtract(BigDecimal.valueOf(rowIdx1));
int rowIdx2; int rowIdx2;
if (factor == 0d) { if (factor.signum() == 0) {
interpolate = false; interpolate = false;
rowIdx2 = rowIdx1; rowIdx2 = rowIdx1;
} else { } else {
rowIdx2 = rowIdx1 + 1; rowIdx2 = rowIdx1 + 1;
if (!interpolate) { if (!interpolate) {
if (factor > 0.5d) { if (factor.compareTo(HALF) > 0) {
rowIdx1 = rowIdx2; rowIdx1 = rowIdx2;
} else { } else {
rowIdx2 = rowIdx1; rowIdx2 = rowIdx1;
...@@ -151,7 +152,7 @@ final class Percentile { ...@@ -151,7 +152,7 @@ final class Percentile {
* @return the result * @return the result
*/ */
static Value getFromIndex(Session session, Expression expression, int dataType, static Value getFromIndex(Session session, Expression expression, int dataType,
ArrayList<SelectOrderBy> orderByList, double percentile, boolean interpolate) { ArrayList<SelectOrderBy> orderByList, BigDecimal percentile, boolean interpolate) {
Index index = getColumnIndex(expression); Index index = getColumnIndex(expression);
long count = index.getRowCount(session); long count = index.getRowCount(session);
if (count == 0) { if (count == 0) {
...@@ -199,17 +200,17 @@ final class Percentile { ...@@ -199,17 +200,17 @@ final class Percentile {
} }
boolean reverseIndex = (orderByList != null ? orderByList.get(0).sortType & SortOrder.DESCENDING : 0) boolean reverseIndex = (orderByList != null ? orderByList.get(0).sortType & SortOrder.DESCENDING : 0)
!= (index.getIndexColumns()[0].sortType & SortOrder.DESCENDING); != (index.getIndexColumns()[0].sortType & SortOrder.DESCENDING);
double fpRow = (count - 1) * percentile; BigDecimal fpRow = BigDecimal.valueOf(count - 1).multiply(percentile);
long rowIdx1 = (long) fpRow; long rowIdx1 = fpRow.longValue();
double factor = fpRow - rowIdx1; BigDecimal factor = fpRow.subtract(BigDecimal.valueOf(rowIdx1));
long rowIdx2; long rowIdx2;
if (factor == 0d) { if (factor.signum() == 0) {
interpolate = false; interpolate = false;
rowIdx2 = rowIdx1; rowIdx2 = rowIdx1;
} else { } else {
rowIdx2 = rowIdx1 + 1; rowIdx2 = rowIdx1 + 1;
if (!interpolate) { if (!interpolate) {
if (factor > 0.5d) { if (factor.compareTo(HALF) > 0) {
rowIdx1 = rowIdx2; rowIdx1 = rowIdx2;
} else { } else {
rowIdx2 = rowIdx1; rowIdx2 = rowIdx1;
...@@ -246,10 +247,10 @@ final class Percentile { ...@@ -246,10 +247,10 @@ final class Percentile {
} }
return interpolate(v, v2, factor, dataType, database.getMode(), database.getCompareMode()); return interpolate(v, v2, factor, dataType, database.getMode(), database.getCompareMode());
} }
return v; return v.convertTo(dataType);
} }
private static Value interpolate(Value v0, Value v1, double factor, int dataType, Mode databaseMode, private static Value interpolate(Value v0, Value v1, BigDecimal factor, int dataType, Mode databaseMode,
CompareMode compareMode) { CompareMode compareMode) {
if (v0.compareTo(v1, databaseMode, compareMode) == 0) { if (v0.compareTo(v1, databaseMode, compareMode) == 0) {
return v0.convertTo(dataType); return v0.convertTo(dataType);
...@@ -258,21 +259,18 @@ final class Percentile { ...@@ -258,21 +259,18 @@ final class Percentile {
case Value.BYTE: case Value.BYTE:
case Value.SHORT: case Value.SHORT:
case Value.INT: case Value.INT:
return ValueInt.get((int) (v0.getInt() * (1 - factor) + v1.getInt() * factor)).convertTo(dataType); return ValueDecimal.get(
interpolateDecimal(BigDecimal.valueOf(v0.getInt()), BigDecimal.valueOf(v1.getInt()), factor));
case Value.LONG: case Value.LONG:
return ValueLong return ValueDecimal.get(
.get(interpolateDecimal(BigDecimal.valueOf(v0.getLong()), BigDecimal.valueOf(v1.getLong()), factor) interpolateDecimal(BigDecimal.valueOf(v0.getLong()), BigDecimal.valueOf(v1.getLong()), factor));
.longValue());
case Value.DECIMAL: case Value.DECIMAL:
return ValueDecimal.get(interpolateDecimal(v0.getBigDecimal(), v1.getBigDecimal(), factor)); return ValueDecimal.get(interpolateDecimal(v0.getBigDecimal(), v1.getBigDecimal(), factor));
case Value.FLOAT: case Value.FLOAT:
return ValueFloat.get(
interpolateDecimal(BigDecimal.valueOf(v0.getFloat()), BigDecimal.valueOf(v1.getFloat()), factor)
.floatValue());
case Value.DOUBLE: case Value.DOUBLE:
return ValueDouble.get( return ValueDecimal.get(
interpolateDecimal(BigDecimal.valueOf(v0.getDouble()), BigDecimal.valueOf(v1.getDouble()), factor) interpolateDecimal(
.doubleValue()); BigDecimal.valueOf(v0.getDouble()), BigDecimal.valueOf(v1.getDouble()), factor));
case Value.TIME: { case Value.TIME: {
ValueTime t0 = (ValueTime) v0.convertTo(Value.TIME), t1 = (ValueTime) v1.convertTo(Value.TIME); ValueTime t0 = (ValueTime) v0.convertTo(Value.TIME), t1 = (ValueTime) v1.convertTo(Value.TIME);
BigDecimal n0 = BigDecimal.valueOf(t0.getNanos()); BigDecimal n0 = BigDecimal.valueOf(t0.getNanos());
...@@ -307,12 +305,15 @@ final class Percentile { ...@@ -307,12 +305,15 @@ final class Percentile {
ts1 = (ValueTimestampTimeZone) v1.convertTo(Value.TIMESTAMP_TZ); ts1 = (ValueTimestampTimeZone) v1.convertTo(Value.TIMESTAMP_TZ);
BigDecimal a0 = timestampToDecimal(ts0.getDateValue(), ts0.getTimeNanos()); BigDecimal a0 = timestampToDecimal(ts0.getDateValue(), ts0.getTimeNanos());
BigDecimal a1 = timestampToDecimal(ts1.getDateValue(), ts1.getTimeNanos()); BigDecimal a1 = timestampToDecimal(ts1.getDateValue(), ts1.getTimeNanos());
double offset = ts0.getTimeZoneOffsetMins() * (1 - factor) + ts1.getTimeZoneOffsetMins() * factor; BigDecimal offset = BigDecimal.valueOf(ts0.getTimeZoneOffsetMins())
short sOffset = (short) offset; .multiply(BigDecimal.ONE.subtract(factor))
.add(BigDecimal.valueOf(ts1.getTimeZoneOffsetMins()).multiply(factor));
short shortOffset = offset.shortValue();
BigDecimal shortOffsetBD = BigDecimal.valueOf(shortOffset);
BigDecimal bd = interpolateDecimal(a0, a1, factor); BigDecimal bd = interpolateDecimal(a0, a1, factor);
if (offset != sOffset) { if (offset.compareTo(shortOffsetBD) != 0) {
bd = bd.add(BigDecimal.valueOf(offset - sOffset) bd = bd.add(
.multiply(BigDecimal.valueOf(DateTimeUtils.NANOS_PER_MINUTE))); offset.subtract(shortOffsetBD).multiply(BigDecimal.valueOf(DateTimeUtils.NANOS_PER_MINUTE)));
} }
BigInteger[] dr = bd.toBigInteger().divideAndRemainder(IntervalUtils.NANOS_PER_DAY_BI); BigInteger[] dr = bd.toBigInteger().divideAndRemainder(IntervalUtils.NANOS_PER_DAY_BI);
long absoluteDay = dr[0].longValue(); long absoluteDay = dr[0].longValue();
...@@ -322,7 +323,7 @@ final class Percentile { ...@@ -322,7 +323,7 @@ final class Percentile {
absoluteDay--; absoluteDay--;
} }
return ValueTimestampTimeZone.fromDateValueAndNanos(DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay), return ValueTimestampTimeZone.fromDateValueAndNanos(DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay),
timeNanos, sOffset); timeNanos, shortOffset);
} }
case Value.INTERVAL_YEAR: case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH: case Value.INTERVAL_MONTH:
...@@ -343,7 +344,7 @@ final class Percentile { ...@@ -343,7 +344,7 @@ final class Percentile {
.toBigInteger()); .toBigInteger());
default: default:
// Use the same rules as PERCENTILE_DISC // Use the same rules as PERCENTILE_DISC
return (factor > 0.5d ? v1 : v0).convertTo(dataType); return (factor.compareTo(HALF) > 0 ? v1 : v0).convertTo(dataType);
} }
} }
...@@ -352,8 +353,8 @@ final class Percentile { ...@@ -352,8 +353,8 @@ final class Percentile {
.multiply(IntervalUtils.NANOS_PER_DAY_BI).add(BigInteger.valueOf(timeNanos))); .multiply(IntervalUtils.NANOS_PER_DAY_BI).add(BigInteger.valueOf(timeNanos)));
} }
private static BigDecimal interpolateDecimal(BigDecimal d0, BigDecimal d1, double factor) { private static BigDecimal interpolateDecimal(BigDecimal d0, BigDecimal d1, BigDecimal factor) {
return d0.multiply(BigDecimal.valueOf(1 - factor)).add(d1.multiply(BigDecimal.valueOf(factor))); return d0.multiply(BigDecimal.ONE.subtract(factor)).add(d1.multiply(factor));
} }
private Percentile() { private Percentile() {
......
...@@ -130,6 +130,11 @@ public class ValueFloat extends Value { ...@@ -130,6 +130,11 @@ public class ValueFloat extends Value {
return value; return value;
} }
@Override
public double getDouble() {
return value;
}
@Override @Override
public String getString() { public String getString() {
return Float.toString(value); return Float.toString(value);
......
...@@ -35,7 +35,7 @@ select ...@@ -35,7 +35,7 @@ select
> rows: 1 > rows: 1
select median(distinct v) from test; select median(distinct v) from test;
>> 15 >> 15.0
insert into test values (10); insert into test values (10);
> update count: 1 > update count: 1
...@@ -45,8 +45,8 @@ select ...@@ -45,8 +45,8 @@ select
percentile_disc(0.5) within group (order by v desc) d50d, percentile_disc(0.5) within group (order by v desc) d50d,
median(v) m from test; median(v) m from test;
> D50A D50D M > D50A D50D M
> ---- ---- -- > ---- ---- ----
> 10 20 15 > 10 20 15.0
> rows: 1 > rows: 1
drop table test; drop table test;
...@@ -84,7 +84,7 @@ select ...@@ -84,7 +84,7 @@ select
> rows: 1 > rows: 1
select median(distinct v) from test; select median(distinct v) from test;
>> 15 >> 15.0
insert into test values (10); insert into test values (10);
> update count: 1 > update count: 1
...@@ -94,8 +94,8 @@ select ...@@ -94,8 +94,8 @@ select
percentile_disc(0.5) within group (order by v desc) d50d, percentile_disc(0.5) within group (order by v desc) d50d,
median(v) m from test; median(v) m from test;
> D50A D50D M > D50A D50D M
> ---- ---- -- > ---- ---- ----
> 10 20 15 > 10 20 15.0
> rows: 1 > rows: 1
drop table test; drop table test;
...@@ -133,7 +133,7 @@ select ...@@ -133,7 +133,7 @@ select
> rows: 1 > rows: 1
select median(distinct v) from test; select median(distinct v) from test;
>> 15 >> 15.0
insert into test values (10); insert into test values (10);
> update count: 1 > update count: 1
...@@ -143,8 +143,8 @@ select ...@@ -143,8 +143,8 @@ select
percentile_disc(0.5) within group (order by v desc) d50d, percentile_disc(0.5) within group (order by v desc) d50d,
median(v) m from test; median(v) m from test;
> D50A D50D M > D50A D50D M
> ---- ---- -- > ---- ---- ----
> 10 20 15 > 10 20 15.0
> rows: 1 > rows: 1
drop table test; drop table test;
...@@ -182,7 +182,7 @@ select ...@@ -182,7 +182,7 @@ select
> rows: 1 > rows: 1
select median(distinct v) from test; select median(distinct v) from test;
>> 15 >> 15.0
insert into test values (10); insert into test values (10);
> update count: 1 > update count: 1
...@@ -192,8 +192,8 @@ select ...@@ -192,8 +192,8 @@ select
percentile_disc(0.5) within group (order by v desc) d50d, percentile_disc(0.5) within group (order by v desc) d50d,
median(v) m from test; median(v) m from test;
> D50A D50D M > D50A D50D M
> ---- ---- -- > ---- ---- ----
> 10 20 15 > 10 20 15.0
> rows: 1 > rows: 1
drop table test; drop table test;
...@@ -231,7 +231,7 @@ select ...@@ -231,7 +231,7 @@ select
> rows: 1 > rows: 1
select median(distinct v) from test; select median(distinct v) from test;
>> 15 >> 15.0
insert into test values (10); insert into test values (10);
> update count: 1 > update count: 1
...@@ -241,8 +241,8 @@ select ...@@ -241,8 +241,8 @@ select
percentile_disc(0.5) within group (order by v desc) d50d, percentile_disc(0.5) within group (order by v desc) d50d,
median(v) m from test; median(v) m from test;
> D50A D50D M > D50A D50D M
> ---- ---- -- > ---- ---- ----
> 10 20 15 > 10 20 15.0
> rows: 1 > rows: 1
drop table test; drop table test;
...@@ -280,7 +280,7 @@ select ...@@ -280,7 +280,7 @@ select
> rows: 1 > rows: 1
select median(distinct v) from test; select median(distinct v) from test;
>> 15 >> 15.0
insert into test values (10); insert into test values (10);
> update count: 1 > update count: 1
...@@ -290,8 +290,8 @@ select ...@@ -290,8 +290,8 @@ select
percentile_disc(0.5) within group (order by v desc) d50d, percentile_disc(0.5) within group (order by v desc) d50d,
median(v) m from test; median(v) m from test;
> D50A D50D M > D50A D50D M
> ---- ---- -- > ---- ---- ----
> 10 20 15 > 10 20 15.0
> rows: 1 > rows: 1
drop table test; drop table test;
...@@ -313,13 +313,13 @@ select median(v) from test; ...@@ -313,13 +313,13 @@ select median(v) from test;
>> 20 >> 20
select median(distinct v) from test; select median(distinct v) from test;
>> 15 >> 15.0
insert into test values (10); insert into test values (10);
> update count: 1 > update count: 1
select median(v) from test; select median(v) from test;
>> 15 >> 15.0
drop table test; drop table test;
> ok > ok
...@@ -340,13 +340,13 @@ select median(v) from test; ...@@ -340,13 +340,13 @@ select median(v) from test;
>> 20 >> 20
select median(distinct v) from test; select median(distinct v) from test;
>> 15 >> 15.0
insert into test values (10); insert into test values (10);
> update count: 1 > update count: 1
select median(v) from test; select median(v) from test;
>> 15 >> 15.0
drop table test; drop table test;
> ok > ok
...@@ -367,13 +367,13 @@ select median(v) from test; ...@@ -367,13 +367,13 @@ select median(v) from test;
>> 20 >> 20
select median(distinct v) from test; select median(distinct v) from test;
>> 15 >> 15.0
insert into test values (10); insert into test values (10);
> update count: 1 > update count: 1
select median(v) from test; select median(v) from test;
>> 15 >> 15.0
drop table test; drop table test;
> ok > ok
...@@ -394,13 +394,13 @@ select median(v) from test; ...@@ -394,13 +394,13 @@ select median(v) from test;
>> 20 >> 20
select median(distinct v) from test; select median(distinct v) from test;
>> 15 >> 15.0
insert into test values (10); insert into test values (10);
> update count: 1 > update count: 1
select median(v) from test; select median(v) from test;
>> 15 >> 15.0
drop table test; drop table test;
> ok > ok
...@@ -421,13 +421,13 @@ select median(v) from test; ...@@ -421,13 +421,13 @@ select median(v) from test;
>> 2.0 >> 2.0
select median(distinct v) from test; select median(distinct v) from test;
>> 1.5 >> 1.50
insert into test values (1); insert into test values (1);
> update count: 1 > update count: 1
select median(v) from test; select median(v) from test;
>> 1.5 >> 1.50
drop table test; drop table test;
> ok > ok
...@@ -448,13 +448,13 @@ select median(v) from test; ...@@ -448,13 +448,13 @@ select median(v) from test;
>> 2.0 >> 2.0
select median(distinct v) from test; select median(distinct v) from test;
>> 1.5 >> 1.50
insert into test values (1); insert into test values (1);
> update count: 1 > update count: 1
select median(v) from test; select median(v) from test;
>> 1.5 >> 1.50
drop table test; drop table test;
> ok > ok
...@@ -650,7 +650,7 @@ insert into test values ('Group 2A', 10), ('Group 2A', 10), ('Group 2A', 20), ...@@ -650,7 +650,7 @@ insert into test values ('Group 2A', 10), ('Group 2A', 10), ('Group 2A', 20),
select name, median(value) from test group by name order by name; select name, median(value) from test group by name order by name;
> NAME MEDIAN(VALUE) > NAME MEDIAN(VALUE)
> -------- ------------- > -------- -------------
> Group 1X 45 > Group 1X 45.0
> Group 2A 10 > Group 2A 10
> Group 3B null > Group 3B null
> rows (ordered): 3 > rows (ordered): 3
...@@ -707,7 +707,7 @@ insert into test values (10), (20); ...@@ -707,7 +707,7 @@ insert into test values (10), (20);
> update count: 2 > update count: 2
select median(v) from test; select median(v) from test;
>> 15 >> 15.0
insert into test values (20), (10), (20); insert into test values (20), (10), (20);
> update count: 3 > update count: 3
...@@ -729,7 +729,7 @@ insert into test values (10), (20), (30), (40), (50), (60), (70), (80), (90), (1 ...@@ -729,7 +729,7 @@ insert into test values (10), (20), (30), (40), (50), (60), (70), (80), (90), (1
select median(v), median(v) filter (where v >= 40) from test where v <= 100; select median(v), median(v) filter (where v >= 40) from test where v <= 100;
> MEDIAN(V) MEDIAN(V) FILTER (WHERE (V >= 40)) > MEDIAN(V) MEDIAN(V) FILTER (WHERE (V >= 40))
> --------- ---------------------------------- > --------- ----------------------------------
> 55 70 > 55.0 70
> rows: 1 > rows: 1
create index test_idx on test(v); create index test_idx on test(v);
...@@ -738,13 +738,13 @@ create index test_idx on test(v); ...@@ -738,13 +738,13 @@ create index test_idx on test(v);
select median(v), median(v) filter (where v >= 40) from test where v <= 100; select median(v), median(v) filter (where v >= 40) from test where v <= 100;
> MEDIAN(V) MEDIAN(V) FILTER (WHERE (V >= 40)) > MEDIAN(V) MEDIAN(V) FILTER (WHERE (V >= 40))
> --------- ---------------------------------- > --------- ----------------------------------
> 55 70 > 55.0 70
> rows: 1 > rows: 1
select median(v), median(v) filter (where v >= 40) from test; select median(v), median(v) filter (where v >= 40) from test;
> MEDIAN(V) MEDIAN(V) FILTER (WHERE (V >= 40)) > MEDIAN(V) MEDIAN(V) FILTER (WHERE (V >= 40))
> --------- ---------------------------------- > --------- ----------------------------------
> 65 80 > 65.0 80
> rows: 1 > rows: 1
drop table test; drop table test;
...@@ -774,7 +774,7 @@ select dept, median(amount) filter (where amount >= 20) from test group by dept ...@@ -774,7 +774,7 @@ select dept, median(amount) filter (where amount >= 20) from test group by dept
> ------ -------------------------------------------- > ------ --------------------------------------------
> First 30 > First 30
> Second 22 > Second 22
> Third 160 > Third 160.0
> rows (ordered): 3 > rows (ordered): 3
select dept, median(amount) filter (where amount >= 20) from test select dept, median(amount) filter (where amount >= 20) from test
...@@ -782,7 +782,7 @@ select dept, median(amount) filter (where amount >= 20) from test ...@@ -782,7 +782,7 @@ select dept, median(amount) filter (where amount >= 20) from test
> DEPT MEDIAN(AMOUNT) FILTER (WHERE (AMOUNT >= 20)) > DEPT MEDIAN(AMOUNT) FILTER (WHERE (AMOUNT >= 20))
> ------ -------------------------------------------- > ------ --------------------------------------------
> First 30 > First 30
> Second 21 > Second 21.0
> Third 150 > Third 150
> rows (ordered): 3 > rows (ordered): 3
...@@ -804,10 +804,10 @@ select ...@@ -804,10 +804,10 @@ select
percentile_cont(0.95) within group (order by v) c95a, percentile_cont(0.95) within group (order by v) c95a,
percentile_cont(0.95) within group (order by v desc) c95d, percentile_cont(0.95) within group (order by v desc) c95d,
g from test group by g; g from test group by g;
> C05A C05D C50 C50D C95A C95D G > C05A C05D C50 C50D C95A C95D G
> ---- ---- --- ---- ---- ---- - > ----- ----- ---- ---- ----- ----- -
> 1 9 5 5 9 1 1 > 1.45 9.55 5.5 5.5 9.55 1.45 1
> 11 89 25 25 89 11 2 > 11.50 89.50 25.0 25.0 89.50 11.50 2
> rows: 2 > rows: 2
select select
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论