oracle group通过返回多一行相同的值

时间:2015-05-27 14:09:43

标签: sql oracle group-by

我的查询使用group by返回相同的值。任何人都可以向我解释为什么它就像那样或我做错了什么。

QUERY:

SELECT ID, LAST_DAY(EXPDATE) EXPDATE, SUM(BASEUNITS) AS BASEUNITS, SUM(BONUSUNITS) AS BONUSUNITS
FROM TABLE
WHERE ID= '10'
GROUP BY ID, EXPDATE

结果:

       ID EXPDATE                BASEUNITS  BONUSUNITS  
       10 2016/04/30 12:00:00 AM    8560       0 
       10 2016/04/30 12:00:00 AM    8560       0

2 个答案:

答案 0 :(得分:0)

虽然这是不可能的,因为主密钥无法实现Oracle冗余。但是当你得到这个时,你可以在Id之前使用distinct

SELECT Distinct ID, LAST_DAY(EXPDATE) EXPDATE, SUM(BASEUNITS) AS BASEUNITS, SUM(BONUSUNITS) AS BONUSUNITS
FROM TABLE
WHERE ID= '10'
GROUP BY EXPDATE

答案 1 :(得分:0)

您正在使用Last_day()函数并且它不是聚合函数,所以在groupby中您应该指定分组函数

SELECT ID, 
       LAST_DAY(EXPDATE) EXPDATE, 
       SUM(BASEUNITS) AS BASEUNITS,
       SUM(BONUSUNITS) AS BONUSUNITS
FROM TABLE
WHERE ID= '10'
GROUP BY ID, Last_Day(EXPDATE)