Oracle查询选择总和计数

时间:2016-08-23 20:08:14

标签: oracle

我有来自下面查询的数据,看起来像下面的第一个表,如果您观察结果是基于count desc。我想以第二个表的形式显示数据,我遇到了查询问题。也就是说,如果pagetype是详细信息,那么我想要对该ID的计数求和,如果pagetype是singe-item,我想不管它,并按所有结果排序desc。 第一个查询要点如下所示,我还有很多其他内容,但这里有一个简化版本。

SELECT id, title, count(id) as count_num , pagetype
, ROW_NUMBER() OVER ( ORDER BY count(id) desc ) AS the_row 
FROM table1, table2
where  pagetype in ('details','items','single-item')
and table1.id = table2.id 
AND ct.PAGE_VIEW_DT > sysdate - 90
ORDER BY the_row


ID             Title                     Count        pagetype
--------------------------------------------------------------
33969   TITLE ONE                         523         details
33969   TITLE ONE                         494          items
198068  TITLE THREE                       400       single-item
33968   TITLE TWO                         395         details
198068  TITLE THREE                       391          items
198068  TITLE THREE                       333          items
198068  TITLE THREE                       281         details
33969   TITLE ONE                         280       single-item
33968   TITLE TWO                         270       single-item


ID             Title                     Count        pagetype
--------------------------------------------------------------
33969   TITLE ONE                        1017   details,items
198068  TITLE THREE                      1005   details,items
198068  TITLE THREE                       400   single-item
33968   TITLE TWO                         395   details
33969   TITLE ONE                         280   single-item
33968   TITLE TWO                         270   single-item

2 个答案:

答案 0 :(得分:1)

考虑:条件聚合。由于我在这里添加了一个组ID,Title将事物分开,我将pagetype中的值更改为详细信息,项目遇到详细信息或项目时,从而允许group by工作。我认为嵌套替换也可以使group by按照需要工作。

SELECT id, title, count(id) as count_num
     , case when pagetype in ('details','items') 
            then 'details,items' 
            else pagetype end as pagetype
    -- , ROW_NUMBER() OVER ( ORDER BY count(id) desc ) AS the_row
FROM table1, table2
where  pagetype in ('details','items','single-item') 
AND ct.PAGE_VIEW_DT > sysdate - 90
GROUP BY  ID, Title, case when pagetype in ('details','items') 
                          then 'details,items' 
                          else pagetype end
ORDER BY --the_row
         count(ID) DESC;

---重读和重读问题后尝试两个...... 感谢@АнатолийПредеин为with语句节省了一些时间......

with a(ID,Title,Count,pagetype) as (
-- here your data from question
select 33969,   'TITLE ONE', 523, 'details' from dual union all
select 33969,  'TITLE ONE', 494, 'items' from dual union all
select 198068,  'TITLE THREE', 400,'single-item' from dual union all
select 33968,  'TITLE TWO', 395,'details' from dual union all
select 198068,  'TITLE THREE', 391, 'items' from dual union all
select 198068,  'TITLE THREE', 333,'items' from dual union all
select 198068, 'TITLE THREE', 281,'details' from dual union all
select 33969,  'TITLE ONE', 280,'single-item' from dual union all
select 33968,  'TITLE TWO', 270,'single-item' from dual ),
with a(ID,Title,Count,pagetype) as (
-- here your data from question
select 33969,   'TITLE ONE', 523, 'details' from dual union all
select 33969,  'TITLE ONE', 494, 'items' from dual union all
select 198068,  'TITLE THREE', 400,'single-item' from dual union all
select 33968,  'TITLE TWO', 395,'details' from dual union all
select 198068,  'TITLE THREE', 391, 'items' from dual union all
select 198068,  'TITLE THREE', 333,'items' from dual union all
select 198068, 'TITLE THREE', 281,'details' from dual union all
select 33969,  'TITLE ONE', 280,'single-item' from dual union all
select 33968,  'TITLE TWO', 270,'single-item' from dual )

Select ID, Title
     , sum(count)
     , case when pageType in ('details','items') then 'details,items' else pagetype end as PageType
FROM a
Group by ID, Title     , case when pageType in ('details','items') then 'details,items' else pagetype end 
order by sum(count) desc

在初始查询中使用条件聚合可能有一种更简单的方法...

类似

SELECT id
     , title
     , count(id) as count_num 
     , case when pagetype in ('details','items') 
            then 'details,items' 
            else pagetype end as pagetype,
FROM table1
INNER JOIN  table2
  on  table1.id = table2.id 
WHERE pagetype in ('details','items','single-item')
  AND ct.PAGE_VIEW_DT > sysdate - 90
GROUP BY ID
       , Title
       , case when pagetype in ('details','items')
              then 'details,items' 
              else pagetype end 
ORDER BY count(id) desc

但如果没有问题中未提供的原始源数据,我无法测试它。 (而且我不想嘲笑一些!)

答案 1 :(得分:0)

您可以像这样使用双listagg和oracle decode以及oracle with a(ID,Title,Count,pagetype) as ( -- here your data from question select 33969, 'TITLE ONE', 523, 'details' from dual union all select 33969, 'TITLE ONE', 494, 'items' from dual union all select 198068, 'TITLE THREE', 400,'single-item' from dual union all select 33968, 'TITLE TWO', 395,'details' from dual union all select 198068, 'TITLE THREE', 391, 'items' from dual union all select 198068, 'TITLE THREE', 333,'items' from dual union all select 198068, 'TITLE THREE', 281,'details' from dual union all select 33969, 'TITLE ONE', 280,'single-item' from dual union all select 33968, 'TITLE TWO', 270,'single-item' from dual ) select id, title, sum(scount) scount, listagg(pagetype,',') WITHIN group (order by pagetype) from ( select id, title, sum(count) scount, pagetype from a group by id, title, pagetype ) group by id, title, decode(pagetype,'details','tosum','items','tosum',pagetype) order by scount desc 函数(此查询基于您查询中的数据):

33969   TITLE ONE   1017    details,items
198068  TITLE THREE 1005    details,items
198068  TITLE THREE 400     single-item
33968   TITLE TWO   395     details
33969   TITLE ONE   280     single-item
33968   TITLE TWO   270     single-item

此查询的输出:

with a as (
SELECT id, title, count(id) as count_num , pagetype
, ROW_NUMBER() OVER ( ORDER BY count(id) desc ) AS the_row 
FROM table1, table2
where  pagetype in ('details','items','single-item') 
AND ct.PAGE_VIEW_DT > sysdate - 90
)

select id, title, sum(scount) scount, listagg(pagetype,',') WITHIN group (order by pagetype) ptype
  from (
select id, title, sum(count_num) scount, pagetype
  from a 
group by id, title, pagetype
)
group by id, title, decode(pagetype,'details','tosum','items','tosum',pagetype)
order by scount desc
您的查询中的

可能看起来像这样

TITLE THREE

它的双重分组是因为'details,items,items'中的listagg concatinate与'details,items'重复的字符串而不是你想要group by这就是为什么我使用双ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity"); //This loops through the results from the searcher foreach (ManagementObject queryObj in searcher.Get()) { //If it finds the port, if (queryObj["Caption"].ToString().Contains("##### Wireless AT")) { //it writes it to the file sw.WriteLine("serial port : {0}", queryObj["Caption"] + "\n"); sw.Flush(); }