我做了这个查询来从DB中选择值。
SELECT
SAMPLE_TIME, ENTITY, STAT_GROUP, STAT_ID, STAT_NAME, CONVERT(decimal(16,2), STAT_VALUE/100.0) AS PERCENTAGE
FROM
VPXV_HIST_STAT_YEARLY WHERE ENTITY LIKE 'vm-1783'
AND SAMPLE_TIME > '2017-10-01'
AND SAMPLE_TIME < '2017-12-31'
AND ((STAT_GROUP = 'CPU' OR STAT_GROUP = 'MEM'))
AND STAT_NAME = 'USAGE'
ORDER by SAMPLE_TIME,STAT_GROUP ASC
这是输出:
|数据|实体| Stat_Group | Stat_ID | Stat_Name |百分比| | ------------ | --------- | ------------ | --------- | --- -------- | ------------ | | 2017-10-02 | vm-1783 | cpu | 2 |用法| 12.04 | | 2017-10-02 | vm-1783 | mem | 24 |用法| 7.21 | | 2017-10-03 | vm-1783 | cpu | 2 |用法| 4.89 | | 2017-10-03 | vm-1783 | mem | 24 |用法| 2.69 |
但我希望得到这种输出: |数据| CPU | MEM | | ------------ | ------- | ------ | | 2017-10-02 | 12.04 | 7.21 | | 2017-10-03 | 4.89 | 2.69 |
尝试过,但也许我犯了一些错误。 有什么帮助吗?
答案 0 :(得分:1)
您可以使用条件聚合:
SELECT SAMPLE_TIME,
MAX(CASE WHEN STAT_GROUP = 'MEM'
THEN CONVERT(decimal(16,2), STAT_VALUE / 100.0)
END) as CPU,
MAX(CASE WHEN STAT_GROUP = 'MEM'
THEN CONVERT(decimal(16,2), STAT_VALUE / 100.0)
END) as CPU,
FROM VPXV_HIST_STAT_YEARLY
WHERE ENTITY LIKE 'vm-1783' AND
SAMPLE_TIME > '2017-10-01' AND
SAMPLE_TIME < '2017-12-31' AND
STAT_GROUP IN ('CPU', 'MEM') AND
STAT_NAME = 'USAGE'
GROUP BY SAMPLE_TIME
ORDER by SAMPLE_TIME;