2012-09-19 76 views
2

我有如下的結果從我的查詢(其中有兩個SELECT查詢的UNION)MySQL的:兩個記錄合併到一個

app_id transaction_id mobile_no user_input1       user_input3       user_input17      user_input110      user_input61   user_input62 
104  731   918087318987 Welcome To Onida~Home Appliances Home Appliances~Washing Machine Washing Machine~Fully Automatic Fully Automatic~SPARKLE 65S    NULL     NULL 
104  731   918087318987  NULL        NULL          NULL       NULL       Quantity ~2    Amount~2 

我想這兩個記錄合併成1行集

app_id transaction_id mobile_no user_input1       user_input3       user_input17      user_input110      user_input61   user_input62 
104  731   918087318987 Welcome To Onida~Home Appliances Home Appliances~Washing Machine Washing Machine~Fully Automatic Fully Automatic~SPARKLE 65S   Quantity ~2    Amount~2 

我可以提供我的SQL查詢,但它太長了。如果需要將提供。

SQLSchema:

create table `trn_user_log` (
`app_id` int (11), 
`transaction_id` int (11), 
`mobile_no` varchar (45), 
`node_id` bigint (20), 
`customer_attribute` varchar (150), 
`entered_value` varchar (150) 
); 

insert into `trn_user_log` (`app_id`, `transaction_id`, `mobile_no`, `node_id`, `customer_attribute`, `entered_value`) values('104','731','918087318987','103','Welcome To Onida','2'); 
insert into `trn_user_log` (`app_id`, `transaction_id`, `mobile_no`, `node_id`, `customer_attribute`, `entered_value`) values('104','731','918087318987','105','Home Appliances','1'); 
insert into `trn_user_log` (`app_id`, `transaction_id`, `mobile_no`, `node_id`, `customer_attribute`, `entered_value`) values('104','731','918087318987','119','Washing Machine','1'); 
insert into `trn_user_log` (`app_id`, `transaction_id`, `mobile_no`, `node_id`, `customer_attribute`, `entered_value`) values('104','731','918087318987','121','Fully Automatic','2'); 
insert into `trn_user_log` (`app_id`, `transaction_id`, `mobile_no`, `node_id`, `customer_attribute`, `entered_value`) values('104','731','918087318987','169','Quantity ','2'); 
insert into `trn_user_log` (`app_id`, `transaction_id`, `mobile_no`, `node_id`, `customer_attribute`, `entered_value`) values('104','731','918087318987','170','Amount','2'); 


create table `mst_node` (
`app_id` int (11), 
`node_id` bigint (20), 
`parent_node_id` bigint (20), 
`display_seq` tinyint (4), 
`display_text` varchar (540), 
`customer_attribute` varchar (150) 
); 

insert into `mst_node` (`app_id`, `node_id`, `parent_node_id`, `display_seq`, `display_text`, `customer_attribute`) values('104','103',NULL,'1','Welcome To Onida','Welcome To Onida'); 
insert into `mst_node` (`app_id`, `node_id`, `parent_node_id`, `display_seq`, `display_text`, `customer_attribute`) values('104','105','103','2','Home Appliances','Home Appliances'); 
insert into `mst_node` (`app_id`, `node_id`, `parent_node_id`, `display_seq`, `display_text`, `customer_attribute`) values('104','119','105','1','Washing Machine','Washing Machine'); 
insert into `mst_node` (`app_id`, `node_id`, `parent_node_id`, `display_seq`, `display_text`, `customer_attribute`) values('104','121','119','1','Fully Automatic','Fully Automatic'); 
insert into `mst_node` (`app_id`, `node_id`, `parent_node_id`, `display_seq`, `display_text`, `customer_attribute`) values('104','124','121','2','SPARKLE 65S ','SPARKLE 65S '); 
insert into `mst_node` (`app_id`, `node_id`, `parent_node_id`, `display_seq`, `display_text`, `customer_attribute`) values('104','125','121','3','Sparkle 65X','Sparkle 65X'); 
insert into `mst_node` (`app_id`, `node_id`, `parent_node_id`, `display_seq`, `display_text`, `customer_attribute`) values('104','126','121','4','Sparkle 62P','Sparkle 62P'); 
insert into `mst_node` (`app_id`, `node_id`, `parent_node_id`, `display_seq`, `display_text`, `customer_attribute`) values('104','169','124','1','Quantity ','Quantity '); 
insert into `mst_node` (`app_id`, `node_id`, `parent_node_id`, `display_seq`, `display_text`, `customer_attribute`) values('104','170','124','2','Amount','Amount'); 

