我在MySQL中有一个select查询,如下所示: 此查询以表格
的形式执行并生成结果Current | Past1 | Past2 | Past 3 | Past4
200 600 800 000 88
我想转换这些结果以获取表单中的信息:因此我希望结果是(转置)
Current 200
Past1 600
Past2 800
Past3 000
Past4 88
可以使用oracle中的SQL语句完成此操作,还是需要PL SQL过程。如果PL SQL可以提供一些示例引用。
select
sum(case
when CPT_ORIGINATED > SUBDATE(DATE(NOW()),30) THEN 1 else 0
end) as Current,
sum(case
when CPT_ORIGINATED > SUBDATE(DATE(NOW()),60) AND CPT_ORIGINATED < SUBDATE(DATE(NOW()),30) THEN 1 else 0
end) as Past1,
sum(case
when CPT_ORIGINATED > SUBDATE(DATE(NOW()),90) AND CPT_ORIGINATED < SUBDATE(DATE(NOW()),60) THEN 1 else 0
end) as Past2,
sum(case
when CPT_ORIGINATED > SUBDATE(DATE(NOW()),120) AND CPT_ORIGINATED < SUBDATE(DATE(NOW()),90) THEN 1 else 0
end) as Past3,
sum(case
when CPT_ORIGINATED > SUBDATE(DATE(NOW()),150) AND CPT_ORIGINATED < SUBDATE(DATE(NOW()),120) THEN 1 else 0
end) as Past4
from `cpt-prod`
where CPT_STATE <> 'Closed'
答案 0 :(得分:1)
这样做的一种方法是结合:
WITH YourWith as (
select value1, value2, value3, ...
from YourTable
)
select name = 'Current', value1 from YourWith
union all
select name = 'Past1', value2 from YourWith
union all
select name = 'Past2', value3 from YourWith
union all
...
答案 1 :(得分:1)
我会以不同的方式进行查询,以便按照您想要的方式提供数据。
(似乎无法弄清楚如何正确格式化以下状态以显示为没有此行的代码...)
SELECT name, COUNT(*)
FROM cpt-prod JOIN Ages on cpt_originated
between Subdate(now - upperBound) And Subdate (now - lowerBound)
GROUP BY name
根据您的需要和dbms,您可以在实际的SQL语句中定义Ages表。 (AFAIK,这不能在Oracle中完成,但也许有人可以在MySQL中添加关于如何执行此操作的注释)。