2

我想按区域(定义为 501,502,503...)以及按年和月汇总数据值。年份和月份来自数据库中的日期字段。所以实际上我想得到以下信息,但我每天都会得到每个区域的总数:

Area   Date      Value
501    10-2013   100000
501    11-2013   120000
502    10-2013   200000
502    11-2013   222000
...

这是SQL。

SELECT BUSINESS_ENTITY.BUSINESS_ENTITY_NAME, to_char(DATA_POINT_DETAIL.EFF_DATE,'MM-YYYY') as TONNAGE_DATE, sum(DATA_POINT_DETAIL.NUMERIC_VALUE) as ACTUAL

 FROM   OIS.BUSINESS_ENTITY BUSINESS_ENTITY INNER JOIN (((OIS.DATA_POINT_DETAIL DATA_POINT_DETAIL INNER JOIN OIS.DATA_POINT_HEADER DATA_POINT_HEADER ON DATA_POINT_DETAIL.DATA_POINT_ID=DATA_POINT_HEADER.DATA_POINT_ID) INNER JOIN OIS.CN_DATA_POINT_BUSINESS_ENTITY CN_DATA_POINT_BUSINESS_ENTITY ON DATA_POINT_HEADER.DATA_POINT_ID=CN_DATA_POINT_BUSINESS_ENTITY.DATA_POINT_ID) INNER JOIN OIS.BUSINESS_ENTITY_ANCESTOR BUSINESS_ENTITY_ANCESTOR ON (CN_DATA_POINT_BUSINESS_ENTITY.BUSINESS_ENTITY_ID=BUSINESS_ENTITY_ANCESTOR.BUSINESS_ENTITY_ID) AND (CN_DATA_POINT_BUSINESS_ENTITY.HIERARCHY_ID=BUSINESS_ENTITY_ANCESTOR.HIERARCHY_ID)) ON BUSINESS_ENTITY.BUSINESS_ENTITY_ID=BUSINESS_ENTITY_ANCESTOR.BUSINESS_ENTITY_ID

WHERE DATA_POINT_DETAIL.EFF_DATE>={?BeginDate} and  DATA_POINT_DETAIL.EFF_DATE<={?EndDate}
AND BUSINESS_ENTITY_ANCESTOR.BUSINESS_ENTITY_ANCESTOR_ID = 57570 
AND (DATA_POINT_HEADER.DATA_CATEGORY_NAME ='Tons Cut') and DATA_POINT_DETAIL.NUMERIC_VALUE<>0 and BUSINESS_ENTITY.BUSINESS_ENTITY_NAME<>'Ore'

group by BUSINESS_ENTITY.BUSINESS_ENTITY_NAME, DATA_POINT_DETAIL.EFF_DATE

我无法为 TONNAGE_DATE 字段按字段分组。我尝试了许多不同的变体,但都失败了。

这是从 Crystal Report 在 ORACLE 数据库上运行的。

任何帮助表示赞赏,

德怀特

4

2 回答 2

7

group by将您的条款更改为:

group by BUSINESS_ENTITY.BUSINESS_ENTITY_NAME,
         to_char(DATA_POINT_DETAIL.EFF_DATE,'MM-YYYY');

您的查询按日期(大概按天)分组,然后将输出格式化为具有月份和年份。

于 2013-10-31T18:12:36.050 回答
1

您必须按 分组to_char(DATA_POINT_DETAIL.EFF_DATE,'MM-YYYY'),而不仅仅是DATA_POINT_DETAIL.EFF_DATE,试试这个:

SELECT BUSINESS_ENTITY.BUSINESS_ENTITY_NAME, to_char(DATA_POINT_DETAIL.EFF_DATE,'MM-YYYY') as TONNAGE_DATE, sum(DATA_POINT_DETAIL.NUMERIC_VALUE) as ACTUAL

 FROM   OIS.BUSINESS_ENTITY BUSINESS_ENTITY INNER JOIN (((OIS.DATA_POINT_DETAIL DATA_POINT_DETAIL INNER JOIN OIS.DATA_POINT_HEADER DATA_POINT_HEADER ON DATA_POINT_DETAIL.DATA_POINT_ID=DATA_POINT_HEADER.DATA_POINT_ID) INNER JOIN OIS.CN_DATA_POINT_BUSINESS_ENTITY CN_DATA_POINT_BUSINESS_ENTITY ON DATA_POINT_HEADER.DATA_POINT_ID=CN_DATA_POINT_BUSINESS_ENTITY.DATA_POINT_ID) INNER JOIN OIS.BUSINESS_ENTITY_ANCESTOR BUSINESS_ENTITY_ANCESTOR ON (CN_DATA_POINT_BUSINESS_ENTITY.BUSINESS_ENTITY_ID=BUSINESS_ENTITY_ANCESTOR.BUSINESS_ENTITY_ID) AND (CN_DATA_POINT_BUSINESS_ENTITY.HIERARCHY_ID=BUSINESS_ENTITY_ANCESTOR.HIERARCHY_ID)) ON BUSINESS_ENTITY.BUSINESS_ENTITY_ID=BUSINESS_ENTITY_ANCESTOR.BUSINESS_ENTITY_ID

WHERE DATA_POINT_DETAIL.EFF_DATE>={?BeginDate} and  DATA_POINT_DETAIL.EFF_DATE<={?EndDate}
AND BUSINESS_ENTITY_ANCESTOR.BUSINESS_ENTITY_ANCESTOR_ID = 57570 
AND (DATA_POINT_HEADER.DATA_CATEGORY_NAME ='Tons Cut') and DATA_POINT_DETAIL.NUMERIC_VALUE<>0 and BUSINESS_ENTITY.BUSINESS_ENTITY_NAME<>'Ore'

group by BUSINESS_ENTITY.BUSINESS_ENTITY_NAME, to_char(DATA_POINT_DETAIL.EFF_DATE,'MM-YYYY')
于 2013-10-31T18:12:31.463 回答