Skip to main content
Inspiring
August 22, 2016
Question

missing dates on query output.

  • August 22, 2016
  • 5 replies
  • 548 views

I'm using cf 9 on a Windows platform

I can run an SQL statement wherein my students who have been absent for 30 days will appear on this output list. Where I am having an issue is that next to each instructor I would like to show the student's last date of attendance. This somehow I was not showing up when I run my query. The query that I am using is as follows:

SELECT Stuid, trim(concat(name.fname,' ',name.lname)) AS OVER_30_Days, ltfname as instructor, Max(meedate) FROM name LEFT JOIN meeting ON meeStuid = Stuid and meedate BETWEEN NOW() - INTERVAL 31 DAY AND NOW() LEFT JOIN squadlt on squadlt = ltid WHERE meeStuid is null AND  type = 'pupil' AND city = 'jackson' AND status = 'a' AND Stuid <> '432' and Stuid <> '5' and Stuid <> '183'  ORDER BY ltfname, OVER_30_Days

The resulting list below displays as such with null values in the maxdate column . Is there something that I am doing wrong?

    This topic has been closed for replies.

    5 replies

    Carl Von Stetten
    Legend
    August 23, 2016

    This question is also posted to StackOverflow here: http://stackoverflow.com/questions/39082069/missing-dates-on-query-output

    BKBK
    Community Expert
    Community Expert
    August 23, 2016

    You are lucky. Looking again at your query, I am surprised it gave you any result at all. It needs to be improved some more after you apply the suggestions from WolfShade and EddieLotter.

    You should:

    1) qualify all column names with the respective table name followed by a dot;

    2) move the meedate condition to the WHERE clause;

    3) avoid givig a table and a column the same name, squadlt.

    The same query would then end up looking like the following (I have guessed which columns beloong to which tables):

    SELECT name.Stuid, trim(concat(name.fname,' ',name.lname)) AS OVER_30_Days, name.ltfname as instructor, Max(meeting.meedate) as maxMeeDate

    FROM name 

    LEFT JOIN meeting ON meeting.meeStuid = name.Stuid

    LEFT JOIN squadlt ON squadlt.squadlt = name.ltid

    WHERE meeting.meeStuid is null

    AND meeting.meedate BETWEEN DATEADD(day,-31,NOW()) AND NOW()

    AND  name.type = 'pupil'

    AND name.city = 'jackson'

    AND name.status = 'a'

    AND name.Stuid <> '432'

    AND name.Stuid <> '5'

    AND name.Stuid <> '183' 

    ORDER BY name.ltfname, OVER_30_Days

    BKBK
    Community Expert
    Community Expert
    August 23, 2016

    LEFT JOIN meeting ON meeting.meeStuid = name.Stuid

    LEFT JOIN squadlt ON squadlt.squadlt = name.ltid

    Then again, one would expect, for a join, something like

    LEFT JOIN meeting ON meeting.stuid = name.stuid

    LEFT JOIN squadlt ON squadlt.ltid= name.ltid

    EddieLotter
    Inspiring
    August 22, 2016

    I suspect you have a problem with your joins. Try changing "name LEFT JOIN meeting" to "name RIGHT JOIN meeting" and see if you get what you expect.

    Cheers

    Eddie

    WolfShade
    Legend
    August 22, 2016

    I don't know what database you're using, but in Oracle, if you do a MAX(anything) without giving it an alias, it just shows up as an undefined column.  Try:

    SELECT Stuid, trim(concat(name.fname,' ',name.lname)) AS OVER_30_Days, ltfname as instructor,

         Max(meedate) as meedate

    FROM name LEFT JOIN meeting ON meeStuid = Stuid

    HTH,

    ^_^

    PS.. it's too late, I'm sure, but I wouldn't use reserved words for table or column names (you named a table "name".)

    BKBK
    Community Expert
    Community Expert
    August 22, 2016

    What about something like

    and meedate BETWEEN DATEADD(day,-31,NOW()) AND NOW()