0

MDW_BILL1有以下列

BILL_ID NOT NULL VARCHAR2(10)
CUSTOMER_ID VARCHAR2(10)
MONTH VARCHAR2(10)
YEAR NUMBER
TOTAL NUMBER
ACTIVE_INDICATOR VARCHAR2(10)

MDW_BILL_TRANSACTION1具有以下列:

BILL_TRANSACTION_ID NOT NULL VARCHAR2(10)
BILL_ID VARCHAR2(10)
ACCOUNT_ID VARCHAR2(10)
TRANSACTION_TYPE VARCHAR2(20)
COST NUMBER
ACTIVE_INDICATOR VARCHAR2(10)

这是我要执行的查询:

select 
    b.ACCOUNT_ID, b.TRANSACTION_TYPE, b.cost 
from 
    mdw_bill1 a, mdw_bill_transaction1 b 
where 
    a.CUSTOMER_Id='CUS0033' 
    and a.month='JUN' and a.year=2013;

o/p 是:

ACCOUNT_ID TRANSACTION_TYPE           COST
---------- -------------------- ----------
AID0159    Transport Cost              182
AID0159    Meal Cost                  1300
AID0159    Subscription Cost           120
AID0161    Transport Cost              168
AID0161    Meal Cost                   840
AID0161    Subscription Cost           240
AID0160    Transport Cost           203.58
AID0160    Meal Cost                   650
AID0160    Subscription Cost           400
AID0164    Transport Cost           187.92
AID0164    Meal Cost                   720
AID0164    Subscription Cost           480

如何删除重复ACCOUNT_ID并为每组计费详细信息显示一个 account_id

我将代码修改为

select b.ACCOUNT_ID, b.TRANSACTION_TYPE, b.cost from mdw_bill1 a
    inner join mdw_bill_transaction1 b
    on a.bill_id = b.bill_id
    where a.CUSTOMER_Id='CUS0033'
    and a.month='JUN' and a.year=2013;

但我仍然得到相同的结果

4

1 回答 1

1

对此的解决方案是枚举正在输出的行,并且仅在每个组的第一行中包含帐户 ID:

select (case when seqnum = 1 then Account_Id else '' end),
       Transaction_type, cost
from (select b.ACCOUNT_ID, b.TRANSACTION_TYPE, b.cost,
             row_number() over (partition by account_id
                                order by b.bill_transacction_id) as seqnum
      from mdw_bill1 a, mdw_bill_transaction1 b 
      where a.CUSTOMER_Id='CUS0033'and a.month='JUN' and a.year=2013
     ) t
order by Account_id, seqnum

一些评论。您应该为您的表格提供合理的别名,例如bformdw_bill1btfor mdw_bill_transaction。您的原始查询没有order by子句;你需要它来保证行的顺序——不能保证一个帐户总是在相邻的行上。第三,以这种方式连接两个表看起来。. . 我会说尴尬吗?我认为您打算将条件设为:

      from mdw_bill1 a join
           mdw_bill_transaction1 b 
           on a.bill_id = b.bill_id
      where a.CUSTOMER_Id='CUS0033'and a.month='JUN' and a.year=2013
于 2013-06-08T22:49:34.353 回答