现有表格t:
namz varchar2(255), sumz number, datez date
-------------------------------------------
John 100.50 01.01.2016
Ivan 200.45 02.02.2014
...
John 400.32 03.03.2016
我是否可以通过使用简单的选择来获得定义期间的月度销售分组:
select namz, .... from t where datez>date1 and datez<=date2 and ....
得到如下结果:
namz 01.2014 02.2014 03.2014 .... 03.2016
-------------------------------------------
John null 1200.34 234.34 ... null
Ivan 1234.45 null null ... 254.23
提前谢谢!
答案 0 :(得分:1)
使用数据集
CREATE TABLE tmp AS
(SELECT 'John' name, 100.5 Sumz, '01-Jan-16' datez FROM dual UNION
SELECT 'Ivan' name, 200.45 Sumz, '02-Feb-14' datez FROM dual UNION
SELECT 'John' name, 400.32 Sumz, '03-Mar-16' datez FROM dual UNION
SELECT 'John' name, 200.0 Sumz, '03-Mar-16' datez FROM dual UNION
SELECT 'John' name, 8475.36 Sumz, '01-Jan-18' datez FROM dual UNION
SELECT 'Ivan' name, 8759.36 Sumz, '03-Mar-16' datez FROM dual)
;
使用pivot语句
select * FROM
(
SELECT name, sumz, datez
FROM tmp
)
PIVOT
(
Sum(sumz)
FOR datez IN ('01-Jan-16','02-Feb-14','03-Mar-16', '01-Jan-18')
)
ORDER BY name;
它可以为您提供您想要的内容
答案 1 :(得分:0)
您可以使用YEAR
,MONTH
和CONCAT
这样的功能:
SELECT namz, CONCAT(MONTH(datez),'.',YEAR(datez)) as d, SUM(something) as s
FROM t
WHERE datez>date1
AND datez<=date2
GROUP BY namz, d
结果如下:
namez d s
---------------------
Ivan 01.2014 1234.45
John 02.2014 1200.34
...
答案 2 :(得分:0)
SELECT namz,
SUM( CASE WHEN datez >= DATE '2014-01-01'
AND datez < DATE '2014-02-01'
THEN sumz END ) AS "01.2014",
SUM( CASE WHEN datez >= DATE '2014-02-01'
AND datez < DATE '2014-03-01'
THEN sumz END ) AS "02.2014",
SUM( CASE WHEN datez >= DATE '2014-03-01'
AND datez < DATE '2014-04-01'
THEN sumz END ) AS "03.2014"
-- ...
FROM t
WHERE datez > :date1
AND datez <= :date2
GROUP BY namz