0

我的表如下所示

Regno      Date       Year Batch h1   h2   h3  h4    h5   Att
1138M0345  25-07-2013  3    1    P    P    P  NULL  NULL  NULL
1138M0346  25-07-2013  3    1    P    P    P  NULL  NULL  NULL
1138M0347  25-07-2013  3    1    P    P    P  NULL  NULL  NULL
1138M0348  25-07-2013  3    1    P    P    P  NULL  NULL  NULL
1138M0349  25-07-2013  3    1    P    P    P  NULL  NULL  NULL
1138M0350  25-07-2013  3    1    P    P    P  NULL  NULL  NULL
1138M0351  25-07-2013  3    1    P    P    P  NULL  NULL  NULL
1138M0352  25-07-2013  3    1    P    P    P  NULL  NULL  NULL
1138M0353  25-07-2013  3    1    P    P    P  NULL  NULL  NULL
1138M0343  25-07-2013  3    1    A    A    A  NULL  NULL  NULL
1138M0344  25-07-2013  3    1    A    A    A  NULL  NULL  NULL

h1、h2、h3、h4、h5、att 字段可以存储空值att 字段是存储学生当天的出勤情况。在以下情况下,我需要使用P/A/1/2更新字段att

1. 'P' if H1 through H5 contains 'P' (Meaning present for the whole day)
2. '1' if H1 through H3 contains 'P' (Meaning present for the first session)
3. '2' if H4 and H5 contains 'P' (Meaning present for the second session)
4. NULL if any of H1 through H5 contains NULL (Meaning table needs to be updated completely)
5. Else 'A' (Meaning absent for the whole day)

谁能帮我解释一下逻辑

4

4 回答 4

1

这是一个查询,它将更新指定日期范围的表

DECLARE @StartDate datetime
DECLARE @EndDate datetime

SET @StartDate = '07/01/2013'
SET @EndDate = '07/26/2013'

UPDATE StudentAttendance
SET Att = 
 (
    CASE
       WHEN (H1 is null) or (H2 is null) or (H3 is null) (H4 is null) (H5 is null) THEN NULL
       WHEN H1='P' and H2='P' and H3='P' and H4='P' and H5='P' THEN 'P'
       WHEN H1='P' and H2='P' and H3='P' THEN 'P'
       WHEN H4='P' and H5='P' THEN 'P'
       ELSE 'A' 
    END
)
WHERE Date BETWEEN @StartDate and @EndDate
于 2013-07-26T09:11:40.817 回答
0

有效的查询是

UPDATE student_attendance_table SET att = 
  CASE 
    WHEN (h1 IS NULL) or (h2 IS NULL) or (h3 IS NULL) or (h4 IS NULL) or (h5 IS NULL) THEN NULL 
    WHEN h1='P' and h2='P' and h3='P' and h4='P' and h5='P' THEN 'P' 
    WHEN h1='P' and h2='P' and h3='P' THEN '1' 
    WHEN h4='P' and h5='P' THEN '2' 
    ELSE 'A' 
  END 
WHERE regno='" & reg & "' AND date='" & dtp1.Text & "' AND year=" & year & " AND batch= " & batch &"
于 2013-07-27T03:44:46.857 回答
0

您也可以编写 SQL 查询

select [Regno], [Date], [Year], [Batch] as KEY,
case 
when 
(ISNULL(H1,'')='' OR ISNULL(H2,'')='' 
OR ISNULL(H3,'')='' OR ISNULL(H4,'')='' 
OR ISNULL(H5,'')='') THEN NULL
when (H1='P' and H2='P' and H3='P' and H4='P' and H5='P') THEN 'P'
when (H1='P' and H2='P' and H3='P') THEN 'P'
when (H4='P' and H5='P') THEN 'P'
ELSE 'A' END as APP
from Table1

SQL小提琴

于 2013-07-26T05:09:12.167 回答
0

如果我要获取一个值并首先插入到表中,我将编写逻辑来查找特定学生的值(无论是 p、a、1/2 ..)然后我将启动 sql 部分以更新找到的值在 sql 查询中......

说要更新 1138M0345 的 att

  • 我会使用select ... whereregNo = '1138M0345' 和 Date ='ur date'来获取该记录
  • 检查condition语句中 P、A、1 和 2 的可能性
  • 存储上一步的结果,并关闭查询
  • 打开另一个查询(主要是使用 where regNo = 更新语句1138M0345),并更新“att”
于 2013-07-26T05:29:50.520 回答