473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

SQL to determine if a non-attendance is a “DROPOUT”

5
I have an attendance table with attendance per week either "Present" or "Absent"

and need to have it changed as follows with reasons attached:

If a person does not attend for an entire month he is changed to a "DROPOUT" for the weeks he/she was absent before that month until the time he re-attended.

I've tried using SQL that check for "Streaks" checking for months with Combined Attendances but cannot get the "Dropped out from" to be correct. Any help will be highly appreciated.

Using Sample SourceData having 1 as "Absent" & 0 as "Present" , I've used the following SQL but have some bugs

Expand|Select|Wrap|Line Numbers
  1. /* Calculate RunGroup */
  2. SELECT WeekNum, Abs_Status, (SELECT COUNT(*) FROM [SourceData] G WHERE G.Abs_Status <> GR.Abs_Status AND G.WeekNum <= GR.WeekNum) as RunGroup 
  3. INTO [RunGroup]
  4. FROM [SourceData] GR;
  5. GO
  6.  
  7. /* Determine how many weeks in each run */
  8. SELECT Abs_Status, MIN(WeekNum) as StartDate, MAX(WeekNum) as EndDate, COUNT(*) as Games 
  9. INTO [WeeksinRun]
  10. FROM [RunGroup] A GROUP BY Abs_Status, RunGroup ORDER BY Min(WeekNum);
  11. GO
  12.  
  13.  
  14. /****** What to mark as Dropouts  ******/
  15. SELECT [StartDate]
  16.       ,[EndDate]
  17.   INTO [WeekstoUpd]
  18.   FROM [WeeksinRun] a,[SourceData] b, [SourceData] c
  19.   where a.[StartDate] = b.[Weeknum]
  20.   and a.[EndDate] = c.[Weeknum]
  21.   and b.[MONTH] <> c.[MONTH]
  22.   and a.Abs_Status = '1'
  23.   and a.[StartDate] <> '2012 Week 01';
  24. GO
  25.  
  26. /****** Update Dropout Weeks  ******/
  27. update [SourceData]
  28. set [SourceData].[Abs_Status] = '-2'
  29.   FROM [SourceData],[WeekstoUpd]
  30.   where [WeekNum]>=[StartDate] and [WeekNum]<=[EndDate];
  31. GO
  32.  
  33. /****** Update Absent Weeks  ******/
  34. UPDATE [SourceData]
  35. SET    [Abs_Status] = '-1'
  36. FROM   [SourceData]
  37. WHERE  [SourceData].[Abs_Status] = '1';
  38. GO
  39.  
SQL Fiddle Example - http://sqlfiddle.com/#!3/5018c/2
Jul 16 '15 #1
11 1895
ck9663
2,878 Expert 2GB
Can you change your sample data and tell us which WeekNum the person will be considered a DROP OUT? Your sample data does not have a month that's all absent. It all has at least 1 week present.

~~ CK
Jul 16 '15 #2
Rabbit
12,516 Expert Mod 8TB
I'm not sure I understand your exact requirements.

You said:
... does not attend for an entire month ...
Which means that if someone is absent for the last 3 weeks of May and absent for the first 3 weeks of June. Then they don't fall into your criteria and none of it gets marked dropout.

You said:
... he is changed to a "DROPOUT" for the weeks he/she was absent before that month ...
Which means if someone is absent for the first half of May, present for the last half of May, and absent for all of June, Then the first half of weeks in May are marked dropout but the weeks in June are marked absent. Which doesn't sound right to me, but it's what you are saying you want.

Having said that, no where in your SQL do you check to see that the "streak" is actually equal to a month. It would also be better for querying if you switch from strings representing week numbers and month / year to an actual date.
Jul 16 '15 #3
AslamJ
5
Output should be

