带连接的简单SQL问题

时间:2015-08-17 19:04:34

标签: sql sql-server-2008 tsql

我对连接表有一些问题:

Table A -> ID,Col1,Col2,Col3

Table B -> Rank , ColX ,  A_ID (Relationship with A.ID)

我希望获得更高的排名(每个A_ID,如A_ID的组{B}

我的结果必须类似于A.ID , Col1 , Col2 , Col3 , ""ColX"",我该怎么做?

我希望我的结果计数等于A.ID计数。

表A

+--------------------+
| ID|Col1|Col2|Col3| |
+--------------------+
| 1 | C1 | C2 | C3   |
| 2 | C1 | C2 | C3   |
+--------------------+

表-B

+-----------------------------+
| ID| COL_X     |RANK  |A_ID| |
+-----------------------------+
| 1 | SomeValue | 1    | 1    |
| 2 | some22222 | 2    | 1    |
| 3 | SOMEXXXX  | 3    | 1    |
| 4 | SOMEVAL   | 1    | 2    |
| 5 | VALUE     | 2    | 2    |
+-----------------------------+

预期输出:

+--------------------------------------------------------------------+
| ID| Col1| Col2 | Col3| COLX                                        |
+--------------------------------------------------------------------+
| 1 | C1  | C2   | C3  | SOMEXXXX (Higher Rank of TableB-> A_ID = 1) |
| 2 | C1  | C2   | C3  | VALUE (Higher Rank of TableB-> A_ID = 2)    |
+--------------------------------------------------------------------+

5 个答案:

答案 0 :(得分:0)

我想你想从表b中获取表a中每一行的最大等级? 有很多不同的方法可以解决这个问题。这是一个简单的问题:

with maxCTE as

    (select
     a_id,
     max(rank) as MaxRank
     from
     tableb
     group by 
     a_id
     )


         select
         *
         from
         tablea a
         inner join tableb b
         on a.id = b.a_id
         inner join maxcte c
         on b.a_id = c.a_id
         and b.rank = c.MaxRank

SQLFiddle

基本上,CTE识别每个a_id的最大排名,然后我们将其连接回tableb以获取该行的详细信息。

答案 1 :(得分:0)

with x as 
(select a_id, max(rank) as mxrnk
 from tableB 
 group by a_id)
select a.id, a.col1, a.col2, a.col3, b.col_x
from tableA a join x
on a.id = x.a_id
join tableB b
on x.mxrnk = b.rank

您可以在cte中选择每个a_id的最大排名,并使用它来选择相应的列。

答案 2 :(得分:0)

一个是通过ID将表B加入表A中。您将从表B返回3条记录。如果您按COLX对这些记录进行排序

SELECT
  ,a.ID
  ,a.Col1
  ,a.Col2
  ,a.Col3
  ,b.COLX
FROM TableA AS a
INNER JOIN TABLE_B AS b on b.A_ID = a.id
ORDER BY b.COLX DESC

然后另一种方法是加入表B的子查询,该子查询还有一个子查询,它将表B记录仅过滤到具有最高RANK的记录。

通过这种方式,您可以从表B中与表A的记录匹配的最高RANK记录中引入COLX值。

我认为至少......

SELECT
   a.ID
  ,a.Col1
  ,a.Col2
  ,a.Col3
  ,b.COLX
FROM TableA a
INNER JOIN (
             SELECT
                a.A_ID
               ,a.RANK
               ,a.COLX
             FROM TABLE_B a
             INNER JOIN (
                          SELECT
                             A_ID, 
                            ,MAX(RANK) AS [RANK] -- Highest Rank
                          FROM TABLE_B 
                          GROUP BY A_ID
                         ) AS b ON b.A_ID = a.A_ID AND b.RANK = a.RANK
            ) AS b on b.A_ID = a.id
ORDER BY a.ID ASC

答案 3 :(得分:0)

您可以使用subquery轻松执行此操作,首先找到每个max的{​​{1}},然后加入tableA和TableB以获取所需的行:

A_ID

SQL Fiddle Demo

答案 4 :(得分:0)

    Select A.*,D.Col_X    
     from 
          (Select C.COL_X,B.A_ID    
           from
                (Select A_ID,MAX(rank) as MAX_rank
                 from TABLE_B
                 group by A_ID) B ----- gets the highest rank and ID of the highest rank
            inner join  TABLE_B c 
             on 
           concat(C.A_ID,C.RANK)= concat(B.A_ID,B.MAX_rank)) D ---- Gets the highest rank name       
    inner join TABLE_A A 
    on D.A_ID=A.ID

输出:

ID Col1 Col2 Col3 Col_X

1 c1 c2 c3 SOMEXXXX

2 c1 c2 c3值