2013-07-26 77 views
0

我的表如下所示複雜的邏輯在SQL Server更新表

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) 

誰能幫助我的邏輯

+0

該字段**'app' **在哪裏? – Luv

+1

使用CASE語句?你有什麼嘗試? –

+0

對不起這個字段是Att –

回答

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 
+0

下面的查詢做了詭計'UPDATE student_attendance_table SET att = CASE WHEN(h1 IS NULL)或(h2 IS NULL)or當h1 ='P'且h2 ='P'且h3 ='P'且h4 ='P'且h5 ='P'時,則THEN爲空或h4爲NULL或h5爲NULL則爲空THEN 'P' 當h1 ='P'且h2 ='P'且h3 ='P'THEN'1' 當h4 ='P'且h5 ='P'THEN'2' 其他'A' END WHERE regno ='「&reg&」'AND date ='「&dtp1.Text&」'AND year =「&year&」AND batch =「&batch&」' –

+1

@Dineshbabu - 請勿將代碼在評論中。更新您的原始問題。 –

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

+0

我需要用DATE,REGNO,YEAR和BATCH作爲鍵獲取行 –

+0

更新了答案。 – Luv

0

如果我要獲取一個值並插入到表中,首先我會編寫邏輯以查找特定學生的值(不管p,a,1/2 ..),然後我將啓動sql部分以更新找到的在SQL查詢中值....

說,爲1138M0345

  • 更新ATT我會取使用select ... where REGNO = '1138M0345' 和日期= '烏爾日期'
  • 檢查的可能性,記錄對於condition中的P,A,1和2語句
  • 存儲上一步驟的結果,並關閉查詢
  • 打開另一個查詢(主要是更新與在REGNO = 1138M0345語句),並更新「ATT」
+0

打開SQL查詢,我的意思是更新,插入或刪除一些記錄,並關閉連接..所以什麼記錄我需要操縱我先做,然後開始在SQL中做... –

+0

我可以請求你一個詳細的邏輯 –

+0

從哪裏知道學生是p,a,1或2是否來自vb格式 –

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 &"