hi all-- this should be cake for someone. i'm just missing something basic here. i'm trying to join two tables and return results that are consolidated
it's probably easiest to just check out my data and desired results:
TABLE:ENDS
DTEWEEK OFFICE OFFICEID REGION orgUnitID totEnds isAllWeeks isAllOffices isAllOrgUNits
2007-01-22 Belgium 526 Belgium 175 1 0 0 0
2007-01-22 NULL NULL Belgium 175 1 0 1 0
2007-01-22 NULL NULL NULL NULL 1 0 1 1
2007-01-29 Belgium 526 Belgium 175 2 0 0 0
2007-01-29 NULL NULL Belgium 175 2 0 1 0
2007-01-29 NULL NULL NULL NULL 2 0 1 1
NULL NULL NULL NULL NULL 3 1 1 1
TABLE:STARTS
DTEWEEK OFFICE OFFICEID REGION orgUnitID totStarts isAllWeeks isAllOffices isAllOrgUNits
2007-01-15 Belgium 526 Belgium 175 3 0 0 0
2007-01-15 NULL NULL Belgium 175 3 0 1 0
2007-01-15 NULL NULL NULL NULL 3 0 1 1
2007-01-22 Belgium 526 Belgium 175 2 0 0 0
2007-01-22 NULL NULL Belgium 175 2 0 1 0
2007-01-22 NULL NULL NULL NULL 2 0 1 1
NULL NULL NULL NULL NULL 5 1 1 1
DESIRED RESULTS:
DTEWEEK | OFFICE | OFFICEID | REGION | orgUnitID | totStarts | totEnds | isAllWeeks| isAllOffices |isAllOrgUNits
2007-01-15 | Belgium | 526 | Belgium | 175 | 3 | null | 0 | 0 | 0
2007-01-15 | NULL | NULL | Belgium | 175 | 3 | null | 0 | 1 | 0
2007-01-15 | NULL | NULL | NULL | NULL | 3 | null | 0 | 1 | 1
2007-01-22 | Belgium | 526 | Belgium | 175 | 2 | 1 | 0 | 0 | 0
2007-01-22 | NULL | NULL | Belgium | 175 | 2 | 1 | 0 | 1 | 0
2007-01-22 | NULL | NULL | NULL | NULL | 2 | 1 | 0 | 1 | 1
2007-01-29 | Belgium | 526 | Belgium | 175 | null | 2 | 0 | 0 | 0
2007-01-29 | NULL | NULL | Belgium | 175 | null | 2 | 0 | 1 | 0
2007-01-29 | NULL | NULL | NULL | NULL | null | 2 | 0 | 1 | 1
NULL | NULL | NULL | NULL | NULL | 5 | 3 | 1 | 1 | 1
Sorry the data is a bit messy.
this does not work, but it's ther direction I'm headed:
SELECT COALESCE(s.dteWeek,e.dteWe
ek) as dteWeek
,COALESCE(s.officeName,e.o
fficeName)
as officeName
,COALESCE(s.officeID,e.off
iceID) as officeID
,COALESCE(s.region,e.regio
n) as region
,COALESCE(s.orgUnitID,s.or
gUnitID) as orgUnitID
,s.totStarts
,e.totEnds
,COALESCE(s.isAllWeeks,e.i
sAllWeeks)
as isAllWeeks
,COALESCE(s.isAllOffices,e
.isAllOffi
ces) as isAllOffices
,COALESCE(s.isAllOrgUnits,
e.isAllOrg
Units) as isAllOrgUnits
FROM #starts s FULL OUTER JOIN
(SELECT *
FROM #ends
) e
ON s.dteWeek = e.dteWeek
AND s.OfficeID = e.OfficeID
AND s.orgUnitID = e.orgUnitID
AND s.isAllWeeks = e.isAllWeeks
AND s.isAllOrgUnits = e.isAllOrgUnits
AND s.isAllOffices = e.isAllOffices
AND s.region = e.region
AND s.officeName = e.officeName
ORDER BY dteWeek
thanks in advance for your help...
Start Free Trial