窗口函数ms访问

时间:2017-11-16 18:04:13

标签: sql ms-access partition-by

我正在使用MS Access中的类调度数据库。有各种各样的课程,每一课都被教授多次,有时一天多次,但不一定每天都有。每门课程都有一套独特的软件和数据存储在笔记本电脑上。每个课程都有一套笔记本电脑,上面装有该软件。

对于任何特定的培训日,我需要为不同房间的正确班级分配一系列笔记本电脑ID,具体取决于该房间将有多少人参加该课程,以便教师知道要将哪些笔记本电脑带到那天他们和他们一起教课。

例如,我有原始数据:

Date   Room   ClassName      HeadCount
----   ----   ---------      ---------
11/30   101   Intro            10
11/30   102   Intro            15
11/30   103   Course 2          5
12/1    101   Intro            10
12/1    102   Course 2         15
12/1    103   Course 3         10

我也知道关于笔记本电脑的以下内容:

ClassName    LaptopID
---------   ---------
Intro       LT.Intro_1
Intro        ...
Intro       LT.Intro_30
Course 2    LT.Course 2_1
Course 2      ...
Course 2    LT.Course 2_30
Course 3    LT.Course 3_1   
Course 3    ...
Course 3    LT.Course 3_30

基于以上两个表,我想输出:

Date   Room   ClassName     HeadCount  First Laptop   Last Laptop
----   ----   ---------     ---------  ------------   -----------
11/30   101   Intro           10       LT.Intro_1     LT.Intro_10
11/30   102   Intro           15       LT.Intro_11    LT.Intro_25
11/30   103   Course 2         5       LT.Course 2_1  LT.Course 2_5
12/1    101   Intro           10       LT.Intro_1     LT.Intro_10
12/1    102   Course 2        15       LT.Course 2_1  LT.Course 2_15
12/1    103   Course 3        10       LT.Course 3_1  LT.Course 3_10  

我知道这是一个窗口函数,但MS Access没有超前或滞后。有解决方法吗?

1 个答案:

答案 0 :(得分:1)

您可能希望更改表定义以获得更好的性能。我已经重新提到了两张桌子。

您知道您的笔记本电脑ID是按顺序排列的,并且您知道每个班级的人数。为了跟随线索,您必须知道最后一个人数。 在当前的课程/活动之前,对于同一个班级的同一日期的参加者。

x = sum(headCount)其中id< currentID& classname = currentClassname& date = currentDate。 (当前表示currentRow。)

现在您知道当前行之前使用的笔记本电脑总数和当前行的headCount。第一台笔记本电脑将是

f = min(laptopid) where laptopid > x (x being totaLaptopUsedBefore this Row)

对于Last笔记本电脑,您还必须添加当前的人数。

l = min(laptopid) where laptopid >= currentHeadCount + x

注意f检查laptopid更大,L检查> =。

以下是一个可以改进的工作演示:

表1:tbl_ClassEvents

+----+------------+------+-----------+-----------+
| ID |    date    | Room | ClassName | HeadCount |
+----+------------+------+-----------+-----------+
|  1 | 30/11/2017 |  101 | Intro     |        10 |
|  2 | 30/11/2017 |  102 | intro     |        15 |
|  3 | 30/11/2017 |  103 | Course 2  |         5 |
|  4 | 01/12/2017 |  101 | Intro     |        10 |
|  5 | 01/12/2017 |  102 | Course 2  |        15 |
|  6 | 01/12/2017 |  103 | Course 3  |        10 |
|  7 | 17/11/2017 |  101 | Intro     |        16 |
+----+------------+------+-----------+-----------+

