我有一个SQL可以计算一天中的条件(差,差,更差)并将其分组为一个月并显示如下:
YEARS MONTHS LV COUNTVAL
----- ------ ----- --------
2009 Apr Bad 5
2009 Apr Poor 3
2009 Apr Worse 2
2009 Aug Bad 3
2009 Aug Poor 2
2009 Aug Worse 5
2009 Dec Bad 5
2009 Dec Poor 2
2009 Dec Worse 3
2009 Feb Bad 5
2009 Feb Poor 3
2009 Feb Worse 2
2009 Jan Bad 3
2009 Jan Poor 3
2009 Jan Worse 4
2009 Jul Bad 2
2009 Jul Poor 3
2009 Jul Worse 5
2009 Jun Bad 1
2009 Jun Poor 4
2009 Jun Worse 5
2009 Mar Bad 4
2009 Mar Poor 4
2009 Mar Worse 2
2009 May Bad 3
2009 May Poor 5
2009 May Worse 2
2009 Nov Bad 1
2009 Nov Poor 5
2009 Nov Worse 4
2009 Oct Bad 5
2009 Oct Poor 3
2009 Oct Worse 2
2009 Sep Bad 2
2009 Sep Poor 4
2009 Sep Worse 4
2010 Apr Bad 6
2010 Apr Poor 1
2010 Apr Worse 3
2010 Aug Bad 3
2010 Aug Poor 2
2010 Aug Worse 5
2010 Dec Bad 4
2010 Dec Poor 2
2010 Dec Worse 4
2010 Feb Bad 4
2010 Feb Poor 3
2010 Feb Worse 3
2010 Jan Bad 6
2010 Jan Poor 3
2010 Jan Worse 1
2010 Jul Bad 2
2010 Jul Poor 4
2010 Jul Worse 4
2010 Jun Bad 4
2010 Jun Poor 3
2010 Jun Worse 3
2010 Mar Bad 6
2010 Mar Poor 1
2010 Mar Worse 3
2010 May Bad 4
2010 May Worse 6
2010 Nov Bad 5
2010 Nov Poor 2
2010 Nov Worse 3
2010 Oct Bad 3
2010 Oct Poor 3
2010 Oct Worse 4
2010 Sep Bad 3
2010 Sep Poor 4
2010 Sep Worse 3
我正在使用的SQL:
select years, months, lv, countVal
from
(
select years, months, lv, count (lv) OVER (PARTITION BY years,months,lv) countVal from
(
SELECT x.years, x.months, x.days, x.lv
FROM airtest,
XMLTABLE ('$d/cdata/name' passing xmldoc as "d"
COLUMNS
years integer path 'year',
months varchar(3) path 'month',
days varchar(2) path 'day',
lv varchar(5) path 'value'
) as X
group by x.years, x.months, x.days, x.lv
order by x.years, x.months, x.days
)
)
group by years, months, lv, countVal
order by years,months
现在的问题是如何将其修改成这种格式?
YEARS MONTHS Bad Poor Worse
----- ------ ----- -------- --------
2009 Apr 5 3 2
2009 Aug 3 2 5
2009 Dec 5 2 3
.........
.........
修改后,它会变成这样的东西。
您可以看到每个月将分开的数据分组成一行。
感谢您的帮助!!
答案 0 :(得分:5)
有条件地按年/月汇总到每个列和组:
SELECT years, months,
SUM(DECODE(lv, 'Bad', countval, 0)) Bad,
SUM(DECODE(lv, 'Poor', countval, 0)) Poor,
SUM(DECODE(lv, 'Worse', countval, 0)) Worse
FROM ( <your query> )
GROUP BY years, months
[Gaurav Soni补充以下内容]
或强>
WITH Base_Table AS
( <your query> )
SELECT years, months,
SUM(DECODE(lv, 'Bad', countval, 0)) Bad,
SUM(DECODE(lv, 'Poor', countval, 0)) Poor,
SUM(DECODE(lv, 'Worse', countval, 0)) Worse
FROM base_Table
GROUP BY years, months