我想按区域(定义为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数据库上运行。
感谢任何帮助,
德怀特
答案 0 :(得分:7)
将您的group by
条款更改为:
group by BUSINESS_ENTITY.BUSINESS_ENTITY_NAME,
to_char(DATA_POINT_DETAIL.EFF_DATE,'MM-YYYY');
您的查询按日期分组(按日推算),然后将输出格式化为月份和年份。
答案 1 :(得分: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')