Skip to main content
Inspiring
Aug 22, 2016 1:51 AM UTC
Question

missing dates on query output.

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 is closed to new replies. Start a new post to keep the conversation going.

    7 replies

    Carl Von Stetten
    Legend
    Aug 23, 2016 11:00 AM UTC

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

    Translate
    BKBK
    Community Expert
    Community Expert
    Aug 23, 2016 4:58 AM UTC

    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

    Translate
    BKBK
    Community Expert
    Community Expert
    Aug 23, 2016 5:01 AM UTC

    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

    Translate
    EddieLotter
    Inspiring
    Aug 22, 2016 10:00 PM UTC

    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

    Translate
    WolfShade
    Legend
    Aug 22, 2016 6:59 PM UTC

    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".)

    Translate
    BKBK
    Community Expert
    Community Expert
    Aug 22, 2016 6:04 PM UTC

    What about something like

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

    Translate