Advertisement
PavloSerg

LabaSqlGraph

Oct 12th, 2023
1,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.56 KB | None | 0 0
  1. --Создание схем как на паре
  2. create schema [Nodes]
  3. go
  4. create schema [Edges]
  5. go
  6.  
  7.  
  8. --Создание графовых таблиц с человеческими названиями
  9. create table Nodes.SprayPaints
  10. (
  11.     Id    int primary key,
  12.     Name  nvarchar(35) not null,
  13.     Color char(1)      not null
  14. ) as Node
  15.  
  16. create table Nodes.Squares
  17. (
  18.     Id   int primary key,
  19.     Name nvarchar(35) not null
  20. ) as Node
  21.  
  22. create table Edges.Paints
  23. (
  24.     Datetime datetime not null,
  25.     Volume   tinyint  not null
  26. ) as Edge
  27.  
  28.  
  29. --Заполение таблиц
  30. insert into Nodes.SprayPaints(Id, Name, Color)
  31. select *
  32. from dbo.utV
  33.  
  34. insert into Nodes.Squares(Id, Name)
  35. select *
  36. from dbo.utQ
  37.  
  38.  
  39. insert into Edges.Paints($from_id, $to_id, Volume, Datetime)
  40. select SprayPaints.$node_id as from_id,
  41.     Squares.$node_id as to_id,
  42.     utB.B_VOL as Volume,
  43.     utB.B_DATETIME as Datetime
  44. from Nodes.SprayPaints
  45.     join dbo.utB on SprayPaints.Id = utB.B_V_ID
  46.     join Nodes.Squares on utB.B_Q_ID = Squares.Id
  47.  
  48.  
  49. --Запросы
  50. --1) Найти квадраты, которые окрашивались красной краской.
  51. --Вывести идентификатор квадрата и объем красной краски.
  52. select Squares.Id,
  53.        sum(Paints.Volume) as RedVolume
  54. from Nodes.SprayPaints,
  55.      Edges.Paints,
  56.      Nodes.Squares
  57. where match(SprayPaints - (Paints) -> Squares)
  58.   and SprayPaints.Color = 'R'
  59. group by Squares.Id
  60.  
  61.  
  62. --2) Найти квадраты, которые окрашивались как красной, так и синей краской.
  63. --Вывести: название квадрата.
  64. select Squares.Name
  65. from Nodes.SprayPaints,
  66.      Edges.Paints,
  67.      Nodes.Squares
  68. where match(SprayPaints - (Paints) -> Squares) and SprayPaints.Color in ('R', 'B')
  69. group by Squares.Id, Squares.Name
  70. having count(distinct SprayPaints.Color) = 2
  71.  
  72. --3) Найти квадраты, которые окрашивались всеми тремя цветами.
  73. select Squares.Id
  74. from Nodes.SprayPaints,
  75.      Edges.Paints,
  76.      Nodes.Squares
  77. where match(SprayPaints - (Paints) -> Squares)
  78. group by Squares.Id
  79. having count(distinct SprayPaints.Color) = 3
  80.  
  81. --4) Найти баллончики, которыми окрашивали более одного квадрата.
  82. select SprayPaints.Id
  83. from Nodes.SprayPaints,
  84.      Edges.Paints,
  85.      Nodes.Squares
  86. where match(SprayPaints - (Paints) -> Squares)
  87. group by SprayPaints.Id
  88. having count(distinct Squares.Id) > 1
  89.  
  90. --Свои запросы
  91. --1) Найти баллончик(и) которым(ими) окрашивали наибольшое количество квадратов и вывести это количество
  92. select top 1 with ties SprayPaints.Id, count(distinct Squares.Id) SquaresCount
  93. from Nodes.SprayPaints,
  94.      Edges.Paints,
  95.      Nodes.Squares
  96. where match(SprayPaints - (Paints) -> Squares)
  97. group by SprayPaints.Id
  98. order by SquaresCount desc
  99.  
  100.  
  101. --2) Найти черные(не покрашенные) квадраты
  102. select Squares.Id
  103. from Nodes.Squares
  104. except
  105. select Squares.Id
  106. from Nodes.SprayPaints,
  107.      Edges.Paints,
  108.      Nodes.Squares
  109. where match(SprayPaints - (Paints) -> Squares)
  110. group by Squares.Id
  111.  
  112. --3) Найти белые квадраты
  113. with RedVolumes as (
  114.         select Squares.Id,
  115.                sum(Paints.Volume) as RedVolume
  116.         from Nodes.SprayPaints,
  117.              Edges.Paints,
  118.              Nodes.Squares
  119.         where match(SprayPaints - (Paints) -> Squares)
  120.           and SprayPaints.Color = 'R'
  121.         group by Squares.Id
  122.     ),
  123.     BlueVolumes as (
  124.         select Squares.Id,
  125.                    sum(Paints.Volume) as BlueVolume
  126.             from Nodes.SprayPaints,
  127.                  Edges.Paints,
  128.                  Nodes.Squares
  129.             where match(SprayPaints - (Paints) -> Squares)
  130.               and SprayPaints.Color = 'B'
  131.             group by Squares.Id
  132.     ),
  133.     GreenVolumes as (
  134.         select Squares.Id,
  135.                    sum(Paints.Volume) as GreenVolume
  136.             from Nodes.SprayPaints,
  137.                  Edges.Paints,
  138.                  Nodes.Squares
  139.             where match(SprayPaints - (Paints) -> Squares)
  140.               and SprayPaints.Color = 'G'
  141.             group by Squares.Id
  142.     )
  143. select RedVolumes.Id,
  144.        BlueVolume,
  145.        RedVolume,
  146.        GreenVolume
  147. from RedVolumes
  148. join BlueVolumes on BlueVolumes.Id = RedVolumes.Id
  149. join GreenVolumes on GreenVolumes.Id = RedVolumes.Id
  150. where BlueVolume = 255 and RedVolume = 255 and GreenVolume = 255
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement