选择查询中的显示列未在group by子句中添加

时间:2017-10-15 12:29:12

标签: sql oracle oracle11g

我被困在创建一个SQL查询。

表:

XX_AR_RECEIPT_CREATE

列:

RECEIPT_ID                 
ENTITY                    
LOG_ID                    
ACTIVITY_ID               
FIN_CLASS                 
RECEIPT_METHOD            
RECEIPT_AMOUNT            
RECEIPT_TYPE              
RECEIPT_NUMBER     
RECEIPT_DATE              
POSTED_DATE               
ACTIVITY_DATE             
SERVICE_DATE              
CASHIER_NAME              
CASHIER_USERNAME          
PAYMENT_REFERENCE_NUMBER  
DEPARTMENT                
SPECIALITY                
ACCOUNT                   
STATUS                    

这是我的查询

select to_char(sum(to_number(receipt_amount))) amount,entity, cashier_name, 
       receipt_method, fin_class,to_char(to_date(posted_date, 'dd/mm/yy HH24:MI:SS'), 'dd/mm/yy') as receipt_date
from XX_AR_RECIPT_CREATE
where to_number(receipt_amount)>0 and status='0'
group by entity, cashier_name, receipt_method,
         fin_class, to_char(to_date(posted_date,'dd/mm/yy HH24:MI:SS'), 'dd/mm/yy');

它工作正常,问题是我想添加一些额外的列示例LOG_ID。但我无法在Group By子句中添加它。 EG:

select to_char(sum(to_number(receipt_amount))) amount, entity, cashier_name, 
       receipt_method, fin_class,to_char(to_date(posted_date, 'dd/mm/yy HH24:MI:SS'), 'dd/mm/yy') as receipt_date,
       LOG_ID
from XX_AR_RECIPT_CREATE
where to_number(receipt_amount) > 0 and status = '0'
group by entity, cashier_name, 
         receipt_method, fin_class, to_char(to_date(posted_date, 'dd/mm/yy HH24:MI:SS'), 'dd/mm/yy');

我也在互联网上搜索解决方案。内连接是一个很好的解决方案。但问题是我使用to_char()所以我无法添加别名。我使用的是Oracle Database 11g Express Edition 11.2.0.2.0版 - 64位生产

1 个答案:

答案 0 :(得分:1)

您需要决定所需的LOG_ID。例如,要获得最低要求:

select to_char(sum(to_number(receipt_amount))) as amount, entity, cashier_name, 
       receipt_method, fin_class,
       to_char(to_date(posted_date, 'dd/mm/yy HH24:MI:SS'), 'dd/mm/yy') as receipt_date,
       min(LOG_ID) as LOG_ID

或者获取它们的列表(不会太长):

select to_char(sum(to_number(receipt_amount))) as amount, entity, cashier_name, 
       receipt_method, fin_class,
       to_char(to_date(posted_date, 'dd/mm/yy HH24:MI:SS'), 'dd/mm/yy') as receipt_date,
       listagg(LOG_ID, ',') within group (order by LOG_ID) as LOG_IDs