I have two tables, EVENT and EVENT_REV
EVENT:
╔══════════╦════════════════════╗
║ EVENT_ID ║ SENT_INTO_WF_BY_ID ║
╠══════════╬════════════════════╣
║ 1 ║ null ║
║ 2 ║ null ║
║ 3 ║ null ║
║ 4 ║ null ║
║ 5 ║ null ║
╚══════════╩════════════════════╝
and EVENT_REV:
╔══════════════╦══════════╦═════════╦════════╦════════════╦══════════╗
║ EVENT_REV_ID ║ EVENT_ID ║ USER_ID ║ STATUS ║ VALID_FROM ║ VALID_TO ║
╠══════════════╬══════════╬═════════╬════════╬════════════╬══════════╣
║ 1 ║ 1 ║ 54 ║ 0 ║ 1000 ║ 1001 ║
║ 2 ║ 1 ║ 55 ║ 100 ║ 2000 ║ 2001 ║
║ 3 ║ 1 ║ 56 ║ 200 ║ 3000 ║ 3001 ║
║ 4 ║ 2 ║ 57 ║ 0 ║ 4000 ║ 4001 ║
║ 5 ║ 3 ║ 58 ║ 0 ║ 5000 ║ 5001 ║
║ 6 ║ 3 ║ 59 ║ 100 ║ 6000 ║ null ║
║ 7 ║ 4 ║ 60 ║ 0 ║ 7000 ║ null ║
║ 8 ║ 5 ║ 61 ║ 500 ║ 8000 ║ 8001 ║
║ 9 ║ 5 ║ 62 ║ 600 ║ 9000 ║ 9001 ║
╚══════════════╩══════════╩═════════╩════════╩════════════╩══════════╝
I want to update the EVENT table and set the SENT_INTO_WF_BY_ID
The rule for this is:
event_ids should match (EVENT.EVENT_ID = EVENT_REV.EVENT_ID)
take the row where STATUS is not equal to the STATUS with the lowest VALID_FROM. Which should be the row with the second lowest VALID_FROM
From that row, take the USER_ID
For example:
For the EVENT_ID = 1 it should select the 2nd row from EVENT_REV and put the USER_ID 55 into the SENT_INTO_WF_BY_ID
Because inner joins are not allowed for H2, my query looks like this:
UPDATE event ltm
SET ltm.sent_into_wf_by_id =
(SELECT top 1 ltmRev.user_id
FROM event_rev ltmRev
WHERE ltmRev.event_id = ltm.event_id
AND ltmRev.status !=
(SELECT top 1 EVENT_REV.status
FROM EVENT_REV
ORDER BY valid_from ASC nulls LAST)
ORDER BY ltmRev.valid_to ASC nulls LAST)
The result should look like:
╔══════════╦════════════════════╗
║ EVENT_ID ║ SENT_INTO_WF_BY_ID ║
╠══════════╬════════════════════╣
║ 1 ║ 55 ║
║ 2 ║ null ║
║ 3 ║ 59 ║
║ 4 ║ null ║
║ 5 ║ 62 ║
╚══════════╩════════════════════╝
but it's actually:
╔══════════╦════════════════════╗
║ EVENT_ID ║ SENT_INTO_WF_BY_ID ║
╠══════════╬════════════════════╣
║ 1 ║ 55 ║
║ 2 ║ null ║
║ 3 ║ 59 ║
║ 4 ║ null ║
║ 5 ║ 61 <-- wrong ║
╚══════════╩════════════════════╝
Could solve it with the following query:
UPDATE ltm_op_risk_event ltm
SET ltm.sent_into_wf_by_id =
(SELECT ltmRev.adm_user_id
FROM ltm_op_risk_event_rev ltmRev
WHERE ltmRev.ltm_op_risk_event_id = ltm.ltm_op_risk_event_id
AND ltmRev.status !=
(SELECT ltmRev2.status
FROM LTM_OP_RISK_EVENT_REV ltmRev2
WHERE valid_from IS NOT NULL
AND ltmRev.ltm_op_risk_event_id = ltmRev2.ltm_op_risk_event_id
ORDER BY valid_from ASC LIMIT 1)
ORDER BY ltmRev.valid_to ASC LIMIT 1)
WHERE ltm.sent_into_wf_by_id IS NULL;
The missing part was the AND ltmRev.ltm_op_risk_event_id = ltmRev2.ltm_op_risk_event_id in the innermost select. I first tested this connection with the wrong connections...
Related
I have a table with this structure:
╔══════╦════╦═════════╗
║ Comp ║ ID ║ Desc ║
╠══════╬════╬═════════╣
║ 1 ║ 1 ║ Comp1-1 ║
║ 1 ║ 2 ║ Comp1-2 ║
║ 3 ║ 2 ║ Comp3-2 ║
║ 1 ║ 3 ║ Comp1-3 ║
║ 1 ║ 4 ║ Comp1-4 ║
║ 3 ║ 5 ║ Comp3-5 ║
╚══════╩════╩═════════╝
The dataset I'm creating should have a unique ID.
If an ID exists in Comp1, use that Desc.
If it does not exist in Comp1, use Comp3.
End result should look like this instead:
╔══════╦════╦═════════╗
║ Comp ║ ID ║ Desc ║
╠══════╬════╬═════════╣
║ 1 ║ 1 ║ Comp1-1 ║
║ 1 ║ 2 ║ Comp1-2 ║
║ 1 ║ 3 ║ Comp1-3 ║
║ 1 ║ 4 ║ Comp1-4 ║
║ 3 ║ 5 ║ Comp3-5 ║
╚══════╩════╩═════════╝
I've tried using NOT EXISTS and joining with a subquery but I'm not sure what to Join on.
Using not exists, it looks like:
select t.*
from t
where t.descr like 'Comp1-%' or
not exists (select 1
from t t2
where t2.id = t.id and t2.descr like 'Comp1-%'
);
all
I've been trying to do, with data following the structure:
╔══════════════╦═══════════╦══╗
║ Alphabetical ║ Numerical ║ ║
╠══════════════╬═══════════╬══╣
║ A ║ 15 ║ ║
║ A ║ 30 ║ ║
║ E ║ 100 ║ ║
║ C ║ 45 ║ ║
║ F ║ 25 ║ ║
║ C ║ 65 ║ ║
║ B ║ 25 ║ ║
║ F ║ 35 ║ ║
║ C ║ 100 ║ ║
║ A ║ 10 ║ ║
║ C ║ 20 ║ ║
║ B ║ 5 ║ ║
║ E ║ 10 ║ ║
║ F ║ 85 ║ ║
║ D ║ 30 ║ ║
║ F ║ 1 ║ ║
╚══════════════╩═══════════╩══╝
To get the following:
╔══════════════╦══════╦═════════╗
║ Alphabetical ║ Rank ║ Numeric ║
╠══════════════╬══════╬═════════╣
║ A ║ 1 ║ 30 ║
║ A ║ 2 ║ 15 ║
║ A ║ 3 ║ 10 ║
║ B ║ 1 ║ 25 ║
║ B ║ 2 ║ 5 ║
║ C ║ 1 ║ 100 ║
║ C ║ 2 ║ 65 ║
║ C ║ 3 ║ 45 ║
║ C ║ 4 ║ 20 ║
║ D ║ 1 ║ 30 ║
║ E ║ 1 ║ 100 ║
║ E ║ 2 ║ 10 ║
║ F ║ 1 ║ 85 ║
║ F ║ 2 ║ 35 ║
║ F ║ 3 ║ 25 ║
║ F ║ 4 ║ 1 ║
╚══════════════╩══════╩═════════╝
Basically, to order the alphabetical field in ascending order, the numerical field in descending order and get the order or rank by using the order used for the numerical field, grouped by the alphabetical field.
I have only achieved it if I limit it to one specific value in the Alphabetical column, by using something like:
select ordered_src.*, ROWNUM Rank from (select src.* from Source src where alphabetical = 'A' order by Numeric desc) ordered_src;
But I have no idea how to get the result shown above. Any idea? Also, is there any alternative that will work also in mysql/mssql/etc?
Thanks!
Use row_number():
select s.*,
row_number() over (partition by alphabetical order by numerical desc) as rank
from source s
order by alphabetical, rank;
Hi i have postgres database and four tables
vehicles -> trips
vehicles -> component_values -> component_types
vehicles:
╔════╦══════════════════════════╦════════════════════════╦════════════════╦═════════╗
║ id ║ slug ║ name ║ manufacturer ║ model ║
╠════╬══════════════════════════╬════════════════════════╬════════════════╬═════════╣
║ 1 ║ manufacturer-x-model-3 ║ Manufacturer X Model 3 ║ Manufacturer X ║ Model 3 ║
║ 2 ║ manufacturer-x-model-1 ║ Manufacturer X Model 1 ║ Manufacturer X ║ Model 1 ║
║ 3 ║ manufacturer-x-model-1-1 ║ Manufacturer X Model 1 ║ Manufacturer X ║ Model 1 ║
╚════╩══════════════════════════╩════════════════════════╩════════════════╩═════════╝
trips:
╔═════╦════════════╦═════════════╦═════════════╦═════════════════╗
║ id ║ vehicle_id ║ name ║ mileage ║ recorded_at ║
╠═════╬════════════╬═════════════╬═════════════╬═════════════════╣
║ 1 ║ 1 ║ 10386735 ║ 386734.997 ║ 2/25/2014 13:56 ║
║ 2 ║ 1 ║ 11771530.14 ║ 771530.14 ║ 3/1/2014 19:41 ║
║ 3 ║ 1 ║ 121112028.4 ║ 1112028.39 ║ 3/5/2014 3:23 ║
║ 4 ║ 1 ║ 131406814.9 ║ 1406814.892 ║ 3/8/2014 20:56 ║
║ 5 ║ 1 ║ 141933528.7 ║ 1933528.711 ║ 3/13/2014 0:19 ║
║ 6 ║ 1 ║ 152256488.6 ║ 2256488.579 ║ 3/16/2014 21:15 ║
╚═════╩════════════╩═════════════╩═════════════╩═════════════════╝
component_values:
╔════╦═══════════════════╦═════════╦════════════╦════════════╦═════════════╦═════════════╗
║ id ║ component_type_id ║ trip_id ║ vehicle_id ║ mileage ║ damage ║ damage_eff ║
╠════╬═══════════════════╬═════════╬════════════╬════════════╬═════════════╬═════════════╣
║ 1 ║ 1 ║ 1 ║ 1 ║ 386734.997 ║ 0.002260565 ║ 0.002225831 ║
║ 2 ║ 2 ║ 1 ║ 1 ║ 386734.997 ║ 0.002260306 ║ 0.002238006 ║
║ 3 ║ 3 ║ 1 ║ 1 ║ 386734.997 ║ 0.002261288 ║ 0.002266295 ║
║ 4 ║ 4 ║ 1 ║ 1 ║ 386734.997 ║ 0.002054489 ║ 0.002060029 ║
║ 5 ║ 5 ║ 1 ║ 1 ║ 386734.997 ║ 0.002052669 ║ 0.002107272 ║
║ 6 ║ 6 ║ 1 ║ 1 ║ 386734.997 ║ NULL ║ NULL ║
║ 7 ║ 7 ║ 1 ║ 1 ║ 386734.997 ║ NULL ║ NULL ║
║ 8 ║ 1 ║ 2 ║ 1 ║ 771530.14 ║ 0.004792952 ║ 0.0048514 ║
║ 9 ║ 2 ║ 2 ║ 1 ║ 771530.14 ║ 0.004792404 ║ 0.004710451 ║
║ 10 ║ 3 ║ 2 ║ 1 ║ 771530.14 ║ 0.004794486 ║ 0.004805461 ║
╚════╩═══════════════════╩═════════╩════════════╩════════════╩═════════════╩═════════════╝
component_types:
╔════╦═════════════════════════════════════╦════════════════╦══════════════════════╗
║ id ║ slug ║ manufacturer ║ name ║
╠════╬═════════════════════════════════════╬════════════════╬══════════════════════╣
║ 6 ║ manufacturer-d-battery ║ Manufacturer D ║ Battery ║
║ 2 ║ manufacturer-b-differential-1 ║ Manufacturer B ║ Differential 1 ║
║ 3 ║ manufacturer-c-driveshaft-1 ║ Manufacturer C ║ Driveshaft 1 ║
║ 5 ║ manufacturer-c-gearbox-output-shaft ║ Manufacturer C ║ Gearbox output shaft ║
║ 1 ║ manufacturer-a-motor-1 ║ Manufacturer A ║ Motor 1 ║
║ 4 ║ manufacturer-c-gearbox-input-shaft ║ Manufacturer C ║ Gearbox input shaft ║
║ 7 ║ usage-profile ║ ║ Usage profile ║
╚════╩═════════════════════════════════════╩════════════════╩══════════════════════╝
and i'm trying to get the vehicles with the latest trip dates and mileage and also the heights and lowest damaged component for each vehicle
so the result will be like:
╔════════════╦══════════════════╦══════════════════════════╦═════════════════════════════════╦════════════════════════════════╦════════════════════════════════╦═══════════════════════════════╗
║ vehicle_id ║ latest_trip_date ║ latest_trip_date_mileage ║ heights_damaged_component_value ║ heights_damaged_component_name ║ lowest_damaged_component_value ║ lowest_damaged_component_name ║
╠════════════╬══════════════════╬══════════════════════════╬═════════════════════════════════╬════════════════════════════════╬════════════════════════════════╬═══════════════════════════════╣
║ 1 ║ 4/19/2014 3:27 ║ 4844305.912 ║ 0.029372972 ║ Gearbox input shaft ║ 0.002052669 ║ Gearbox output shaft ║
║ 2 ║ 5/19/2014 16:13 ║ 5567945.164 ║ 0.029405924 ║ Driveshaft 1 ║ 0.001864137 ║ Gearbox output shaft ║
║ 3 ║ 4/28/2014 12:55 ║ 5286175.763 ║ 0.030745029 ║ Driveshaft 1 ║ 0.001957685 ║ Differential 1 ║
║ 4 ║ 2/25/2014 3:32 ║ 5398006.007 ║ 0.030495792 ║ Driveshaft 1 ║ 0.001814434 ║ Differential 1 ║
║ 5 ║ 4/25/2014 9:51 ║ 5179558.475 ║ 0.032060074 ║ Gearbox input shaft ║ 0.001936431 ║ Differential 1 ║
║ 6 ║ 5/9/2014 7:43 ║ 5234355.804 ║ 0.030576454 ║ Gearbox input shaft ║ 0.002254191 ║ Gearbox output shaft ║
║ 7 ║ 6/21/2014 18:09 ║ 5705722.416 ║ 0.029957374 ║ Driveshaft 1 ║ 0.001653441 ║ Gearbox output shaft ║
║ 8 ║ 4/23/2014 5:25 ║ 5590470.028 ║ 0.031900163 ║ Driveshaft 1 ║ 0.002083733 ║ Gearbox output shaft ║
║ 9 ║ 3/28/2014 20:37 ║ 5598159.883 ║ 0.031208918 ║ Driveshaft 1 ║ 0.00162805 ║ Differential 1 ║
║ 10 ║ 5/24/2014 19:27 ║ 5020795.001 ║ 0.02962505 ║ Gearbox input shaft ║ 0.001729646 ║ Differential 1 ║
╚════════════╩══════════════════╩══════════════════════════╩═════════════════════════════════╩════════════════════════════════╩════════════════════════════════╩═══════════════════════════════╝
i already tried this query
select
vehicles.id as vehicle_id,
latest_trips.recorded_at as latest_trip_date,
latest_trips.mileage as latest_trip_date_mileage,
heights_damaged_components.damage as heights_damaged_component_value,
heights_damaged_components.name as heights_damaged_component_name,
lowest_damaged_components.damage as lowest_damaged_component_value,
lowest_damaged_components.name as lowest_damaged_component_name
from vehicles
left join (
SELECT t.id, t.vehicle_id, t.mileage, t.recorded_at
FROM public.trips t
inner JOIN (SELECT vehicle_id, MAX(recorded_at) as latest_trip_date FROM public.trips GROUP BY vehicle_id)
tm ON t.vehicle_id = tm.vehicle_id AND t.recorded_at = tm.latest_trip_date
)
as latest_trips on latest_trips.vehicle_id = vehicles.id
left join (
select ct.name, hd.component_type_id, hd.vehicle_id, hd.damage
from public.component_values as hd
INNER JOIN (
SELECT vehicle_id,
MAX(damage) as heights_damaged_component
FROM public.component_values
GROUP BY vehicle_id
)
hdm ON hd.vehicle_id = hdm.vehicle_id AND hd.damage = hdm.heights_damaged_component
join public.component_types as ct on ct.id = hd.component_type_id
)
as heights_damaged_components on heights_damaged_components.vehicle_id = vehicles.id
left join (
select ct.name, ld.component_type_id, ld.vehicle_id, ld.damage
from public.component_values as ld
INNER JOIN (
SELECT vehicle_id, MIN(damage) as lowest_damaged_component
FROM public.component_values
GROUP BY vehicle_id
)
ldm ON ld.vehicle_id = ldm.vehicle_id AND ld.damage = ldm.lowest_damaged_component
join public.component_types as ct on ct.id = ld.component_type_id
) as lowest_damaged_components on lowest_damaged_components.vehicle_id = vehicles.id
but i have like 10000 vehicles and big trips and component_values and this query give me the result in like 3 to 6 seconds, is their a batter way to do this with better performance and time?
can i use GENERATED columns in my case and how ?
please any help and many many thanks in advance.
I have this table(table name CH);
╔══════╦══════╦══════════╦══════════╗
║ ID_A ║ ID_B ║Category_A║Category_B║
╠══════╬══════╬══════════╬══════════╣
║ 1 ║ 1 ║ 1 ║ 5 ║
║ 1 ║ 2 ║ 1 ║ 5 ║
║ 1 ║ 3 ║ 1 ║ 5 ║
║ 1 ║ 1 ║ 2 ║ 5 ║
║ 1 ║ 3 ║ 2 ║ 5 ║
║ 1 ║ 1 ║ 3 ║ 5 ║
║ 1 ║ 2 ║ 3 ║ 5 ║
║ 2 ║ 1 ║ 1 ║ 4 ║
║ 2 ║ 2 ║ 1 ║ 3 ║
║ 2 ║ 2 ║ 2 ║ 2 ║
║ 2 ║ 2 ║ 3 ║ 1 ║
╚══════╩══════╩══════════╩══════════╝
ID_A = 1 & ID_B = 1 appears all in the Category_A(=1,2,3)
and also
ID_A = 2 & ID_B = 2 appears all in the Category_A(=1,2,3)
Are there anyway to select those kinds of rows?
I've tried select distinct or count distinct with condition but failed.
select * from ch group by ID_A, ID_B having count(ditinct Category_A)=4;
I expect table like this;
╔══════╦══════╦══════════╦══════════╗
║ ID_A ║ ID_B ║Category_A║Category_B║
╠══════╬══════╬══════════╬══════════╣
║ 1 ║ 1 ║ 1 ║ 5 ║
║ 1 ║ 1 ║ 2 ║ 5 ║
║ 1 ║ 1 ║ 3 ║ 5 ║
║ 2 ║ 2 ║ 1 ║ 3 ║
║ 2 ║ 2 ║ 2 ║ 2 ║
║ 2 ║ 2 ║ 3 ║ 1 ║
╚══════╩══════╩══════════╩══════════╝
This might work
SELECT *
FROM (
SELECT ID_A, ID_B, COUNT(DISTINCT Category_A) as COUNT_CAT_A
FROM CH
GROUP BY ID_A, ID_B
) X
WHERE COUNT_CAT_A = 3
I'm attempting to match up results of Employees schedules vs Reporting schedules. I need to output a report that shows all reports and who was assigned to them. The place I'm having the trouble is based on the day of week.
My ReportSchedule table looks something like this:
╔══════════════╦══════════════╦══════╦══════╦══════╦══════╦══════╦══════╦════╗
║ ReportID ║ Time ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠══════════════╬══════════════╬══════╬══════╬══════╬══════╬══════╬══════╬════╣
║ 1001 ║ 06:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 1002 ║ 06:48:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║
╚══════════════╩══════════════╩══════╩══════╩══════╩══════╩══════╩══════╩════╝
My EmployeesSchedule table looks something like this:
╔════════════╦══════════╦═════════════╦═══════════╦═══╦════╦═══╦════╦═══╦════╦════╗
║ EmployeeID ║ ReportID ║ ReportStart ║ ReportEnd ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠════════════╬══════════╬═════════════╬═══════════╬═══╬════╬═══╬════╬═══╬════╬════╣
║ 22001 ║ 1001 ║ 05:00:00 ║ 12:00:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22001 ║ 1002 ║ 05:00:00 ║ 12:00:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22001 ║ 1003 ║ 05:00:00 ║ 12:00:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1001 ║ 06:00:00 ║ 14:00:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22002 ║ 1002 ║ 06:00:00 ║ 14:00:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22002 ║ 1003 ║ 06:00:00 ║ 14:00:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
╚════════════╩══════════╩═════════════╩═══════════╩═══╩════╩═══╩════╩═══╩════╩════╝
What I would need based on the above is something like this:
╔════════════╦══════════╦══════════╦═══╦════╦═══╦════╦═══╦════╦════╗
║ EmployeeID ║ ReportID ║ Time ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠════════════╬══════════╬══════════╬═══╬════╬═══╬════╬═══╬════╬════╣
║ 22001 ║ 1001 ║ 06:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1002 ║ 06:48:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22001 ║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1003 ║ 07:18:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
╚════════════╩══════════╩══════════╩═══╩════╩═══╩════╩═══╩════╩════╝
The query I was running is below:
SELECT EmployeeSchedule.EmployeeID, ReportSchedule.ReportID, ReportSchedule.Time,
ReportSchedule.M, ReportSchedule.Tu, ReportSchedule.W, ReportSchedule.Th, ReportSchedule.F, ReportSchedule.Sa, ReportSchedule.Su
FROM ReportSchedule
INNER JOIN EmployeeSchedule on ReportSchedule.ReportID = EmployeeSchedule.ReportID
WHERE (
ReportSchedule.Time > EmployeeSchedule.ReportStart AND
ReportSchedule.Time < EmployeeSchedule.ReportEnd AND
(
(ReportSchedule.M=1) AND (ReportSchedule.M = EmployeeSchedule.M) OR
(ReportSchedule.Tu=1) AND (ReportSchedule.Tu = EmployeeSchedule.Tu) OR
(ReportSchedule.W=1) AND (ReportSchedule.W = EmployeeSchedule.W) OR
(ReportSchedule.Th=1) AND (ReportSchedule.Th = EmployeeSchedule.Th) OR
(ReportSchedule.F=1) AND (ReportSchedule.F = EmployeeSchedule.F) OR
(ReportSchedule.Sa=1) AND (ReportSchedule.Sa = EmployeeSchedule.Sa) OR
(ReportSchedule.Su=1) AND (ReportSchedule.Su = EmployeeSchedule.Su)
)
)
The results returned by this are not what I'm looking for as its not filtering out the days of the week where an employee doesn't do a report. Here is what is being returned:
╔════════════╦══════════╦══════════╦═══╦════╦═══╦════╦═══╦════╦════╗
║ EmployeeID ║ ReportID ║ Time ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠════════════╬══════════╬══════════╬═══╬════╬═══╬════╬═══╬════╬════╣
║ 22001 ║ 1001 ║ 06:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1002 ║ 06:48:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22001 ║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║
║ 22002 ║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║
╚════════════╩══════════╩══════════╩═══╩════╩═══╩════╩═══╩════╩════╝
What do I need to do to get the results I'm looking for?
Add a where clause where report <> 0? Also, you have the report id = report id in the where statement - I'm not sure that's necessary as that's your join clause. Still new to sql, so I'm not sure.
Problem solved.
I inserted the original query into a temp table, then run the following and get the needed data returned.
SELECT #Temp.EmployeeID, #Temp.ReportID, #Temp.Time,
CASE WHEN EmployeeSchedule.M = 0 THEN 0 ELSE 1 END AS M,
CASE WHEN EmployeeSchedule.Tu = 0 THEN 0 ELSE 1 END AS Tu,
CASE WHEN EmployeeSchedule.W = 0 THEN 0 ELSE 1 END AS W,
CASE WHEN EmployeeSchedule.Th = 0 THEN 0 ELSE 1 END AS Th,
CASE WHEN EmployeeSchedule.F = 0 THEN 0 ELSE 1 END AS F,
CASE WHEN EmployeeSchedule.Sa = 0 THEN 0 ELSE 1 END AS Sa,
CASE WHEN EmployeeSchedule.Su = 0 THEN 0 ELSE 1 END AS Su
FROM #Temp
INNER JOIN EmployeeSchedule on #Temp.ReportID = EmployeeSchedule.ReportID