Hello all.
I need a hand with a query, and need to ask for your brain power. This is very similar to a post I made in the past, which I thought I had working, but I still have an issue.
I have 2 tables
Table MAIN_TABLE
F_NAME
L_NAME
MINUTES_WORKED
Table VACATION_TABLE
F_NAME
L_NAME
VACATION_MINUTES
MAIN_TABLE (Example data)
F_NAME L_NAME WEEK_NUMBER MINUTES_WORKED
Al Smith 1 2000
Ben Johnson 6 2100
Cathy Stevens 7 1500
VACATION_TABLE (Example data)
F_NAME L_NAME VACATION_MINUTES
Al Smith 50
Cathy Stevens 90
Cathy Stevens 20
I want the results to appear like so;
F_NAME L_NAME MINUTES_WORKED VACATION_MINUTES
Al Smith 2000 50
Ben Johnson 2100 0
Cathy Stevens 1500 110
Using the below query , I get the following results
SELECT MAIN_TABLE.F_NAME , MAIN_TABLE.L_NAME, sum(MAIN_TABLE.MINUTES_WORKED) as MinutesWorked
sum(tmp.VACATION_MINUTES) as VacationMinutes
FROM MAIN_TABLE
LEFT JOIN(SELECT F_NAME, L_NAME, VACATION_MINUTES from VACATION_TABLE) as tmp
ON MAIN_TABLE.F_NAME = tmp.F_NAME AND
MAIN_TABLE.L_NAME = tmp.L_NAME AND
WHERE MAIN_TABLE.WEEK_NUMBER BETWEEN 1 and 8
group by MAIN_TABLE.F_NAME , MAIN_TABLE.L_NAME
F_NAME L_NAME MINUTES_WORKED VACATION_MINUTES
Al Smith 2000 50
Ben Johnson 2100 0
Cathy Stevens 3000 110
The MINUTES_WORKED for Cathy is doubled becuse there are 2 records in the VACTION_TABLE
---------------------------------------------------------------------------------------------------
To fix this I tried to sum the VACATION_MINUTES in the tmp table. I get the correct results, but the query takes 5 times as long to run.
SELECT MAIN_TABLE.F_NAME , MAIN_TABLE.L_NAME, sum(MAIN_TABLE.MINUTES_WORKED) as MinutesWorked
sum(tmp.VACATION_MINUTES) as VacationMinutes
FROM MAIN_TABLE
LEFT JOIN(SELECT F_NAME, L_NAME, sum(VACATION_MINUTES) as VACATION_MINUTES from VACATION_TABLE) as tmp
ON MAIN_TABLE.F_NAME = tmp.F_NAME AND
MAIN_TABLE.L_NAME = tmp.L_NAME AND
WHERE MAIN_TABLE.WEEK_NUMBER BETWEEN 1 and 8
group by MAIN_TABLE.F_NAME , MAIN_TABLE.L_NAME
F_NAME L_NAME MINUTES_WORKED VACATION_MINUTES
Al Smith 2000 50
Ben Johnson 2100 0
Cathy Stevens 1500 110
I get the correct results, but the query takes 5 times as long to run. Is there another way I can do this and keep the execution time down?
I need a hand with a query, and need to ask for your brain power. This is very similar to a post I made in the past, which I thought I had working, but I still have an issue.
I have 2 tables
Table MAIN_TABLE
F_NAME
L_NAME
MINUTES_WORKED
Table VACATION_TABLE
F_NAME
L_NAME
VACATION_MINUTES
MAIN_TABLE (Example data)
F_NAME L_NAME WEEK_NUMBER MINUTES_WORKED
Al Smith 1 2000
Ben Johnson 6 2100
Cathy Stevens 7 1500
VACATION_TABLE (Example data)
F_NAME L_NAME VACATION_MINUTES
Al Smith 50
Cathy Stevens 90
Cathy Stevens 20
I want the results to appear like so;
F_NAME L_NAME MINUTES_WORKED VACATION_MINUTES
Al Smith 2000 50
Ben Johnson 2100 0
Cathy Stevens 1500 110
Using the below query , I get the following results
SELECT MAIN_TABLE.F_NAME , MAIN_TABLE.L_NAME, sum(MAIN_TABLE.MINUTES_WORKED) as MinutesWorked
sum(tmp.VACATION_MINUTES) as VacationMinutes
FROM MAIN_TABLE
LEFT JOIN(SELECT F_NAME, L_NAME, VACATION_MINUTES from VACATION_TABLE) as tmp
ON MAIN_TABLE.F_NAME = tmp.F_NAME AND
MAIN_TABLE.L_NAME = tmp.L_NAME AND
WHERE MAIN_TABLE.WEEK_NUMBER BETWEEN 1 and 8
group by MAIN_TABLE.F_NAME , MAIN_TABLE.L_NAME
F_NAME L_NAME MINUTES_WORKED VACATION_MINUTES
Al Smith 2000 50
Ben Johnson 2100 0
Cathy Stevens 3000 110
The MINUTES_WORKED for Cathy is doubled becuse there are 2 records in the VACTION_TABLE
---------------------------------------------------------------------------------------------------
To fix this I tried to sum the VACATION_MINUTES in the tmp table. I get the correct results, but the query takes 5 times as long to run.
SELECT MAIN_TABLE.F_NAME , MAIN_TABLE.L_NAME, sum(MAIN_TABLE.MINUTES_WORKED) as MinutesWorked
sum(tmp.VACATION_MINUTES) as VacationMinutes
FROM MAIN_TABLE
LEFT JOIN(SELECT F_NAME, L_NAME, sum(VACATION_MINUTES) as VACATION_MINUTES from VACATION_TABLE) as tmp
ON MAIN_TABLE.F_NAME = tmp.F_NAME AND
MAIN_TABLE.L_NAME = tmp.L_NAME AND
WHERE MAIN_TABLE.WEEK_NUMBER BETWEEN 1 and 8
group by MAIN_TABLE.F_NAME , MAIN_TABLE.L_NAME
F_NAME L_NAME MINUTES_WORKED VACATION_MINUTES
Al Smith 2000 50
Ben Johnson 2100 0
Cathy Stevens 1500 110
I get the correct results, but the query takes 5 times as long to run. Is there another way I can do this and keep the execution time down?