Database design - how do I track information over time and query a table for latest data?

前端 未结 5 1505
名媛妹妹
名媛妹妹 2021-01-15 16:01

We are trying to track our applications in our department and our unit test usage so I have created a database to keep track of this. I have an Applications

5条回答
  •  清歌不尽
    2021-01-15 16:28

    I think people seem to be making this harder than it is.

    To resolve this you need two queries:

    1. Find me the latest entry for each application ID
    2. Using the latest entry for each application ID give me the sum of the unit test counts.

    SQL for the first is:

    SELECT application_ID, MAX(date_added) AS lastDateAdded FROM UnitTestTracking GROUP BY application_ID
    

    For the second we make this work by nesting queries:

    SELECT 
        SUM(unittestcount) 
    FROM 
        UnitTestTracking JOIN 
        (SELECT 
             application_ID, MAX(date_added) AS lastDateAdded 
         FROM 
             UnitTestTracking GROUP BY application_ID) T 
        ON UnitTestTracking.application_ID = T.application_ID AND 
           UnitTestTracking.date_added = T.LastDateAdded
    

    And that should give you what you need i.e. the current total number of unit tests.

提交回复
热议问题