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

前端 未结 5 1373
名媛妹妹
名媛妹妹 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:27

    I think that original design is a bit off, hence the complexity. The design below suggests daily (or more frequent) entries, but only count for the day, for a specific application, by a specific person. The Kimball star schema allows for easy slicing and dicing by date, by month, by year, by application, by person, by job title etc.

    For example, across all applications in years 2008, 2009, 2010

    SELECT  sum(TestCount) AS "Test Count"
    FROM    factTest AS f
            JOIN dimApplication AS a ON a.ApplicationID = f.ApplicationID
            JOIN dimPerson AS p ON p.PersonID = f.PersonID
            JOIN dimDate AS d ON d.DateID = f.DateID
    WHERE [Year] BETWEEN 2008 AND 2010
    

    Across all applications in year 2009, only on Fridays

    WHERE [Year] = 2009 AND DayOfWeek = 'Friday'
    

    Across all applications in year 2009, by person.

    SELECT  FullName, sum(TestCount) AS "Test Count"
    FROM    factTest AS f
            JOIN dimApplication AS a ON a.ApplicationID = f.ApplicationID
            JOIN dimPerson AS p ON p.PersonID = f.PersonID
            JOIN dimDate AS d ON d.DateID = f.DateID
    WHERE [Year] = 2009
    GROUP BY FullName
    

    By application, by person, by month in year 2009, but only on weekends

    SELECT  ApplicationName, FullName, [MonthName], sum(TestCount) AS "Test Count"
    FROM    factTest AS f
            JOIN dimApplication AS a ON a.ApplicationID = f.ApplicationID
            JOIN dimPerson AS p ON p.PersonID = f.PersonID
            JOIN dimDate AS d ON d.DateID = f.DateID
    WHERE [Year] = 2009 AND IsWeekend = 'Yes'
    GROUP BY ApplicationName, FullName, [MonthName]
    

    Across all applications for years 2000-2009, by year, by month, but only for tests done by a receptionist on Tuesdays.

    SELECT  [Year], [Month], sum(TestCount) AS "Test Count"
    FROM    factTest AS f
            JOIN dimApplication AS a ON a.ApplicationID = f.ApplicationID
            JOIN dimPerson AS p ON p.PersonID = f.PersonID
            JOIN dimDate AS d ON d.DateID = f.DateID
    WHERE   [Year] BETWEEN 2000 AND 2009
            AND JobTitle = 'Receptionist'
            AND DayOfWeek = 'Tuesday'
    GROUP BY [Year], [Month]
    

    Across all applications, for year 2009, tests done on weekends by short people who own two or more cats.

    SELECT  sum(TestCount) AS "Test Count"
    FROM    factTest AS f
            JOIN dimApplication AS a ON a.ApplicationID = f.ApplicationID
            JOIN dimPerson AS p ON p.PersonID = f.PersonID
            JOIN dimDate AS d ON d.DateID = f.DateID
    WHERE [Year] = 2009
          AND IsWeekend = 'Yes'
          AND IsShortPerson ='Yes'
          AND CatsOwned >= 2
    

    Etc...

    The syntax is sql server, but there is nothing special here.

    UPDATE
    Notice that FROM ... JOIN ... JOIN ... JOIN ... is always the same. All slicing and dicing is done via SELECT, WHERE, and GROUP BY -- no "complex queries" required.

提交回复
热议问题