How Do I Query Latest Dates across Columns?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WarcraftNoob
    New Member
    • Mar 2007
    • 8

    How Do I Query Latest Dates across Columns?

    This is what I have right now,
    [code=mysql]
    SELECT Project, S_on, P_on, H_on, A_on, I_on, 1_on, 2_on, 3_on, 4_on, Z_on, D_on, T_on, K_on, F_on, J_on, FINAL_on
    From DTable
    WHERE
    Class='XXX' AND
    Project like 'Proj%' AND QA= 'ST' AND FINAL_on is not NULL
    [/code]
    This will query make me a nice big table,
    Code:
    Proj1.xxx  DATE DATE DATE DATE DATE NULL DATE.... FINAL_onDATE
    Proj1.xxx  DATE DATE DATE DATE DATE DATE DATE.... FINAL_onDATE
    Proj1.xxx  DATE NULL DATE DATE NULL DATE DATE.... FINAL_onDATE
    Proj2.xxx  DATE DATE DATE DATE DATE DATE DATE.... FINAL_onDATE
    Proj2.xxx  DATE DATE DATE DATE DATE DATE DATE.... FINAL_onDATE
    Proj3.xxx  DATE DATE DATE NULL DATE DATE DATE.... FINAL_onDATE
    where date are in the form DD-MMM-YY (eg 24-JUN-08)

    but what I want is the max (latest) date out of

    S_on, P_on, H_on, A_on, I_on, 1_on, 2_on, 3_on, 4_on, Z_on, D_on, T_on, K_on, F_on, J_on


    And then

    FINAL_on - maxdate(above) which would return number of days

    Then the avg of the days ( sum of days / count(projects) ) for each project


    So I would want something like to be queried
    Code:
    PROJ  XXX   FINAL_on-maxdate   FINAL_on - maxdate / XXX
    A     3      100               33.33
    B     4      200               50
    C     3      33                11
    D     7      2                 ...
    E     8      365               ....
    I just need the first and last columns to be displayed. Thanks :)
    Last edited by Atli; Jul 23 '08, 12:36 AM. Reason: Added [code] tags
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    I don't really understand what the data in this table is used for.

    What are all the X_on fields?

    If each of the X_on fields are meant to represent a milestone, leading to a final "turn in" date, would it not be best to store these in a separate table?
    Like:
    Code:
    Project
    -----------
    ProjectID Serial Primary Key
    ProjectName VarChar
    etc..
    ----------
    
    MileStone
    ----------
    MSID Serial Primary Key
    MSName VarChar
    MSDate DateTime
    ProjectID BigInt References Project(ProjectID)
    ----------
    Then you could query that table, rather than having to manually check the various fields in the main table.

    Comment

    Working...