我想选择区域智能数据,并希望将每个区域显示为列标题。
我的数据:
region state status
North UP invalid
North HAR invalid
North MAH valid
South KER valid
South TAM valid
South KAR valid
South KAR invalid
输出:
Status North south total
invalid 2 1 3
valid 1 3 4
total 3 4 7
怎么做? 这个区域可以稍后更新,以后可以添加新区域
有没有办法先获取所有活动区域,然后在查询中使用它 activeregions
答案 0 :(得分:2)
//select a faq section
faqsCategories.on('click', function(event){
event.preventDefault();
var selectedHref = $(this).attr('href'),
target= $(selectedHref);
if( $(window).width() < MqM) {
faqsContainer.scrollTop(0).addClass('slide-in').children('ul').removeClass('selected').end().children(selectedHref).addClass('selected');
closeFaqsContainer.addClass('move-left');
$('body').addClass('overlay');
} else {
$('body,html').animate({ 'scrollTop': target.offset().top - 69}, 200);
}
});
替代解决方案,可以处理以后添加的新区域:
select status,
count(case when region = 'North' then 1 end) as North,
count(case when region = 'South' then 1 end) as South
from tablename
group by status
另一个解决方案,也可以处理以后添加的新区域:
select region, status, count(*)
from tablename
group by region, status
答案 1 :(得分:0)
SQL旨在通过之前已知的列来获取记录。因此,您无法直接获得所需输出格式的数据。主要有两种选择:
1)选择行中的聚合数据:
select region, status, count(*)
from mytable
group by region, status
order by region, status;
然后让您的应用以列显示数据。 (循环和获取数据,根据数据在GUI网格中构建列,即每次获得新区域时在网格中添加新列。)
2)首先选择不同的区域,然后选择数据:
select distinct region
from mytable;
现在,你得到了北方的&#39;和&#39; South&#39;让您的应用动态构建以下查询:
select status as "Status"
, sum(case when region = 'North' then 1 else 0 end) as "North"
, sum(case when region = 'South' then 1 else 0 end) as "South"
from mytable
group by status;
答案 2 :(得分:0)
方法1 请检查SQLFIDDLE
FIRST STEP
单独查找活动和非活动区域的计数
SELECT 'ACTIVE' "STATUS",NVL(REGION,'TOTAL') REGION,
COUNT(REGION) REGCOUNT
FROM TEMPPIVOTTEST
where STATUS like 'ACTIVE'
GROUP BY ROLLUP (REGION)
SELECT 'INACTIVE' "STATUS",NVL(REGION,'TOTAL') REGION,
COUNT(REGION) REGCOUNT
FROM TEMPPIVOTTEST
where STATUS like 'INACTIVE'
GROUP BY ROLLUP (REGION)
在上面的查询中我们使用GROUP By ROLLUP,所以最后一行我们将获得所有其他行的总和
SECOND STEP
加入此查询 并使用Pivot
SELECT NVL(STATUS,'GRAND TOTAL') STATUS,SUM(EAST) EAST,SUM(NORTH) NORTH,SUM(SOUTH) SOUTH,SUM(TOTAL) TOTAL from
( SELECT 'ACTIVE' "STATUS",NVL(REGION,'TOTAL') REGION,
COUNT(REGION) REGCOUNT
FROM TEMPPIVOTTEST
where STATUS like 'ACTIVE'
GROUP BY ROLLUP (REGION)
union
SELECT 'INACTIVE' "STATUS",NVL(REGION,'TOTAL') REGION,
COUNT(REGION) REGCOUNT
FROM TEMPPIVOTTEST
where STATUS like 'INACTIVE'
GROUP BY ROLLUP (REGION)
)pivot (SUM(REGCOUNT) FOR (REGION) IN ('EAST' EAST,'NORTH' NORTH,'SOUTH' SOUTH,'TOTAL' TOTAL))
GROUP BY ROLLUP (STATUS)
在枢轴中,我们需要给出表格中的区域
方法2 SQLFIDDLE
另一种方式用于执行相同的上述查询我们已将状态指定为“有效”和“无效”,而不是我们可以在查询后使用的状态
SELECT nvl(STATUS,'GRAND_TOTAL') STATUS,sum(EAST)EAST,sum(NORTH)NORTH,sum(SOUTH)SOUTH,sum(TOTAL) TOTAL from
(SELECT STATUS,REGION,
COUNT(REGION) REGCOUNT
FROM TEMPPIVOTTEST
GROUP BY (STATUS,REGION)
union
SELECT STATUS,'TOTAL'REGION,
COUNT(REGION) REGCOUNT
FROM TEMPPIVOTTEST
GROUP BY (STATUS)
)pivot (SUM(REGCOUNT) FOR (REGION) IN ('EAST' EAST,'NORTH' NORTH,'SOUTH' SOUTH,'TOTAL' Total))
group by rollup (status)
oracle 10 g pivot将无法工作,因此我们可以使用case语句
select nvl(status,'total')status,SUM(NORTH) NORTH,SUM(SOUTH)SOUTH,SUM(EAST)EAST,sum(TOTAL)TOTAL from (select status,
sum(case when region = 'NORTH' then 1 else 0 end) as "NORTH",
sum(case when region = 'SOUTH' then 1 else 0 end) as "SOUTH",
sum(case when region = 'EAST' then 1 else 0 end) as "EAST",
sum(case when region in('') then 0 else 1 end) as "TOTAL"
from TEMPPIVOTTEST
group by status
)group by ROLLUP (STATUS)