我需要编写一个可以执行多项操作的状态。
这是第一个选择计数的选项,它可以正常工作。
select PLANNED
from (SELECT count(FACT.EVENT) AS PLANNED
FROM FACT FACT
WHERE FACT.PLANNEDOTGFLAG = 1
AND FACT.STARTDATETIME >= SYSDATE - 365
)
我需要编写另一个语句,该语句将显示来自同一个表示例的不同信息。
select count(effected)
from fact
where startime between 01/01/2013 and 01/02/2013
我想知道我是否可以做这样的事情
select PLANNED,
Count_EFFECTED
from ( SELECT count(FACT.EVENT) AS PLANNED
FROM FACT FACT
WHERE FACT.PLANNEDOTGFLAG = 1
AND FACT.STARTDATETIME >= SYSDATE - 365
union all
select count(FACT.effected) AS Count_EFFECTED
from fact fact
where fact.startime between 01/01/2013 and 01/02/2013
)
第三次嵌套查询
SELECT
A.PLANNED,
B.effec,
C.XOUND // not working yet.
FROM
(SELECT count(FACT.EVENT) AS PLANNED FROM FACT FACT
WHERE FACT.PLANNEDOTGFLAG = 1 AND FACT.STARTDATETIME >= SYSDATE - 365) A,
(select count(FACT.effected) AS effec from fact fact
WHERE FACT.STARTDATETIME between 01/01/2013 and 01/02/2013) B
//how can I add 3rd select which is nested.
(select round(FACT.ID) AS XOUND
FROM
(SELECT SUM(FACT.CIM)/SUM(FACT.CUST) AS ID
FROM FACT FACT
WHERE FACT.STARTDATETIME between 01/01/2013 and 01/02/2013
AND OTGFLAG = 1 AND PLANNEDOTGFLAG = 0 )) C
答案 0 :(得分:1)
由于您从同一个表中获得两个不同的计数,我建议使用案例陈述:
SELECT
Planned_Outages = COUNT(CASE WHEN Fact.PlannedOTGFlag = 1 AND Fact.StartDateTime >= Sysdate - 365 THEN Fact.Event END),
Effec = COUNT(CASE WHEN FactStartTime between '01/01/2013' and '01/02/2013' THEN Fact.Effected END)
FROM
Fact
答案 1 :(得分:1)
您可以使用CASE
表达式重写SQL语句:
select count(case
when plannedotgflag = 1 AND
startdatetime >= SYSDATE - 365
then 1
end
) as planned
, count(case
when startime between to_date('01/01/2013', 'dd/mm/yyy') and
to_date('01/02/2013', 'dd/mm/yyy')
then 1
end
) as effected
from fact
答案 2 :(得分:1)
你的第二个查询会给你一个错误,因为导致FROM子句的表只会返回一列(2行),第一行会有PLANNED_OUTAGES
而第二行会有effect
计数。
根据您的要求,您可以将查询更改为:
包含第三个查询:
SELECT A.PLANNED_OUTAGES,B.effec, C.ID
FROM (SELECT count(FACT.EVENT) AS PLANNED
FROM FACT FACT
WHERE FACT.PLANNEDOTGFLAG = 1 AND FACT.STARTDATETIME >= SYSDATE - 365) A,
(select count(FACT.effected) AS effec
from fact fact
WHERE FACT.STARTDATETIME between 01/01/2013 and 01/02/2013) B,
(SELECT ROUND(SUM(FACT.CIM)/SUM(FACT.CUST)) AS ID
FROM FACT FACT
WHERE FACT.STARTDATETIME between 01/01/2013 and 01/02/2013
AND OTGFLAG = 1 AND PLANNEDOTGFLAG = 0
GROUP BY FACT.CIM,FACT.CUST) C