简体   繁体   English

如何进行合并重复的主键的SELECT

[英]how to make a SELECT that merges duplicated Primary Keys

I'm performing a query that looks like this: 我正在执行如下查询:

SELECT a.transactionID,a.customerID,b.value      
        FROM adjustments a 
                INNER JOIN change b 
                        on  a.transactionID = b.transactionID 
                        and a.event_date    = b.event_date 
                        and a.event_id      = b.event_id 
        WHERE comment LIKE 'TRANSFER'
        ORDER BY a.transactionID;

this query brings the following result: 该查询带来以下结果:

    transactionID | customerID | value
   ------------------------------------     
    TRANSFER-001  |    CUSTA   | -200
    TRANSFER-001  |    CUSTB   |  200
    TRANSFER-002  |    CUSTC   | -150
    TRANSFER-002  |    CUSTD   |   0
    TRANSFER-003  |    CUSTA   |   0
    TRANSFER-003  |    CUSTC   |  150

I need to change this query to bring a list that ignore those cases where the sum of value is 0 for the same transactionID and also, group the customerID and values as following: 我需要更改此查询以带来一个列表,该列表将忽略那些对于相同transactionIDvalue之和为0的情况 ,并且将customerID和值分组如下:

   transactionID | customerID_A | value_A | customerID_B | value_B
 ------------------------------------------------------------------     
    TRANSFER-002 |    CUSTC     |   -150  |    CUSTD     |    0
    TRANSFER-003 |    CUSTA     |      0  |    CUSTC     |  150

Can you give any advise about how to solve this? 您能提供任何有关如何解决此问题的建议吗?

Try this : 尝试这个 :

SELECT * FROM (
SELECT a.transactionID,a.customerID,b.value      
    FROM adjustments a 
    INNER JOIN change b 
                    on  a.transactionID = b.transactionID 
                    and a.event_date    = b.event_date 
                    and a.event_id      = b.event_id 
    WHERE comment LIKE 'TRANSFER'
)M
GROUP BY transactionID,customerID,value
HAVING SUM(value) <> 0
ORDER BY transactionID;
SELECT distinct a.transactionID,a.customerID,b.value      
    FROM adjustments a 
            INNER JOIN change b 
                    on  a.transactionID = b.transactionID 
                    and a.event_date    = b.event_date 
                    and a.event_id      = b.event_id 
    WHERE comment LIKE 'TRANSFER'
    ORDER BY a.transactionID;

Try distinct at the beginning it will eliminate all the duplicate values. 在开始时尝试使用distinct,它将消除所有重复的值。

If I understand correctly, you want conditional aggregation. 如果我理解正确,则需要条件聚合。 However, you need to pivot the customers and there is no column for doing that. 但是,您需要引导客户,并且没有专栏来做。 You can enumerate the customers for each transaction using variables, and use that to pivot the first two customers on the transaction: 您可以使用变量枚举每个交易的客户,并使用它来轮换交易的前两个客户:

SELECT transactionId,
       MAX(CASE WHEN seqnum = 1 THEN customerId END) as customer_A,
       MAX(CASE WHEN seqnum = 1 THEN value END) as value_A,
       MAX(CASE WHEN seqnum = 2 THEN customerId END) as customer_B,
       MAX(CASE WHEN seqnum = 2 THEN value END) as value_B
FROM (SELECT a.transactionID, a.customerID, b.value,
             (@rn := if(@t = a.transactionID, @rn + 1,
                        if(@t := a.transactionID, 1, 1)
                       )
             ) as seqnum
      FROM adjustments a INNER JOIN
           change c 
           ON a.transactionID = c.transactionID AND
              a.event_date    = c.event_date AND
              a.event_id      = c.event_id CROSS JOIN
           (SELECT @rn := 0, @t := '') params,
      WHERE comment LIKE 'TRANSFER'
      ORDER BY a.transactionID, b.value DESC
     ) t
GROUP BY transactionId
HAVING SUM(value) <> 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM