选择2行记录到一行

时间:2017-04-27 13:00:17

标签: sql sql-server tsql

我正在尝试使用以下查询将2行合并为一行,但它仍会返回2条记录。无论如何我是否可以使用单个查询?感谢您的任何建议。

TABLE_A

+------+-------+----------+
| A_ID | EN_ID | COMMENTS |
+------+-------+----------+
|    2 |    10 | test     |
+------+-------+----------+

表-B

+-------+--------+
| EN_ID | ADD_ID |
+-------+--------+
|    10 |    101 |
|    10 |    102 |
+-------+--------+

TABLE_C

+--------+-------------+-------------+-----------+
| ADD_ID | ADD_TYPE_ID |   street    |   city    |
+--------+-------------+-------------+-----------+
|    101 |           1 | teststreet1 | citytest1 |
|    102 |           2 | teststreet2 | Citytest2 |
+--------+-------------+-------------+-----------+

期望的结果

+-------+----------+-------------+-----------+-------------+-----------+
| EN_ID | COMMENTS |  street_1   |  city_1   |  street_2   |  city_2   |
+-------+----------+-------------+-----------+-------------+-----------+
|     3 | test     | teststreet1 | citytest1 | teststreet2 | Citytest2 |
+-------+----------+-------------+-----------+-------------+-----------+

查询

select    
    A.EN_ID, 
    A.COMMENTS, 
    C1.STREET as strret_1, 
    C1.CITY as city_1,  
    C2.STREET as strret_2, 
    C2.CITY as city_2  
from      
    TABLE_A A
left join 
    TABLE_B B ON
    B.EN_ID = A.EN_ID 
left join 
    TABLE_C C1 
    ON C1.ADD_ID = B.ADD_ID 
    and C1.ADD_TYPE_ID = 1
left join 
    TABLE_C C2 
    ON  C2.ADD_ID = B.ADD_ID 
    and C2.ADD_TYPE_ID = 2
    and A.A_ID = 2;

3 个答案:

答案 0 :(得分:2)

这样就可以了解

DECLARE @tA TABLE (A_ID INT, EN_ID INT, COMMENTS VARCHAR(255))
INSERT INTO @tA VALUES
(2,10,'test')

DECLARE @tB TABLE (EN_ID INT, ADD_ID INT)
INSERT INTO @tB VALUES
(10,101),
(10,102)

DECLARE @tC TABLE (ADD_ID INT, ADD_TYPE_ID INT, Street VARCHAR(255), City VARCHAR(255))
INSERT INTO @tC VALUES
(101,1,'teststreet1','citytest1'),        
(102,2,'teststreet2','Citytest2') 

SELECT A.EN_ID, A.COMMENTS, MAX(C1.Street) AS Street1, MAX(C1.City) AS City1, MAX(C2.Street) AS Street2, MAX(C2.City) AS City2
FROM @tA AS A
LEFT OUTER JOIN @tB AS B ON B.EN_ID = A.EN_ID
LEFT OUTER JOIN (SELECT * FROM @tC WHERE ADD_TYPE_ID = 1) AS C1 ON C1.ADD_ID = B.ADD_ID 
LEFT OUTER JOIN (SELECT * FROM @tC WHERE ADD_TYPE_ID = 2) AS C2 ON C2.ADD_ID = B.ADD_ID 
GROUP BY A.EN_ID, A.COMMENTS

结果

EN_ID  COMMENTS  Street1      City1      Street2      City2
----------------------------------------------------------------
10     test      teststreet1  citytest1  teststreet2  Citytest2

答案 1 :(得分:0)

被修改

尝试这样的事情:

SELECT a.en_id, a.comments, c1.street AS street_1, c1.city AS city_1,
       c2.street AS street_2, c2.city AS city_2  
  FROM table_a a
  LEFT JOIN table_b b1 ON b1.en_id = a.en_id 
  LEFT JOIN table_c c1 ON c1.add_id = b1.add_id AND c1.add_type_id = 1
  LEFT JOIN table_b b2 ON b2.en_id = a.en_id AND b1.add_id <> b2.add_id
  LEFT JOIN table_c c2 ON c2.add_id = b2.add_id AND c2.add_type_id = 2
  WHERE a.a_id = 2 AND ( b1.add_id IS NOT NULL OR b2.add_id IS NOT NULL )

最后一行也可能是:

  WHERE a.a_id = 2 AND NOT ( b1.add_id IS NULL AND b2.add_id IS NULL)

答案 2 :(得分:0)

另一种更快的方式:

SELECT    
    A.EN_ID, 
    A.COMMENTS,
    SUM(CASE WHEN C.ADD_TYPE_ID = 1 THEN C.city ELSE '' END) AS city1,
    SUM(CASE WHEN C.ADD_TYPE_ID = 1 THEN C.street ELSE '' END) AS street1,
    SUM(CASE WHEN C.ADD_TYPE_ID = 2 THEN C.city ELSE '' END) AS city2,
    SUM(CASE WHEN C.ADD_TYPE_ID = 2 THEN C.street ELSE '' END) AS street2
FROM      
    TABLE_A A
left join TABLE_B as B ON (B.EN_ID = A.EN_ID) 
left join TABLE_C AS C ON (C.ADD_ID = B.ADD_ID)
GROUP BY A.EN_ID, A.COMMENTS

注意:如果C.city或C.street有一些NULL值,您可能会收到警告。如果是这样,请使用ISNULL(C.city,'')和ISNULL(C.street,'')语法......