我的表格如下所示
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 field是为了存储学生当天的出勤率。我需要在以下条件下用 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)
任何人都可以帮助我解决逻辑
答案 0 :(得分: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
答案 1 :(得分: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 Fiddle 强>
答案 2 :(得分:0)
如果我要获取一个值并首先插入表中,我会编写逻辑来查找特定学生的值(无论是p,a,1/2 ..)然后我会启动sql部分来更新在sql查询中找到的值....
说要更新1138M0345的att
select ... where
regNo ='1138M0345'和Date ='ur date'来获取该记录condition
声明1138M0345
),并更新'att'答案 3 :(得分: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 &"