Advertisement
Guest User

Untitled

a guest
Jun 14th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.79 KB | None | 0 0
  1. INSERT INTO [Terrorism].[FactAttack]
  2. SELECT *
  3. FROM (
  4.     SELECT
  5.     (
  6.         SELECT TOP 1 location_id
  7.         FROM [Terrorism].[DimLocation] loc
  8.         WHERE
  9.             loc.city = dest.city
  10.             AND loc.country = dest.country
  11.             AND loc.region = dest.region
  12.             AND ABS(loc.latitude - latitude) < 0.0001
  13.             AND ABS(loc.longtitude - longitude) < 0.0001
  14.     ) location_id,
  15.     (
  16.         SELECT TOP 1 date_id
  17.         FROM [Terrorism].[DimDate] dat
  18.         WHERE
  19.             dat.[day] = dest.[day]
  20.             AND dat.[month] = dest.[month]
  21.             AND dat.[year] = dest.[year]
  22.     ) date_id,
  23.     (
  24.         SELECT TOP 1 target_group_id
  25.         FROM [Terrorism].[DimTargetGroup] tar
  26.         WHERE tar.target_group_name = dest.targetype
  27.     ) target_group_id,
  28.     (
  29.         SELECT TOP 1 terrorist_group_id
  30.         FROM [Terrorism].[DimTerroristGroup] ter
  31.         WHERE ter.terrorist_group_name = dest.[group]
  32.     ) terrorist_group_id,
  33.     (
  34.         SELECT TOP 1 weapon_type_id
  35.         FROM [Terrorism].[DimWeaponType] weap
  36.         WHERE weap.weapon_type_name = dest.weapontype
  37.     ) weapon_type_id,
  38.     wounded,
  39.     killed
  40.     FROM [Terrorism].[dbo].[OLE DB Destination] dest
  41. ) inp
  42. WHERE NOT EXISTS (
  43.     SELECT *
  44.     FROM [Terrorism].[FactAttack] fa
  45.     WHERE
  46.         fa.date_id = inp.date_id
  47.         AND (fa.location_id = inp.location_id
  48.             OR (fa.location_id IS NULL AND inp.location_id IS NULL))
  49.         AND (fa.target_group_id = inp.target_group_id
  50.             OR (fa.target_group_id IS NULL AND inp.target_group_id IS NULL))
  51.         AND (fa.terrorist_group_id = inp.terrorist_group_id
  52.             OR (fa.terrorist_group_id IS NULL AND inp.terrorist_group_id IS NULL))
  53.         AND (fa.weapon_type_id = inp.weapon_type_id
  54.             OR (fa.weapon_type_id IS NULL AND inp.weapon_type_id IS NULL))
  55.         AND (fa.injured_count = inp.wounded
  56.             OR (fa.injured_count IS NULL AND inp.wounded IS NULL))
  57.         AND (fa.killed_count = inp.killed
  58.             OR (fa.killed_count IS NULL AND inp.killed IS NULL))
  59. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement