获取NonUniqueDiscoveredSqlAliasException

时间:2017-10-26 08:54:33

标签: mysql sql hibernate

表结构如下

+ --------------- + ---------- + ------------------- + ------------- + --------- + -------- + ---------------- --------- + ---------------- + ---------------- + ------ ----------------- + ---------------------- + --------- ------------ + ------------------- + -------------- +

  

| REDEMPTION_ID | CCID | MEMBERSHIP_NUMBER | POINTS_TYPE |伙伴   |计划| REDEMPTION_ORDER_STATUS | MEMBER_SEGMENT | PARTNER_POINTS |   MEMBERSHIP_FIRST_NAME | MEMBERSHIP_LAST_NAME | REDEMPTION_DATE |   OUTBOUND_FILENAME | PRODUCT_TYPE |

+ --------------- + ---------- + -------- ----------- ------------- + + --------- + -------- + ----- -------------------- + ---------------- + ------------ ---- + ----------------------- + --------------------- - + --------------------- + ------------------- + ------ -------- +

  

| 1003740 | 21212103 | 1231237 | BASE | QANTAS   |签证|订购| LEGACY | 5000.000000 |   e |名称| 2017-10-23 10:26:51 |   NABQF05P.012 |消费者|

     

| 1003741 | 21212103 | 1231238 |奖金| QANTAS |签证|订购| LEGACY | 2500.000000 | e |名称
  | 2017-10-23 10:26:51 | NABQF05P.012 |消费者|

我想根据Membership_Number和POINTS_TYPE列对上述行进行分组,结果行应该是一行。

我使用以下查询:

    select * from ((
    select * from NAB_REDEMPTION_DETAILS 
where PARTNER='QANTAS' and REDEMPTION_ORDER_STATUS IN ('PLACED','RESEND') and POINTS_TYPE  = 'BASE' group by MEMBERSHIP_NUMBER) a 
left OUTER JOIN (
        select * from NAB_REDEMPTION_DETAILS 
where PARTNER='QANTAS' and REDEMPTION_ORDER_STATUS IN ('PLACED','RESEND') and POINTS_TYPE  = 'BONUS' group by MEMBERSHIP_NUMBER) b on a.MEMBERSHIP_NUMBER=b.MEMBERSHIP_NUMBER) union (
        select * from (
        select * from NAB_REDEMPTION_DETAILS 
where PARTNER='QANTAS' and REDEMPTION_ORDER_STATUS IN ('PLACED','RESEND') and POINTS_TYPE  = 'BASE' group by MEMBERSHIP_NUMBER) c right OUTER JOIN (
        select * from NAB_REDEMPTION_DETAILS 
where PARTNER='QANTAS' and REDEMPTION_ORDER_STATUS IN ('PLACED','RESEND') and POINTS_TYPE  = 'BONUS' group by MEMBERSHIP_NUMBER) d on c.MEMBERSHIP_NUMBER=d.MEMBERSHIP_NUMBER)

执行查询时,我得到上述异常

1 个答案:

答案 0 :(得分:0)

我明白了。因为我在同一个表中使用连接而Redemption_id是一个主键,所以导致异常。我使用group_concat而不是join,它解决了我的问题。

请在链接http://sqlfiddle.com/#!9/dc59a1/5

中找到解决方案