我的SQL查詢:

SELECT * 
FROM 
(
SELECT T1.app_id, 
    T1.transaction_id, 
    T1.mobile_no, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 103 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input1, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 105 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input3, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 119 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input17, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 121 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input110, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 169 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input61, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 170 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input62 
FROM trn_user_log T1 INNER JOIN mst_node T2 ON T1.APP_ID = T2.APP_ID 
WHERE  T1.app_id = 104 
    AND T1.transaction_id = 731 
    AND T1.node_id = T2.parent_node_id 
    AND T2.`display_seq` = T1.entered_value 
-- GROUP BY T1.app_id, T1.transaction_id, T1.mobile_no 
-- ORDER BY T1.node_id 

UNION 

SELECT T3.app_id, 
    T3.transaction_id, 
    T3.mobile_no, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 103 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input1, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 105 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input3, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 119 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input17, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 121 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input110, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 169 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input61, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 170 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input62 
FROM trn_user_log T3 INNER JOIN mst_node T4 ON T3.APP_ID = T4.APP_ID 
WHERE T3.app_id = 104 
    AND T3.transaction_id = 731 
    AND T3.node_id = T4.node_id 
    AND not exists (select 1 from mst_node b where b.parent_node_id = t4.node_id) 

--  GROUP BY T3.app_id, T3.transaction_id, T3.mobile_no 
-- ORDER BY T3.node_id 
)T 
GROUP BY T.app_id, T.transaction_id, T.mobile_no 

SQLFiddle Demo Schema and Query

+0

是它總是一個行會的數據和其他將有相應的列空的情況?如果不是,這些列的組合行應包含哪些內容? – Barmar

+0

如果您看到兩個select查詢,那麼在第一個查詢中將返回Null的列將在第二個查詢中具有值,反之亦然。我想將這兩個記錄合併爲一個,所以理想情況下在這兩個記錄中不會有NULL值最終結果 – DarkKnightFan

回答

2

可以JOIN 2選擇與對方,然後對列進行COALESCE

http://www.sqlfiddle.com/#!2/bfcd8/15

