重写多次使用case语句的查询

时间:2015-09-29 07:13:28

标签: mysql database

SELECT Transaction_Id,Transaction_Amount,
Seller_Account_Number=CASE Transaction_Identifier When 'S' Then counter_Account_Number Else Account_Number END,
Seller_Name=CASE Transaction_Identifier When 'S' Then counter_Account_Name Else Account_Name END ,
Buyer_Account_Number=CASE Transaction_Identifier When 'S' Then Account_Number Else Counter_Account_Number END ,
Buyer_Name=CASE Transaction_Identifier When 'S' Then Account_Name Else Counter_Account_Name END,
FROM Transaction_Table

在这个查询中,我从交易表中提取数据,该交易表有两种类型的交易买入和卖出数据

Transaction_Id    Transaction_Amount   Account_Number   Account_Name   Counter_Account_Number   Counter_Account_Name   Transaction_Identifier

123457                    200              5234545        Supriti               3476756                  Ria                    S 
123458                    300              5234545        Supriti               3476756                  Ria                    B
123459                    700              5234545        Supriti               3476756                  Ria                    S 

我的结果应该是这样的

Transaction_Id   Transaction_Amount   Seller_Account_Number   Seller_Account_Name   Buyer_Account_Number  Buyer_Name
123457                  200                  3476756                    Ria             5234545          Supriti 
123457                  700                  3476756                    Ria             5234545          Supriti 
123458                  300                  5234545                  Supriti           3476756          Ria 

0 个答案:

没有答案