首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >窗口函数ms访问

窗口函数ms访问
EN

Stack Overflow用户
提问于 2017-11-16 18:04:13
回答 1查看 3.5K关注 0票数 0

我正在MS中开发一个类调度数据库。有各种各样的课程,每一节课都教过多次,有时一天教几次,但不一定每天都教。每门课程都有一套独特的软件和数据存储在笔记本电脑上。每门课程都有一套笔记本电脑,上面装着这个软件。

在任何特定的培训日,我都需要在不同的教室里为正确的班级分配一系列的笔记本电脑I,这取决于有多少人会在那个教室里上课,这样老师们就能知道当天该带哪台笔记本电脑去教室上课。

例如,我有原始数据:

代码语言:javascript
复制
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

关于笔记本电脑,我也知道以下几点:

代码语言:javascript
复制
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

基于上述两个表,我希望输出:

代码语言:javascript
复制
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没有引导或滞后。有解决办法吗?

EN

回答 1

Stack Overflow用户

发布于 2017-11-17 11:37:39

您可能希望更改表定义以获得更好的性能。正如你提到的,我重新创造了两张桌子。

你知道你的笔记本电脑ids是按顺序排列的,而且你知道每个班级的人数。为了跟随一条线索,你必须知道最后的人数。它将是同一日期、同一班级、当前班级/事件之前的toal与会者。

X= sum(headCount),其中id < currentID & classname = currentClassname & date = currentDate。(当前的意思是currentRow.)

现在您知道了当前行之前使用的总膝上型计算机和当前行的headCount。第一台笔记本电脑

代码语言:javascript
复制
f = min(laptopid) where laptopid > x (x being totaLaptopUsedBefore this Row)

对于“最后一台笔记本”,还必须添加当前的人数。

代码语言:javascript
复制
l = min(laptopid) where laptopid >= currentHeadCount + x

注f检查翻版,L检查>=。

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

Table1: tbl_ClassEvents

代码语言:javascript
复制
+----+------------+------+-----------+-----------+
| 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 |
+----+-----------+----------------+

以下是查询:

代码语言:javascript
复制
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;

以及产出:

代码语言:javascript
复制
+----+------------+------+-----------+-----------+---------------+----------------+
| 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 |
+----+------------+------+-----------+-----------+---------------+----------------+
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47336387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档