如何编写查询以获取多级数据

时间:2014-07-25 10:51:52

标签: sql sql-server-2008

我有四张表格如下:

tblAccount

Id i sprimary key
+----+-----------------+
| Id |  AccName        |
+----+-----------------+
|  1 | AccountA         |
|  2 | AccountB         |
+----+-----------------+

tblLocation

Id is primary key.
+----+---------------+
| Id |  LocName      |
+----+---------------+
|  1 | LocationA     |
|  2 | LocationB     |
|  3 | LocationC     |
+----+---------------+

tblAccountwiseLocation

Id i sprimary key.LocId and AccId are foreign key.
+----+---------------+---------------+
| Id |  LocId        |  AccId        |
+----+---------------+---------------+
|  1 |   1           |    1          |
|  2 |   2           |    1          |
|  3 |   3           |    1          |
|  4 |   1           |    2          |
|  5 |   2           |    2          |
|  6 |   3           |    2          |
+----+---------------+---------------+

tblRSCMaster

Id i sprimary key.LocId and AccId are foreign key.
+----+---------------+---------------+----------------+------------------+
| Id |  LocId        |  AccId        |  RSCNo         |  DateOfAddition  |
+----+---------------+---------------+----------------+------------------+
|  1 |   1           |    1          | Acc1_Loc1_1_14 |    15/01/2014    |
|  2 |   2           |    1          | Acc1_Loc2_1_14 |    15/01/2014    |
|  3 |   3           |    1          | Acc1_Loc2_1_14 |    15/01/2014    |
|  4 |   1           |    2          | Acc2_Loc1_1_14 |    15/01/2014    |
|  5 |   2           |    2          | Acc2_Loc2_1_14 |    15/01/2014    |
|  6 |   3           |    2          | Acc2_Loc3_1_14 |    15/01/2014    |
|  7 |   1           |    1          | Acc1_Loc1_2_14 |    15/02/2014    |
|  8 |   2           |    1          | Acc1_Loc2_2_14 |    15/02/2014    |
|  9 |   3           |    1          | Acc1_Loc3_2_14 |    15/02/2014    |
| 10 |   1           |    2          | Acc2_Loc1_2_14 |    15/02/2014    |
| 11 |   2           |    2          | Acc2_Loc2_2_14 |    15/02/2014    |
| 12 |   3           |    2          | Acc2_Loc3_2_14 |    15/02/2014    |
| 13 |   1           |    1          | Acc1_Loc1_3_14 |    15/03/2014    |
| 14 |   2           |    1          | Acc1_Loc2_3_14 |    15/03/2014    |
| 15 |   3           |    1          | Acc1_Loc3_3_14 |    15/03/2014    |
| 16 |   1           |    2          | Acc2_Loc1_3_14 |    15/03/2014    |
| 17 |   2           |    2          | Acc2_Loc2_3_14 |    15/03/2014    |
| 18 |   3           |    2          | Acc2_Loc3_3_14 |    15/03/2014    |
| 19 |   1           |    1          | Acc1_Loc1_4_14 |    15/04/2014    |
| 20 |   2           |    1          | Acc1_Loc2_4_14 |    15/04/2014    |
| 21 |   3           |    1          | Acc1_Loc3_4_14 |    15/04/2014    |
| 22 |   1           |    2          | Acc2_Loc1_4_14 |    15/04/2014    |
| 23 |   2           |    2          | Acc2_Loc2_4_14 |    15/04/2014    |
| 24 |   3           |    2          | Acc2_Loc3_4_14 |    15/04/2014    |
| 25 |   1           |    1          | Acc1_Loc1_5_14 |    15/05/2014    |
| 26 |   2           |    1          | Acc1_Loc2_5_14 |    15/05/2014    |
| 27 |   3           |    1          | Acc1_Loc3_5_14 |    15/05/2014    |
| 28 |   1           |    2          | Acc2_Loc1_5_14 |    15/05/2014    |
| 29 |   2           |    2          | Acc2_Loc2_5_14 |    15/05/2014    |
| 30 |   3           |    2          | Acc2_Loc3_5_14 |    15/05/2014    |
+----+---------------+---------------+----------------+------------------+

Acc1_Loc1_1_14类似于2014年1月AccountA的LocationA的RSC。

我需要从tblRSCMaster获得如下输出。

+---------------+---------------+----------------+------------------+
|  LocId        |  AccId        |  RSCNo         |  DateOfAddition  |
+---------------+---------------+----------------+------------------+
|   1           |    1          | Acc1_Loc1_3_14 |    15/03/2014    |
|   1           |    1          | Acc1_Loc1_4_14 |    15/04/2014    |
|   1           |    1          | Acc1_Loc1_5_14 |    15/05/2014    |
|   2           |    1          | Acc1_Loc2_3_14 |    15/03/2014    |
|   2           |    1          | Acc1_Loc2_4_14 |    15/04/2014    |
|   2           |    1          | Acc1_Loc2_5_14 |    15/05/2014    |
|   3           |    1          | Acc1_Loc3_3_14 |    15/03/2014    |
|   3           |    1          | Acc1_Loc3_4_14 |    15/04/2014    |
|   3           |    1          | Acc1_Loc3_5_14 |    15/05/2014    |
+---------------+---------------+----------------+------------------+

每个帐户都有多个位置,每个位置都有多个RSC。

我需要为AccountA的每个位置获取最后三个RSC。

我尝试过以下查询:

   SELECT tblAccountwiseLocation.LocId,tblAccountwiseLocation.AccId,tblRSCMaster.RSCNo,tblRSCMaster.DateOfAddition FROM   tblAccountwiseLocation
   INNER JOIN tblRSCMaster ON tblAccountwiseLocation.LocId= tblRSCMaster.LocId
   where tblRSCMaster.AccId=1

但没有得到正确的输出。 请帮帮我。

提前谢谢大家。

2 个答案:

答案 0 :(得分:1)

您可以将现有查询包装在公用表表达式中,并使用ROW_NUMBER()每个tblAccountwiseLocation.LocId仅获取最后3个(通过tblRSCMaster.DateOfAddition)条目。

WITH cte AS (
  SELECT tblAccountwiseLocation.LocId,
       tblAccountwiseLocation.AccId,
       tblRSCMaster.RSCNo,
       tblRSCMaster.DateOfAddition,
       ROW_NUMBER() OVER (PARTITION BY tblAccountwiseLocation.LocId
                          ORDER BY tblRSCMaster.DateOfAddition DESC) rn
  FROM   tblAccountwiseLocation
  INNER JOIN tblRSCMaster 
    ON tblAccountwiseLocation.LocId = tblRSCMaster.LocId
   AND tblAccountwiseLocation.AccId = tblRSCMaster.AccId
  WHERE tblRSCMaster.AccId=1
)
SELECT LocId, AccId, RSCNo, DateOfAddition
FROM cte
WHERE rn <= 3
ORDER BY LocId, AccId, DateOfAddition

An SQLfiddle to test with

答案 1 :(得分:0)

这是你需要的吗?

select m.*
from (select m.*, row_number() over (partition by accID
                                     order by DateOfAddition desc) as seqnum
      from tblRSCMaster
      where m.locid = 1
     ) m
where seqnum <= 3
order by AccId, DateOfAddition;

我认为您需要过滤locid而不是AccId以获得您想要的内容。