I'm currently working on a Sybase ASE rdms and I have the following table (JOB_AUDIT) filled with sample dataset :
| IDJOB | BATCH | M_DATE | START_TIME | ENDTIME |
|---|---|---|---|---|
| 1 | A | 12-25-2012 00:00:00 | 40100 | 40110 |
| 2 | A | 12-25-2012 00:00:00 | 40111 | 40118 |
| 3 | B | 12-25-2012 00:00:00 | 40118 | 40129 |
| 4 | A | 12-27-2012 00:00:00 | 40100 | 40112 |
| 5 | A | 12-27-2012 00:00:00 | 40113 | 40118 |
| 6 | C | 12-27-2012 00:00:00 | 40120 | 40134 |
....
What I want to do is at first to calculate the total execution time for each batch to run on a daily basis, then I want to calculate the pencentage each bach took on the daily execution.
so I created this query :
SELECT
T1.M_DATE as Batch_DATE,
T1.BATCH as Batch,
count(T1.IDJOB) as Nb_jobs,
sum(T1.END_TIME-T1.START_TIME) as Exec_Duration,
sum(T1.END_TIME-T1.START_TIME)/T2.total as pct
FROM
JOB_AUDIT T1
inner join
(select M_DATE,sum(END_TIME-START_TIME) as total from AUDIT_REP group by M_DATE) T2
ON
T1.M_DATE = T2.M_DATE
GROUP BY T1.M_DATE,T1.M_BATCH;
the T2 table should give me an output like this :
| M_DATE | total |
|---|---|
| 12-25-2012 00:00:00 | 28 |
| 12-27-2012 00:00:00 | 31 |
then the final table should look like :
| Batch_DATE | Batch | Nb_jobs | Exec_Duration | pct |
|---|---|---|---|---|
| 12-25-2012 00:00:00 | A | 2 | 17 | 0.607 |
| 12-25-2012 00:00:00 | B | 1 | 11 | 0.393 |
| 12-27-2012 00:00:00 | A | 2 | 17 | 0.548 |
| 12-27-2012 00:00:00 | C | 1 | 14 | 0.452 |
but it seems like the the join isn't working as I have the following result :
| Batch_DATE | Batch | Nb_jobs | Exec_Duration | pct |
|---|---|---|---|---|
| 12-25-2012 00:00:00 | A | 2 | 17 | 0.607 (17/28) |
| 12-25-2012 00:00:00 | A | 2 | 17 | 0.548 (17/31) not correct |
| 12-25-2012 00:00:00 | B | 1 | 11 | 0.393 (11/28) |
| 12-25-2012 00:00:00 | B | 1 | 11 | 0.355 (11/31) not correct |
| 12-27-2012 00:00:00 | A | 2 | 17 | 0.607 (17/28) not correct |
| 12-27-2012 00:00:00 | A | 2 | 17 | 0.548 (17/31) |
| 12-27-2012 00:00:00 | C | 1 | 14 | 0.500 (14/28) not correct |
| 12-27-2012 00:00:00 | C | 1 | 14 | 0.452 (14/31) |
So, I was wondering why I couldn't use a date a key, I'm sure there will always be a unique record per date in my T2 table as I grouped by date.
I thought the problem was that the record was a datetime to I even replaced all M_DATE fields by convert(date,M_DATE) and it changed nothing.
Note : I'm working with SYBASE ASE so I can't use window functions as it is not supported by this RDMS
Here is the problem --
-- I know you want to make the aggregation's result to divided with t2.total -- But the syntax here will take each matched line in t1 to sum first then divided with t2.total. So if you write it to --
-- Then it will give you the result you want -- Here's my test--
Hope it help