Hi guys,
I need to create a monthly report that uses data from 2 existing database tables - Table A and Table B. My plan is to create a new table - Table C and write data to it from Tables A & B on the daily. Table C would then be used to generate the monthly report. It will have 12 columns (Column A will be the date and Columns B thru L will hold report data from Tables A & B). All 3 tables will reside in the same database.
- Step 1: Create New Table C
- Step 2: Insert Table A data into Columns G thru L of Table C
- Data will be inserted at the end of each day (11:59:59 PM)
- Need to use aggregate functions to determine daily Min, Max, and Avg values
Draft code:
INSERT INTO Table C (Column_G, Column_H, Column_I, Column_J, Column_K, Column_L)
SELECT
DATEPART(Year, time_stamp) AS YEAR
DATEPART(Month, time_stamp) AS MONTH
DATEPART(Day, time_stamp) AS DAY
Min(Results_PSI) AS Column_G,
Max(Results_PSI) AS Column_H,
Avg(Results_PSI) AS Column_I,
Min(Results_TMP) AS Column_J,
Max(Results_TMP) AS Column_K,
Avg(Results_TMP) AS Column_L,
FROM Table A
GROUP BY DATEPART(Year, time_stamp), DATEPART(Month, time_stamp), DATEPART(Day, time_stamp),
ORDER BY YEAR, MONTH, DAY
- Step 2: Update Table C data entry (row) from Step 1 by inserting data into Columns B thru F
- This update/data insert will occur at 1:30am of the following day (data is representative of yesterdayâs total)
Draft code:
INSERT INTO Table C (Column_B, Column_C, Column_D, Column_E, Column_F)
SELECT * FROM Table B
ORDER BY time_stamp
Questions:
-
How do I specify the time ( in this case 11:59:59PM) for the data to be inserted into Table C in Step 1?
-
In Step 2, how do I update the row of data (inserted in Step 1) with additional data from Table B without creating a separate row? Since the update will occur at 1:30am on the following day, the time stamp will be different than the one of the data that was inserted in Step 1.
-
Any better/easier ways of accomplishing the above? I donât think I can use transaction groups to do what I want to do.
Any help would be much appreciated.