表数据基于其他表 [英] Table data based on other tables
问题描述
您好,
我有一张员工表,如下图所示。每位员工都可以参加考试,可以获得1到6级的水平
I’ve a employee table as below and just an example. Each employee can take a test, can earn levels 1 – 6
EmployeeId 跨度> TestId <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
测试日期
等级 跨度>
EmployeeId TestId Test Date Level
1 <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
跨度> 10 <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
1/1/2019
1
1 10 1/1/2019 1
1 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
跨度> 20 <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
2/1/2019
2
1 20 2/1/2019 2
1 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
跨度> 30 <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
3/1/2019
3
1 30 3/1/2019 3
1 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
跨度> 40 <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
3/15/2019
1
1 40 3/15/2019 1
以第一个测试为基础,检查员工是否通过进入下一级别来显示改进。当他移动到下一级别时,在另一个表格中创建新行,显示他获得了多少。
Take the first test as base and check whether employee shows improvement by moving to next level. When he moves to next level, create new rows into another table that shows how much he gained.
Employee_Test_Status
Employee_Test_Status
状态ID &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
Followup_test_id
跨度>状态
Status Id Followup_test_id Status
1 <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
20 跨度>
<跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
从1级移至2
1 20 Moved from level 1 to 2
2   ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
跨度> 30 <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
从1级移到3级
2 30 Moved from level 1 to 3
当员工降级像2009年3月15日这样的级别时,不需要在Employee_Test_Status表中插入任何内容。
When employee downgraded a level like on 3/15/2019, no need to insert anything in Employee_Test_Status table.
创建
< span style ="font-size:9.5pt; font-family:Consolas; color:blue"> table #employee
( employeeId
int , test_id
int , testdate
日期时间 , levelid
int )
create table #employee (employeeId int, test_id int, testdate datetime, levelid int)
插入
进入 #employee
值 ( 1 ,
10 ,
'1/1/2019' , 1 ),
values ( 1, 10, '1/1/2019', 1),
( 1 ,
20 ,
'2/1/2019' , 2 ),
(1, 20, '2/1/2019', 2),
( 1 ,
30 ,
'3/1/2019' , 3 ),
(1, 30, '3/1/2019', 3),
( 1 ,
40 ,
'3/15/2019' , 1 )
(1, 40, '3/15/2019', 1)
创建
表 #employee_basetest ( employeeId
int , basetest_id
int )
create table #employee_basetest(employeeId int, basetest_id int)
插入
进入 #employee_basetest
值 ( 1 ,
10 )
创建
表 #employee_status ( statusid
< span style ="font-size:9.5pt; line-height:115%; font-family:Consolas; color:blue"> int ,
Followup_test_id int ,
status
varchar ( 200 ")
create table #employee_status(statusid int, Followup_test_id int, status varchar(200))
基础测试有可能从
#employee_basetest然后必须重新计算状态。
例如,当测试10删除时,20成为基础测试,状态必须更改为
状态ID &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
跨度> Followup_test_id <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
跨度> <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
跨度>状态
Status Id Followup_test_id Status
1 <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
跨度> 30 <跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; 跨度>
<跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
跨度> <跨度>&NBSP;&NBSP;&NBSP;&NBSP; 从2级移到3
1 30 Moved from level 2 to 3
TIA
推荐答案
美好的一天,
我不清楚一个重要的观点
One huge point is not clear to me
你想要一张桌子"employee_status"吗?每次"雇员"时,它实际上都会动态更新。表格已更改,或者您是否需要在查询执行时返回状态的查询?
Do you want to have a table "employee_status" which actually dynamically updated each time the "employee" table changed, or do you need query that returns the status at the time the query executed?
在第一种情况下,您需要在源表上使用触发器 " employee"上的动作的插入,删除,更新和触发器内则需要更新表" employee_status"
In first case, you need to use triggers on the source table "employee" on the actions INSERT,DELETE,UPDATE and inside the trigger you need to update the table "employee_status".
在第二种情况下,如何&NBSP;" employee_status"与问题有关吗?我们只需要一个查询来获取来自 ""employee"的数据。表,解析它,并显示结果SET
In the second case, how the "employee_status" is relevant to the question? We simply need a query which get the data from "employee" table, parse it, and present the result SET
这篇关于表数据基于其他表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!