![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
Hi Guys, DB2 LUW v9.5 I have two tables that does information from a Machine -- Active and Inactive. Also, *machine belongs to a department. I want to find out the count of Active and Inactive machines that belongs to each Department. I could do a simple: select DEPARTMENT_ID, ACTIVE, INACTIVE from * ( * select DEP.DEPARTMENT_ID , COUNT (M.MACHINE_ID) *as Active * from TBL_ACTIVE M, TBL_department DEP * where M.DEPARTMENT_ID = DEP.DEPARTMENT_ID group by DEP.DEPARTMENT_ID * ) as A, * ( * select DEP.DEPARTMENT_ID , COUNT (M.MACHINE_ID) *as INACTIVE * from TBL_INACTIVE M, TBL_department DEP * where M.DEPARTMENT_ID = DEP.DEPARTMENT_ID group by DEP.DEPARTMENT_ID * ) as I where A.DEPARTMENT_ID = I.DEPARTMENT_ID Performance of this simple query is critical and I am looking for ways to optimize it. I was hoping to find a better solution or one that does NOT scan any tables more than once -- in the example above, TBL_DEPARTMENT was used twice. Any ideas? Thanks |
#4
| |||
| |||
|
|
Try: select DEP.DEPARTMENT_ID * * *, COUNT (A.MACHINE_ID) as Active * * *, COUNT (N.MACHINE_ID) as Inactive * from TBL_department DEP * left outer join * * * *TBL_ACTIVE * * A * *on *A.DEPARTMENT_ID = DEP.DEPARTMENT_ID * left outer join * * * *TBL_INACTIVE * N * *on *N.DEPARTMENT_ID = DEP.DEPARTMENT_ID *group by * * * *DEP.DEPARTMENT_ID ; |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
Any ideas? |
![]() |
| Thread Tools | |
| Display Modes | |
| |