private static List<MetricsValues> buildMetricsValues(final String metricName,
                                                          final List<String> ids,
                                                          final Map<String, DataTable> idMap,
                                                          final List<String> allLabels) {
        final int defaultValue = ValueColumnMetadata.INSTANCE.getDefaultValue(metricName);
        final var labeledValues = new TreeSet<>(allLabels).stream()
                                                          .flatMap(label -> ids.stream().map(id -> {
                                                              final var value = idMap.getOrDefault(id, new DataTable());

                                                              return Objects.nonNull(value.get(label)) ?
                                                                  new LabeledValue(
                                                                      label,
                                                                      id,
                                                                      value.get(label), false) :
                                                                  new LabeledValue(
                                                                      label,
                                                                      id,
                                                                      defaultValue, true);
                                                          }))
                                                          .collect(toList());
        MetricsValues current = new MetricsValues();
        List<MetricsValues> result = new ArrayList<>();
        for (LabeledValue each : labeledValues) {
            if (Objects.equals(current.getLabel(), each.label)) {
                current.getValues().addKVInt(each.kv);
            } else {
                current = new MetricsValues();
                current.setLabel(each.label);
                current.getValues().addKVInt(each.kv);
                result.add(current);
            }
        }
        return result;
    }
Compose the multiple metric result based on conditions.
oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/query/IMetricsQueryDAO.java