我的查询是
SELECT
AIP.TERMID,
(SELECT COUNT(RAU.TERMNAME)
FROM REPORT_API_USAGE RAU
WHERE RAU.TERMID = AIP.TERMID AND RAU.VOCID = 4) AS page_views
FROM REPORT_API_PAGES AIP
GROUP BY AIP.TERMID
我需要获得列page_views
的总和。我怎么才能得到它?请帮忙。 PHP和ORACLE。
我需要总共page_views
为2(1 + 1)。我需要在没有更改现有查询的情况下将其作为新行。
SELECT
AIP.TERMID,
(SELECT COUNT(RAU.TERMNAME)
FROM REPORT_API_USAGE RAU
WHERE RAU.TERMID = AIP.TERMID AND RAU.VOCID = 4) AS page_views,
(SELECT SUM(COUNT(RAU.TERMNAME))
FROM REPORT_API_USAGE RAU
WHERE RAU.VOCID = 4 GROUP BY AIP.TERMID) AS page_views
FROM REPORT_API_PAGES AIP
GROUP BY AIP.TERMID`
我尝试了这一点,但给出了结果4
而不是2
答案 0 :(得分:1)
你想要展示什么?每个TERMID的页面浏览总数?
harist
虽然,根据您当前的查询,甚至不需要加入(除非SELECT aip.TERMID,
COUNT(rau.TERMNAME) AS PAGE_VIEWS
FROM REPORT_API_PAGES aip
JOIN REPORT_API_USAGE rau ON rau.TERMID = aip.TERMID
WHERE rau.VOCID = 4
GROUP BY aip.TERMID
中的行没有REPORT_API_USAGE
中的父项
REPORT_API_PAGES
编辑:所以,根据您的问题更新,如果您只想要总浏览量,可以使用
SELECT rau.TERMID,
COUNT(rau.TERMNAME) AS PAGE_VIEWS
FROM REPORT_API_USAGE rau
WHERE rau.VOCID = 4
GROUP BY rau.TERMID
(连接不是必需的,它允许您只计算SELECT COUNT(rau.TERMNAME) AS page_views
FROM REPORT_API_PAGES aip
JOIN REPORT_API_USAGE rau ON rau.TERMID = aip.TERMID
WHERE rau.VOCID = 4
表中定义TERMID
的页面的页面视图
EDIT2:根据您的第二次更新,我建议您在子查询中使用REPORT_API_PAGES
SUM(PAGE_VIEWS) OVER ()
显然,您也可以使用原始查询替换子查询。
答案 1 :(得分:1)
SELECT AIP.TERMID,
(
select count(RAU.TERMNAME)
FROM
REPORT_API_USAGE RAU
WHERE
RAU.TERMID = AIP.TERMID AND RAU.VOCID = 4
) as page_views
from
REPORT_API_PAGES AIP
GROUP BY
AIP.TERMID
只是尝试将其修改为喜欢这个
select sum(page_views)
(
SELECT AIP.TERMID,
(
select count(RAU.TERMNAME)
FROM
REPORT_API_USAGE RAU
WHERE
RAU.TERMID = AIP.TERMID AND RAU.VOCID = 4
) as page_views
from
REPORT_API_PAGES AIP
GROUP BY
AIP.TERMID
) from dual;