Month Week Status
2013/01 2013 Week 01 -1
2013/01 2013 Week 02 -1
2013/01 2013 Week 03 -1
2013/01 2013 Week 04 0
2013/01 2013 Week 05 -1
2013/02 2013 Week 06 -1
2013/02 2013 Week 07 -1
2013/02 2013 Week 08 -1
2013/02 2013 Week 09 0
2013/03 2013 Week 10 0
2013/03 2013 Week 11 0
2013/03 2013 Week 12 0
2013/03 2013 Week 13 0
2013/04 2013 Week 14 -1
2013/04 2013 Week 15 -1
2013/04 2013 Week 16 -1
2013/04 2013 Week 17 0
2013/05 2013 Week 18 -1
2013/05 2013 Week 19 -1
2013/05 2013 Week 20 -1
2013/05 2013 Week 21 0
2013/05 2013 Week 22 -1
2013/06 2013 Week 23 -1
2013/06 2013 Week 24 -1
2013/06 2013 Week 25 0
2013/06 2013 Week 26 0
2013/07 2013 Week 27 -1
2013/07 2013 Week 28 -1
2013/07 2013 Week 29 -1
2013/07 2013 Week 30 0
2013/08 2013 Week 31 -1
2013/08 2013 Week 32 -1
2013/08 2013 Week 33 0
2013/08 2013 Week 34 -1
2013/08 2013 Week 35 -1
2013/09 2013 Week 36 -1
2013/09 2013 Week 37 -1
2013/09 2013 Week 38 -1
2013/09 2013 Week 39 0
2013/10 2013 Week 40 -1
2013/10 2013 Week 41 -1
2013/10 2013 Week 42 -1
2013/10 2013 Week 43 0
2013/10 2013 Week 44 -1
2013/11 2013 Week 45 -1
2013/11 2013 Week 46 -1
2013/11 2013 Week 47 -1
2013/11 2013 Week 48 0
2013/12 2013 Week 49 -1
2013/12 2013 Week 50 -1
2013/12 2013 Week 51 0
2013/12 2013 Week 52 -2
2013/12 2013 Week 53 -2
2014/01 2014 Week 01 -2
2014/01 2014 Week 02 -2
2014/01 2014 Week 03 -2
2014/01 2014 Week 04 -2
2014/01 2014 Week 05 -2
2014/02 2014 Week 06 -2
2014/02 2014 Week 07 -2
2014/02 2014 Week 08 0
2014/02 2014 Week 09 -2
2014/03 2014 Week 10 -2
2014/03 2014 Week 11 -2
2014/03 2014 Week 12 -2
2014/03 2014 Week 13 -2
Jul 17 '15 #4
Rabbit
12,516 Expert Mod 8TB
You haven't answered any of my questions.
Jul 17 '15 #5
AslamJ
5
Hi Rabbit

My apologies - to answer your questions

Which means that if someone is absent for the last 3 weeks of May and absent for the first 3 weeks of June. Then they don't fall into your criteria and none of it gets marked dropout......

YES, none gets marked as dropout.

Which means if someone is absent for the first half of May, present for the last half of May, and absent for all of June, Then the first half of weeks in May are marked dropout but the weeks in June are marked absent. Which doesn't sound right to me, but it's what you are saying you want.

Answer : 1st half of May marked as absent & June marked as dropout
Jul 21 '15 #6
Rabbit
12,516 Expert Mod 8TB
And what happens if they are absent the last 3 weeks of May and absent all of June?

You didn't respond to my last paragraph in post #3
Jul 21 '15 #7
AslamJ
5
Then last 3 weeks of May as well as whole of June marked as DROPOUT
Jul 21 '15 #8
ck9663
2,878 Expert 2GB
Simply put, they are absent when they are absent. However, if they are absent for, at least, four consecutive weeks, they are marked dropped out from the first week they are absent to the day prior they return.

Can you confirm that?


~~ CK
Jul 22 '15 #9
Rabbit
12,516 Expert Mod 8TB
I don't think that's correct. The consecutive weeks have to cover one calendar month. it doesn't matter if it's 6 consecutive weeks if those 6 weeks don't fully cover a month. See post #6 first paragraph.

Aslam,
What you will need to do is get the streaks from the query and a query that returns all the months where the count of absences for the month equals the total number of weeks in that month. Then compare the two to see which streaks contain a month fully covered by absences.

Also, you still have not responded to my last paragraph in post #3
Jul 22 '15 #10
AslamJ
5
Rabbit, your explanation is 100% Correct.

And with regards to paragraph 3, data is originally stored per week and month in the original database table
Jul 22 '15 #11
Rabbit
12,516 Expert Mod 8TB
Then what you'll need to do is implement what I laid out in post #10, second paragraph. You already have the first part, the query that returns the streaks. You need the rest of that paragraph.

I understand that's how it's stored in the table. What I'm saying is that, if feasible, it is better in the long run if you change how it's stored.
Jul 22 '15 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nathan Sokalski | last post by:
I want to determine the operating system using ASP (or VBScript inside of ASP). I tried to get it using the Request. ServerVariables(EnvironmentVariable) method. On the web page about ASP in which...
3
by: Shahid Juma | last post by:
Hello All, This may be a trivial question, but I was wondering how can you determine if a value contains an Integer or Float. I know there is a function called IsNumeric, however you can't...
5
by: Hennie de Nooijer | last post by:
Hi, This is a diffcult issue to explain. I hope to make my problem clear to you. SITUATION I'm building A SLA Query for a customer. This customer has an awkward way to determine the SLA results...
3
by: lucpustjens | last post by:
Hello, I want te determine the client operating system, because I need to kno the default cookie directory. If there is a way to determine the cooki directory directly, than it is also good. ...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
9
by: Adam | last post by:
Can someone please help!! I am trying to figure out what a font is? Assume I am working with a fixed font say Courier 10 point font Question 1: What does this mean 10 point font Question 2:...
2
by: Ryu | last post by:
Is there a way to determine if a text is ASCII or Unicode in C#. I have looked at Encoding classes but I have found that They dont allow me to pass a text to the encoding obj. In addition is there...
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
6
by: Jana | last post by:
Greetings Access Gurus! I am working on an app to send batch transactions to our bank, and the bank requires that we place an effective date on our files that is 'one business day in the future,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.