| Author |
Topic |
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2008-12-29 : 05:17:47
|
| This is my table data.Table Name is AttendanceCardNo Dt OutCnt InCnt MisPunch a 12/12/2008 0 2 Null a 14/12/2008 2 1 Null a 12/12/2008 0 1 Null b 13/12/2008 1 1 Null c 13/12/2008 1 1 Null b 12/12/2008 0 2 Null b 12/12/2008 1 2 Null c 11/12/2008 1 0 Null c 13/12/2008 1 1 Null d 13/12/2008 0 1 NullI have to update table(Attendance) column Mispunch as M, based on both CardNo and Date when OutCnt or InCnt is equal to zero(0).I have tried, but its not working. This is my queryupdate Attendance set Mispunch = 'M'where CardNo in(select CardNo from Attendance where Dt in (select Dt from Attendance where OutCnt = 0 or InCnt = 0))any please help me, how to write query for this....its urgentSuresh Kumar |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-12-29 : 05:30:04
|
Your requirements are not clear. I think you need this,update Attendance set Mispunch = 'M'where OutCnt = 0 or InCnt = 0 |
 |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2008-12-29 : 05:36:28
|
| Thanks for ur reply Sakets..My requirements is, after executing the update query, The o/p should beCardNo Dt OutCnt InCnt MisPuncha 12/12/2008 0 2 Ma 14/12/2008 2 1 Nulla 12/12/2008 0 1 Mb 13/12/2008 1 1 Nullc 13/12/2008 1 1 Nullb 12/12/2008 0 2 Mb 12/12/2008 1 2 Nullc 11/12/2008 1 0 Mc 13/12/2008 1 1 Nulld 13/12/2008 0 1 MBut, when I'm executing my query, every row is updated. This is the o/p of my query.CardNo Dt OutCnt InCnt MisPuncha 12/12/2008 0 2 Ma 14/12/2008 2 1 Ma 12/12/2008 0 1 Mb 13/12/2008 1 1 Mc 13/12/2008 1 1 Mb 12/12/2008 0 2 Mb 12/12/2008 1 2 Mc 11/12/2008 1 0 Mc 13/12/2008 1 1 Md 13/12/2008 0 1 MYou can see the difference nowSuresh Kumar |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2008-12-29 : 05:39:11
|
| The CardNo 'a' has three differene dates, based on CardNo and Dt, the column Mispunch should get updated and the condition is OutCnt = 0 or InCnt = 0Suresh Kumar |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-29 : 05:39:35
|
| ah...so your reqmnt is changed? in that case what you need is query given by sakets |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-29 : 05:45:55
|
quote: Originally posted by sakets_2000
quote: Originally posted by visakh16 didnt we answered this?http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=116860
Have you tried running the query I posted before ?
it seems his reqmnts have changed from last post |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2008-12-29 : 06:49:18
|
| Use or Instead of and between tow checks,,,,Thanks...Like dat...OutCnt = 0 or InCnt = 0 |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-29 : 07:20:38
|
quote: Originally posted by soori457 This is my table data.Table Name is AttendanceCardNo Dt OutCnt InCnt MisPunch a 12/12/2008 0 2 Null a 14/12/2008 2 1 Null a 12/12/2008 0 1 Null b 13/12/2008 1 1 Null c 13/12/2008 1 1 Null b 12/12/2008 0 2 Null b 12/12/2008 1 2 Null c 11/12/2008 1 0 Null c 13/12/2008 1 1 Null d 13/12/2008 0 1 NullI have to update table(Attendance) column Mispunch as M, based on both CardNo and Date when OutCnt or InCnt is equal to zero(0).I have tried, but its not working. This is my queryupdate Attendance set Mispunch = 'M'where CardNo in(select CardNo from Attendance where Dt in (select Dt from Attendance where OutCnt = 0 or InCnt = 0) )any please help me, how to write query for this....its urgentSuresh Kumar
The highlighted subquery o/p is a,b,c,d so all the rows are getting updated since all of them has either a or b or c or d as card no use sakets solution it will solve ur problemJai Krishna |
 |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2008-12-29 : 23:10:19
|
| Everyone, Thanks for all ur replies. The solution provided by Visakh16 is correct.here's is the solution..UPDATE tSET t.Mispunched=1FROM(SELECT *,SUM(CASE WHEN In_Out='I' THEN 1 ELSE 0 END) OVER(PARTITION BY CardNo,Dt) AS InCount,SUM(CASE WHEN In_Out='O' THEN 1 ELSE 0 END) OVER(PARTITION BY CardNo,Dt) AS OutCountFROM YourTable)tWHERE t.InCount * t.OutCount=0Suresh Kumar |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 00:20:05
|
| welcome |
 |
|
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2008-12-30 : 01:02:55
|
| You are doing a Great Job...Visak16Suresh Kumar |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 01:04:25
|
| Thanks Soori |
 |
|
|
|