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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AslamJ
    New Member
    • Jul 2015
    • 5

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

    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

    Code:
    /* Calculate RunGroup */
    SELECT WeekNum, Abs_Status, (SELECT COUNT(*) FROM [SourceData] G WHERE G.Abs_Status <> GR.Abs_Status AND G.WeekNum <= GR.WeekNum) as RunGroup 
    INTO [RunGroup]
    FROM [SourceData] GR;
    GO
    
    /* Determine how many weeks in each run */
    SELECT Abs_Status, MIN(WeekNum) as StartDate, MAX(WeekNum) as EndDate, COUNT(*) as Games 
    INTO [WeeksinRun]
    FROM [RunGroup] A GROUP BY Abs_Status, RunGroup ORDER BY Min(WeekNum);
    GO
    
    
    /****** What to mark as Dropouts  ******/
    SELECT [StartDate]
          ,[EndDate]
      INTO [WeekstoUpd]
      FROM [WeeksinRun] a,[SourceData] b, [SourceData] c
      where a.[StartDate] = b.[Weeknum]
      and a.[EndDate] = c.[Weeknum]
      and b.[MONTH] <> c.[MONTH]
      and a.Abs_Status = '1'
      and a.[StartDate] <> '2012 Week 01';
    GO
    
    /****** Update Dropout Weeks  ******/
    update [SourceData]
    set [SourceData].[Abs_Status] = '-2'
      FROM [SourceData],[WeekstoUpd]
      where [WeekNum]>=[StartDate] and [WeekNum]<=[EndDate];
    GO
    
    /****** Update Absent Weeks  ******/
    UPDATE [SourceData]
    SET    [Abs_Status] = '-1'
    FROM   [SourceData]
    WHERE  [SourceData].[Abs_Status] = '1';
    GO
    SQL Fiddle Example - http://sqlfiddle.com/#!3/5018c/2
    Last edited by AslamJ; Jul 16 '15, 11:35 AM. Reason: Formatted Code correctly
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    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

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      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.

      Comment

      • AslamJ
        New Member
        • Jul 2015
        • 5

        #4
        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

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          You haven't answered any of my questions.

          Comment

          • AslamJ
            New Member
            • Jul 2015
            • 5

            #6
            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

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              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

              Comment

              • AslamJ
                New Member
                • Jul 2015
                • 5

                #8
                Then last 3 weeks of May as well as whole of June marked as DROPOUT

                Comment

                • ck9663
                  Recognized Expert Specialist
                  • Jun 2007
                  • 2878

                  #9
                  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

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    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

                    Comment

                    • AslamJ
                      New Member
                      • Jul 2015
                      • 5

                      #11
                      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

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        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.

                        Comment

                        Working...