SELECT app_id,transaction_id,mobile_no, 
coalesce(tt1.USER_INPUT1,tt2.USER_INPUT1) AS USER_INPUT1, 
coalesce(tt1.USER_INPUT3,tt2.USER_INPUT3) AS USER_INPUT3, 
coalesce(tt1.USER_INPUT17,tt2.USER_INPUT17) AS USER_INPUT17, 
coalesce(tt1.USER_INPUT110,tt2.USER_INPUT110) AS USER_INPUT110, 
coalesce(tt1.USER_INPUT61,tt2.USER_INPUT61) AS USER_INPUT61, 
coalesce(tt1.USER_INPUT62,tt2.USER_INPUT62) AS USER_INPUT62 
FROM (SELECT T1.app_id, 
     T1.transaction_id, 
     T1.mobile_no, 
     CONVERT(
      GROUP_CONCAT(
      (CASE T1.node_id 
       WHEN 103 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input1, 
      CONVERT(
      GROUP_CONCAT(
      (CASE T1.node_id 
       WHEN 105 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input3, 
      CONVERT(
      GROUP_CONCAT(
      (CASE T1.node_id 
       WHEN 119 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input17, 
      CONVERT(
      GROUP_CONCAT(
      (CASE T1.node_id 
       WHEN 121 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input110, 
     CONVERT(
      GROUP_CONCAT(
      (CASE T1.node_id 
       WHEN 169 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input61, 
     CONVERT(
      GROUP_CONCAT(
      (CASE T1.node_id 
       WHEN 170 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input62 
    FROM trn_user_log T1 INNER JOIN mst_node T2 ON T1.APP_ID = T2.APP_ID 
WHERE  T1.app_id = 104 
     AND T1.transaction_id = 731 
     AND T1.node_id = T2.parent_node_id 
     AND T2.`display_seq` = T1.entered_value 
-- GROUP BY T1.app_id, T1.transaction_id, T1.mobile_no 
-- ORDER BY T1.node_id 
) AS tt1 

LEFT JOIN 

(SELECT T3.app_id, 
     T3.transaction_id, 
     T3.mobile_no, 
     CONVERT(
      GROUP_CONCAT(
      (CASE T3.node_id 
       WHEN 103 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input1, 
      CONVERT(
      GROUP_CONCAT(
      (CASE T3.node_id 
       WHEN 105 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input3, 
      CONVERT(
      GROUP_CONCAT(
      (CASE T3.node_id 
       WHEN 119 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input17, 
      CONVERT(
      GROUP_CONCAT(
      (CASE T3.node_id 
       WHEN 121 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input110, 
     CONVERT(
      GROUP_CONCAT(
      (CASE T3.node_id 
       WHEN 169 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input61, 
     CONVERT(
      GROUP_CONCAT(
      (CASE T3.node_id 
       WHEN 170 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
       ELSE NULL 
       END)) USING LATIN1) 
      AS user_input62 
FROM trn_user_log T3 INNER JOIN mst_node T4 ON T3.APP_ID = T4.APP_ID 
WHERE T3.app_id = 104 
     AND T3.transaction_id = 731 
     AND T3.node_id = T4.node_id 
     AND NOT EXISTS (SELECT 1 FROM mst_node b WHERE b.parent_node_id = t4.node_id) 

--  GROUP BY T3.app_id, T3.transaction_id, T3.mobile_no 
-- ORDER BY T3.node_id 
) AS tt2 USING (app_id, transaction_id, mobile_no) 
+0

謝謝..這工作,或多或少我正在尋找的東西! – DarkKnightFan

0

裹你unionsubquery然後加入。

SELECT * 
FROM 
(
    -- place your unioned query 
) a 
GROUP BY a.app_id, a.transaction_id, a.mobile_no 
+0

我試過這個東西,但是我得到了與上面提到的第一條記錄相同的一行...這意味着最後兩列('user_input61'和'user_input62')仍然是NULL .. – DarkKnightFan

+0

好多了,如果你可以添加架構和示例記錄。 :D –

+0

嗯..我知道你會需要的..讓我看看我可以擠進一些有限的記錄.. – DarkKnightFan

0
SELECT app_id, transaction_id, mobile_no, 
     MAX(user_input1) user_input1, MAX(user_input3) user_input3, 
     MAX(user_input17) user_input17, MAX(user_input110) user_input110, 
     MAX(user_input61) user_input61, MAX(user_input62) user_input62 
FROM (
SELECT T1.app_id, 
    T1.transaction_id, 
    T1.mobile_no, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 103 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input1, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 105 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input3, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 119 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input17, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 121 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input110, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 169 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input61, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T1.node_id 
      WHEN 170 THEN CONCAT(T1.customer_attribute, '~', T2.display_text) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input62 
FROM trn_user_log T1 INNER JOIN mst_node T2 ON T1.APP_ID = T2.APP_ID 
WHERE  T1.app_id = 104 
    AND T1.transaction_id = 731 
    AND T1.node_id = T2.parent_node_id 
    AND T2.`display_seq` = T1.entered_value 
-- GROUP BY T1.app_id, T1.transaction_id, T1.mobile_no 
-- ORDER BY T1.node_id 

UNION 

SELECT T3.app_id, 
    T3.transaction_id, 
    T3.mobile_no, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 103 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input1, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 105 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input3, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 119 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input17, 
     CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 121 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input110, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 169 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input61, 
    CONVERT(
     GROUP_CONCAT(
     (CASE T3.node_id 
      WHEN 170 THEN CONCAT(T3.customer_attribute, '~', T3.entered_value) 
      ELSE NULL 
      END)) USING LATIN1) 
     AS user_input62 
FROM trn_user_log T3 INNER JOIN mst_node T4 ON T3.APP_ID = T4.APP_ID 
WHERE T3.app_id = 104 
    AND T3.transaction_id = 731 
    AND T3.node_id = T4.node_id 
    AND not exists (select 1 from mst_node b where b.parent_node_id = t4.node_id) 

--  GROUP BY T3.app_id, T3.transaction_id, T3.mobile_no 
-- ORDER BY T3.node_id 
)T 
GROUP BY T.app_id, T.transaction_id, T.mobile_no