create table DEFECT (id int identity not null, defect_type_description varchar(255), defect_type_id varchar(255), shape GEOMETRY, identified_on datetime2, municipality varchar(255) not null, municipality_key varchar(255), objectId varchar(255), object_type varchar(255), object_reference_real_property_key varchar(255), real_property_designation varchar(255), real_property_key varchar(255), primary key (id))
create table ADDRESS_AREA (objectId varchar(255) not null, created_at datetime2 not null, created_by varchar(255), last_sync datetime2, municipality varchar(255) not null, object_version int, updated_at datetime2 not null, updated_by varchar(255), version bigint, version_valid_from datetime2, version_valid_to datetime2, area_type varchar(255), assigned_name varchar(255), object_status varchar(255), part_of_municipality_reference varchar(255), place_name_id varchar(255), activityType varchar(255), primary key (objectId))
create table ADDRESS_PLACE (objectId varchar(255) not null, created_at datetime2 not null, created_by varchar(255), last_sync datetime2, municipality varchar(255) not null, object_version int, updated_at datetime2 not null, updated_by varchar(255), version bigint, version_valid_from datetime2, version_valid_to datetime2, activityType varchar(255), address_area_reference varchar(255), adressed_construction_type varchar(255), adressed_construction varchar(255), common_name_type varchar(255), common_name varchar(255), designation_number varchar(255), designation_number_letter varchar(255), deviate_from_standard int, staircase_identifier varchar(255), staircase_identifier_number int, designation_value varchar(255), farm_address_area_reference varchar(255), location_address_status varchar(255), object_status varchar(255), horizontal_evaluation_method varchar(255), horizontal_no_information int, horizontal_measure varchar(255), horizontal_statistic_method varchar(255), horizontal_value numeric(10,3), horizontal_coordinate_system varchar(255), shape GEOMETRY, position_kind varchar(255), post_code int, post_town varchar(255), real_property_key varchar(255), address_type varchar(255), primary key (objectId))
create table ADDRESS_PLACE_REMARK (id int identity not null, remark_type varchar(255), remark_value varchar(255), address_place_id varchar(255), primary key (id))
create table BUILDING (objectId varchar(255) not null, created_at datetime2 not null, created_by varchar(255), last_sync datetime2, municipality varchar(255) not null, object_version int, updated_at datetime2 not null, updated_by varchar(255), version bigint, version_valid_from datetime2, version_valid_to datetime2, activityType varchar(255), construction_year int, easement_building int, exempt_addressing int, extension_year int, house_number int, main_building int, non_freehold_property int, object_status varchar(255), belongs_to varchar(255), lies_on varchar(255), primary key (objectId))
create table BUILDING_NAME (id int identity not null, place_name_id varchar(255), name_type varchar(255), name_value varchar(255), building_id varchar(255), primary key (id))
create table BUILDING_PART (id int identity not null, shape GEOMETRY, horizontal_evaluation_method varchar(255), horizontal_no_information int, horizontal_measure varchar(255), horizontal_statistic_method varchar(255), horizontal_value numeric(10,3), horizontal_coordinate_system varchar(255), position_kind varchar(255), vertical_evaluation_method varchar(255), vertical_no_information int, vertical_measure varchar(255), vertical_statistic_method varchar(255), vertical_value numeric(10,3), vertical_coordinate_system varchar(255), building_id varchar(255), primary key (id))
create table BUILDING_REAL_PROPERTY_REFERENCE_AFFECTED_BY (building_id varchar(255) not null, affected_by varchar(255))
create table BUILDING_REMARK (id int identity not null, remark_type varchar(255), remark_value varchar(255), building_id varchar(255), primary key (id))
create table BUILDING_USE (DTYPE varchar(31) not null, id int identity not null, main_use int, uncertain_use int, usage varchar(255), useful_floor_space int, building_id varchar(255), primary key (id))
create table DWELLING (objectId varchar(255) not null, created_at datetime2 not null, created_by varchar(255), last_sync datetime2, municipality varchar(255) not null, object_version int, updated_at datetime2 not null, updated_by varchar(255), version bigint, version_valid_from datetime2, version_valid_to datetime2, activityType varchar(255), dwelling_category varchar(255), collection_method varchar(255), common_kitchen varchar(255), dwelling_number varchar(255), entrance_reference varchar(255), notification_of_numbering_date date, number_of_rooms int, numbering_date date, object_status varchar(255), real_property_key varchar(255), type_of_kitchen varchar(255), useful_floor_space double precision, primary key (objectId))
create table ENTRANCE (objectId varchar(255) not null, created_at datetime2 not null, created_by varchar(255), last_sync datetime2, municipality varchar(255) not null, object_version int, updated_at datetime2 not null, updated_by varchar(255), version bigint, version_valid_from datetime2, version_valid_to datetime2, activityType varchar(255), address_place_reference varchar(255), building_reference varchar(255), lift varchar(255), object_status varchar(255), type_of_entrance varchar(255), primary key (objectId))
create table EVENT (id int identity not null, entity varchar(255) not null, event varchar(255), id_ref varchar(255), message varchar(1048), municipality varchar(255) not null, resolved int, event_timestamp datetime2, user_ref varchar(255), primary key (id))
create table FARM_ADDRESS_AREA (objectId varchar(255) not null, created_at datetime2 not null, created_by varchar(255), last_sync datetime2, municipality varchar(255) not null, object_version int, updated_at datetime2 not null, updated_by varchar(255), version bigint, version_valid_from datetime2, version_valid_to datetime2, activityType varchar(255), address_area_reference varchar(255), assigned_name varchar(255), object_status varchar(255), place_name_id varchar(255), primary key (objectId))
create table MSB_SHELTER (objectId varchar(255) not null, created_at datetime2 not null, created_by varchar(255), last_sync datetime2, municipality varchar(255) not null, object_version int, updated_at datetime2 not null, updated_by varchar(255), version bigint, version_valid_from datetime2, version_valid_to datetime2, msb_shelter_identity varchar(255), primary key (objectId))
create table PART_OF_MUNICIPALITY (objectId varchar(255) not null, created_at datetime2 not null, created_by varchar(255), last_sync datetime2, municipality varchar(255) not null, object_version int, updated_at datetime2 not null, updated_by varchar(255), version bigint, version_valid_from datetime2, version_valid_to datetime2, activityType varchar(255), assigned_name varchar(255), municipality_key varchar(255), object_status varchar(255), place_name_id varchar(255), primary key (objectId))
create table REL_MSB_SHELTER_BUILDING (MsbShelterType_objectId varchar(255) not null, building_reference varchar(255))
create table REL_TAXATION_UNIT_BUILDING (TaxationUnitType_objectId varchar(255) not null, building_reference varchar(255))
create table REL_VALUE_UNIT_FOR_TAXATION_BUILDING (ValueUnitForTaxationType_objectId varchar(255) not null, building_reference varchar(255))
create table SYNCHRONIZATION_SUPPORT (id int identity not null, entity varchar(255) not null, last_sync datetime2 not null, municipality varchar(255) not null, primary key (id))
create table TAXATION_UNIT (objectId varchar(255) not null, created_at datetime2 not null, created_by varchar(255), last_sync datetime2, municipality varchar(255) not null, object_version int, updated_at datetime2 not null, updated_by varchar(255), version bigint, version_valid_from datetime2, version_valid_to datetime2, taxation_unit_identity varchar(255), primary key (objectId))
create table VALUE_UNIT_FOR_TAXATION (objectId varchar(255) not null, created_at datetime2 not null, created_by varchar(255), last_sync datetime2, municipality varchar(255) not null, object_version int, updated_at datetime2 not null, updated_by varchar(255), version bigint, version_valid_from datetime2, version_valid_to datetime2, value_unit_identity varchar(255), primary key (objectId))
create table BUILDING_ATTRIBUTE (id int identity not null, date1 datetime2, date2 datetime2, date3 datetime2, date4 datetime2, double1 double precision, double2 double precision, double3 double precision, double4 double precision, int1 int, int2 int, int3 int, int4 int, string1 varchar(255), string2 varchar(255), string3 varchar(255), string4 varchar(255), building_id varchar(255), primary key (id))
alter table SYNCHRONIZATION_SUPPORT add constraint UKcrf9bompydcqr2xcs1l5lb065 unique (municipality, entity)
alter table ADDRESS_AREA add constraint FK34hwehxk6l3ojy2jykxmxx6me foreign key (part_of_municipality_reference) references PART_OF_MUNICIPALITY
alter table ADDRESS_PLACE add constraint FKlrsa7gsspnhppmli2ku4dxhq2 foreign key (address_area_reference) references ADDRESS_AREA
alter table ADDRESS_PLACE add constraint FKrvcox4melhsgd6fqdmhph1elc foreign key (farm_address_area_reference) references FARM_ADDRESS_AREA
alter table ADDRESS_PLACE_REMARK add constraint FKagoh61s2na6rd72cqik087m3e foreign key (address_place_id) references ADDRESS_PLACE
alter table BUILDING_NAME add constraint FKfcvfimeyookf71xqyf4qflpsj foreign key (building_id) references BUILDING
alter table BUILDING_PART add constraint FK4e5y7kt3pcimowst0yabp03ih foreign key (building_id) references BUILDING
alter table BUILDING_REAL_PROPERTY_REFERENCE_AFFECTED_BY add constraint FK7fijlw3hk7rxbso8j3xacy3t4 foreign key (building_id) references BUILDING
alter table BUILDING_REMARK add constraint FKcidmgl62wimhml97g3s1whac6 foreign key (building_id) references BUILDING
alter table BUILDING_USE add constraint FK8r9uelffg812ycru4arvn49lw foreign key (building_id) references BUILDING
alter table DWELLING add constraint FKoi0vkodt2n04in8itk7m7rsbb foreign key (entrance_reference) references ENTRANCE
alter table ENTRANCE add constraint FKceurbuelwfs72m60ejr48i4i9 foreign key (address_place_reference) references ADDRESS_PLACE
alter table ENTRANCE add constraint FKfj6tbl7peur0mtdrmq6w4ams3 foreign key (building_reference) references BUILDING
alter table FARM_ADDRESS_AREA add constraint FK66cdjp3lo2gecsv5uemr57ger foreign key (address_area_reference) references ADDRESS_AREA
alter table REL_MSB_SHELTER_BUILDING add constraint FKhl778me93abyabt0i56dt9eke foreign key (MsbShelterType_objectId) references MSB_SHELTER
alter table REL_TAXATION_UNIT_BUILDING add constraint FK9tpw6hm6w66qf5i3khyspqq0s foreign key (TaxationUnitType_objectId) references TAXATION_UNIT
alter table REL_VALUE_UNIT_FOR_TAXATION_BUILDING add constraint FK68ci5kxsmed6nuath1lru1okr foreign key (ValueUnitForTaxationType_objectId) references VALUE_UNIT_FOR_TAXATION
alter table BUILDING_ATTRIBUTE add constraint FK2r1798nlgfek9hmokoadnde3n foreign key (building_id) references BUILDING
GO
create view [dbo].[EVENT_LOG] as
SELECT * FROM
(
-- Part Of Municipality
SELECT et.id, pom.municipality, pom.objectId, null, pom.assigned_name, pom.updated_at,
et.entity, et.event, et.event_timestamp, et.message, et.user_ref, pom.activityType, DATEDIFF(DAY, et.event_timestamp, getdate())
FROM PART_OF_MUNICIPALITY pom
JOIN EVENT et ON pom.objectId = et.id_ref
UNION ALL
-- Address Area
SELECT et.id, aa.municipality, aa.objectId, null,
aa.assigned_name,
aa.updated_at,
et.entity, et.event, et.event_timestamp, et.message, et.user_ref, aa.activityType , DATEDIFF(DAY, et.event_timestamp, getdate())
FROM ADDRESS_AREA aa
JOIN EVENT et ON aa.objectId = et.id_ref
UNION ALL
-- Farm Address Area
SELECT et.id, faa.municipality, faa.objectId, cast(null as varchar),
aa.assigned_name +
coalesce(' ' + faa.assigned_name,'' ),
faa.updated_at,
et.entity, et.event, et.event_timestamp, et.message, et.user_ref, faa.activityType, DATEDIFF(DAY, et.event_timestamp, getdate())
FROM FARM_ADDRESS_AREA faa
JOIN EVENT et ON faa.objectId = et.id_ref
LEFT JOIN ADDRESS_AREA aa ON faa.address_area_reference = aa.objectId
UNION ALL
-- Address Place
SELECT et.id, ap.municipality, ap.objectId, ap.real_property_key,
aa.assigned_name +
coalesce(' ' + faa.assigned_name,'' ) +
coalesce(' ' + ap.designation_number, '') +
coalesce(ap.designation_number_letter + ' ', ' ') +
coalesce(ap.staircase_identifier, ''),
ap.updated_at,
et.entity, et.event, et.event_timestamp, et.message, et.user_ref, ap.activityType, DATEDIFF(DAY, et.event_timestamp, getdate())
FROM ADDRESS_PLACE ap
JOIN EVENT et ON ap.objectId = et.id_ref
LEFT JOIN FARM_ADDRESS_AREA faa ON ap.farm_address_area_reference = faa.objectId
LEFT JOIN ADDRESS_AREA aa ON ap.address_area_reference = aa.objectId OR faa.address_area_reference = aa.objectId
UNION ALL
-- Entrance
SELECT et.id, e.municipality, e.objectId, ap.real_property_key,
aa.assigned_name +
coalesce(' ' + faa.assigned_name,'' ) +
coalesce(' ' + ap.designation_number, '') +
coalesce(ap.designation_number_letter + ' ', ' ') +
coalesce(ap.staircase_identifier, ''),
e.updated_at,
et.entity, et.event, et.event_timestamp, et.message, et.user_ref, e.activityType, DATEDIFF(DAY, et.event_timestamp, getdate())
FROM ENTRANCE e
JOIN EVENT et ON e.objectId = et.id_ref
LEFT JOIN ADDRESS_PLACE ap ON e.address_place_reference = ap.objectId
LEFT JOIN FARM_ADDRESS_AREA faa ON ap.farm_address_area_reference = faa.objectId
LEFT JOIN ADDRESS_AREA aa ON ap.address_area_reference = aa.objectId OR faa.address_area_reference = aa.objectId
UNION ALL
-- Dwelling
SELECT et.id, d.municipality, d.objectId, ap.real_property_key,
aa.assigned_name +
coalesce(' ' + faa.assigned_name,'' ) +
coalesce(' ' + ap.designation_number, '') +
coalesce(ap.designation_number_letter + ' ', ' ') +
coalesce(ap.staircase_identifier, '') + 'lgh: ' +
coalesce(d.dwelling_number, '(saknas)'),
d.updated_at,
et.entity, et.event, et.event_timestamp, et.message, et.user_ref, d.activityType, DATEDIFF(DAY, et.event_timestamp, getdate())
FROM DWELLING d
JOIN EVENT et ON d.objectId = et.id_ref
LEFT JOIN ENTRANCE e ON e.objectId = d.entrance_reference
LEFT JOIN ADDRESS_PLACE ap ON e.address_place_reference = ap.objectId
LEFT JOIN FARM_ADDRESS_AREA faa ON ap.farm_address_area_reference = faa.objectId
LEFT JOIN ADDRESS_AREA aa ON ap.address_area_reference = aa.objectId OR faa.address_area_reference = aa.objectId
UNION ALL
-- BUILDING
SELECT et.id, b.municipality, b.objectId, b.lies_on, cast(coalesce('Byggnad ' + cast(b.house_number as varchar), '(Byggnadsnummer saknas)') as varchar),
b.updated_at, et.entity, et.event, et.event_timestamp, et.message, et.user_ref, b.activityType, DATEDIFF(DAY, et.event_timestamp, getdate())
FROM BUILDING b
JOIN EVENT et ON b.objectId = et.id_ref
) as T("id","municipality","objectid","property_reference","label", "updated_at","entity_type","event_type","event_timestamp","message", event_user, "activity_type", "days_in_queue" )
GO