Table2: Tbl_ClassVsLaptop
+----+-----------+----------------+
| Id | ClassName |    LaptopId    |
+----+-----------+----------------+
|  1 | Intro     | LT.Intro_1     |
|  2 | Intro     | LT.Intro_2     |
|  3 | Intro     | LT.Intro_3     |
|  4 | Intro     | LT.Intro_4     |
|  5 | Intro     | LT.Intro_5     |
|  6 | Intro     | LT.Intro_6     |
|  7 | Intro     | LT.Intro_7     |
|  8 | Intro     | LT.Intro_8     |
|  9 | Intro     | LT.Intro_9     |
| 10 | Intro     | LT.Intro_10    |
| 11 | Intro     | LT.Intro_11    |
| 12 | Intro     | LT.Intro_12    |
| 13 | Intro     | LT.Intro_13    |
| 14 | Intro     | LT.Intro_14    |
| 15 | Intro     | LT.Intro_15    |
| 16 | Intro     | LT.Intro_16    |
| 17 | Intro     | LT.Intro_17    |
| 18 | Intro     | LT.Intro_18    |
| 19 | Intro     | LT.Intro_19    |
| 20 | Intro     | LT.Intro_20    |
| 21 | Intro     | LT.Intro_21    |
| 22 | Intro     | LT.Intro_22    |
| 23 | Intro     | LT.Intro_23    |
| 24 | Intro     | LT.Intro_24    |
| 25 | Intro     | LT.Intro_25    |
| 26 | Intro     | LT.Intro_26    |
| 27 | Intro     | LT.Intro_27    |
| 28 | Intro     | LT.Intro_28    |
| 29 | Intro     | LT.Intro_29    |
| 30 | Intro     | LT.Intro_30    |
| 31 | Course 2  | LT.Course 2_1  |
| 32 | Course 2  | LT.Course 2_2  |
| 33 | Course 2  | LT.Course 2_3  |
| 34 | Course 2  | LT.Course 2_4  |
| 35 | Course 2  | LT.Course 2_5  |
| 36 | Course 2  | LT.Course 2_6  |
| 37 | Course 2  | LT.Course 2_7  |
| 38 | Course 2  | LT.Course 2_8  |
| 39 | Course 2  | LT.Course 2_9  |
| 40 | Course 2  | LT.Course 2_10 |
| 41 | Course 2  | LT.Course 2_11 |
| 42 | Course 2  | LT.Course 2_12 |
| 43 | Course 2  | LT.Course 2_13 |
| 44 | Course 2  | LT.Course 2_14 |
| 45 | Course 2  | LT.Course 2_15 |
| 46 | Course 2  | LT.Course 2_16 |
| 47 | Course 2  | LT.Course 2_17 |
| 48 | Course 2  | LT.Course 2_18 |
| 49 | Course 2  | LT.Course 2_19 |
| 50 | Course 2  | LT.Course 2_20 |
| 51 | Course 2  | LT.Course 2_21 |
| 52 | Course 2  | LT.Course 2_22 |
| 53 | Course 2  | LT.Course 2_23 |
| 54 | Course 2  | LT.Course 2_24 |
| 55 | Course 2  | LT.Course 2_25 |
| 56 | Course 2  | LT.Course 2_26 |
| 57 | Course 2  | LT.Course 2_27 |
| 58 | Course 2  | LT.Course 2_28 |
| 59 | Course 2  | LT.Course 2_29 |
| 60 | Course 2  | LT.Course 2_30 |
| 61 | Course 3  | LT.Course 3_1  |
| 62 | Course 3  | LT.Course 3_2  |
| 63 | Course 3  | LT.Course 3_3  |
| 64 | Course 3  | LT.Course 3_4  |
| 65 | Course 3  | LT.Course 3_5  |
| 66 | Course 3  | LT.Course 3_6  |
| 67 | Course 3  | LT.Course 3_7  |
| 68 | Course 3  | LT.Course 3_8  |
| 69 | Course 3  | LT.Course 3_9  |
| 70 | Course 3  | LT.Course 3_10 |
| 71 | Course 3  | LT.Course 3_11 |
| 72 | Course 3  | LT.Course 3_12 |
| 73 | Course 3  | LT.Course 3_13 |
| 74 | Course 3  | LT.Course 3_14 |
| 75 | Course 3  | LT.Course 3_15 |
| 76 | Course 3  | LT.Course 3_16 |
| 77 | Course 3  | LT.Course 3_17 |
| 78 | Course 3  | LT.Course 3_18 |
| 79 | Course 3  | LT.Course 3_19 |
| 80 | Course 3  | LT.Course 3_20 |
| 81 | Course 3  | LT.Course 3_21 |
| 82 | Course 3  | LT.Course 3_22 |
| 83 | Course 3  | LT.Course 3_23 |
| 84 | Course 3  | LT.Course 3_24 |
| 85 | Course 3  | LT.Course 3_25 |
| 86 | Course 3  | LT.Course 3_26 |
| 87 | Course 3  | LT.Course 3_27 |
| 88 | Course 3  | LT.Course 3_28 |
| 89 | Course 3  | LT.Course 3_29 |
| 90 | Course 3  | LT.Course 3_30 |
+----+-----------+----------------+

