关于SQL查询的错误数据

时间:2015-08-03 05:52:43

标签: sql join

我的要求是从两个表REPORT_API_USAGE&中获取详细信息。 REPORT_API_PAGES。我需要从TERMNAMECOUNT(TERMNAME)获取REPORT_API_USAGECOUNT(CTYPE)eventdownload来自REPORT_API_PAGES。两个表都有公共字段termid。要求是获取TERMNAMECOUNT(TERMNAME)相关事件和文件下载。

enter image description here

enter image description here

enter image description here

TERMID 24 REPORT_API_PAGES CTYPE download只有Face-to-Face Learning 1 0 1 Face-to-Face Learning。所以结果应该是TERMID'

我有. I have added an event and document with reference of页面. Once i viewed 24 page,面对面学习will store the page hit value in面对面学习table. Once the event and document pages are viewed, COUNT(TERMNAME) will store the details with REPORT_API_USAGE reference. I need to create a report of views. Need to get REPORT_API_USAGE for i in g: print(i) TERMID str = str.replace(/,#/g, '#'); 面对面学习`视图计数以及相关事件和文档点击次数。

2 个答案:

答案 0 :(得分:0)

report_api_usage 中有两行 termname ='面对面学习',因此返回两行。

您应该使用:

select ap.termname,
count(distinct ap.termname) as tot,
count(distinct ai.ctype) as event,
sum(case when ai.ctype = 'event' then 1 else 0 end) as page
from ...

我不明白你想要的数量..如果你想要计算多少 repoet_api_pages ctype '事件'或'下载'使用以下查询:

select ap.termname,
count(ap.termname) as tot, -- if it total count rows in table report_api_usage, if you need unique count use distinct inside count function
sum(case when ai.ctype = 'download' then 1 else 0 end) as page_download
sum(case when ai.ctype = 'event' then 1 else 0 end) as page_event
from ...

答案 1 :(得分:0)

Finlay我找到了昂贵的查询。

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 and RAU.TERMID in (select AIP.TERMID from REPORT_API_PAGES AIP GROUP BY AIP.TERMID) group by RAU.VOCID) as page_views_sum, (select sum(count(AIP2.TERMID)) from REPORT_API_PAGES AIP2 join REPORT_API_USAGE RAU ON (RAU.TERMID = AIP2.TERMID AND RAU.VOCID = 4) where aip2.ctype = 'download' GROUP by AIP2.termid) as download_sum, (select sum(count(AIP2.TERMID)) from REPORT_API_PAGES AIP2 join REPORT_API_USAGE RAU ON (RAU.TERMID = AIP2.TERMID AND RAU.VOCID = 4) where aip2.ctype = 'event' GROUP by AIP2.termid) as event_sum, COUNT(case when aip.ctype = 'download' then 1 else null end) as page_download, COUNT(case when aip.ctype = 'event' then 1 else null end) as page_event from REPORT_API_PAGES AIP GROUP BY AIP.TERMID