以下是查询:

SELECT tbl_classEvents.ID
    ,tbl_classEvents.DATE
    ,tbl_classEvents.Room
    ,tbl_classEvents.ClassName
    ,tbl_classEvents.HeadCount
    ,(
        SELECT min(laptopId)
        FROM tbl_ClassVsLaptop T1
        WHERE T1.ClassName = tbl_ClassEvents.ClassNAme
            AND Mid([T1.LaptopID], InStrRev([T1.LaptopID], "_") + 1, 3) > (
                + Nz((
                        SELECT sum(headCount)
                        FROM tbl_classEvents T2
                        WHERE T2.ID < Tbl_ClassEvents.ID
                            AND T2.[DATE] = [Tbl_ClassEvents].[DATE]
                            AND T2.[ClassName] = [Tbl_ClassEvents].[ClassName]
                        ), 0)
                )
        ) AS FirstLaptop
    ,(
        SELECT min(laptopId)
        FROM tbl_ClassVsLaptop T1
        WHERE T1.ClassName = tbl_ClassEvents.ClassNAme
            AND Mid([T1.LaptopID], InStrRev([T1.LaptopID], "_") + 1, 3) >= (
                + [tbl_classEvents].[HeadCount] + Nz((
                        SELECT sum(headCount)
                        FROM tbl_classEvents T2
                        WHERE T2.ID < Tbl_ClassEvents.ID
                            AND T2.[DATE] = [Tbl_ClassEvents].[DATE]
                            AND T2.[ClassName] = [Tbl_ClassEvents].[ClassName]
                        ), 0)
                )
        ) AS LastLaptop
FROM tbl_classEvents
ORDER BY tbl_classEvents.DATE
    ,tbl_classEvents.Room
    ,tbl_classEvents.ClassNAme;

输出:

+----+------------+------+-----------+-----------+---------------+----------------+
| ID |    DATE    | Room | ClassName | HeadCount |  FirstLaptop  |   LastLaptop   |
+----+------------+------+-----------+-----------+---------------+----------------+
|  7 | 17/11/2017 |  101 | Intro     |        16 | LT.Intro_1    | LT.Intro_16    |
|  1 | 30/11/2017 |  101 | Intro     |        10 | LT.Intro_1    | LT.Intro_10    |
|  2 | 30/11/2017 |  102 | intro     |        15 | LT.Intro_11   | LT.Intro_25    |
|  3 | 30/11/2017 |  103 | Course 2  |         5 | LT.Course 2_1 | LT.Course 2_5  |
|  4 | 01/12/2017 |  101 | Intro     |        10 | LT.Intro_1    | LT.Intro_10    |
|  5 | 01/12/2017 |  102 | Course 2  |        15 | LT.Course 2_1 | LT.Course 2_15 |
|  6 | 01/12/2017 |  103 | Course 3  |        10 | LT.Course 3_1 | LT.Course 3_10 |
+----+------------+------+-----------+-----------+---------------+----------------+