Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.99 KB | None | 0 0
  1. USE KN301_Ilinykh;
  2.  
  3. go
  4. DROP TABLE ProductPrices;
  5. DROP TABLE Cart;
  6. DROP TABLE Product;
  7. DROP TABLE Cities;
  8. DROP FUNCTION GetCartPrice;
  9. DROP FUNCTION GetPricesInCities;
  10. DROP FUNCTION IsEnough;
  11. DROP FUNCTION IsPositive;
  12.  
  13. go
  14. CREATE TABLE Product(
  15.     id int IDENTITY(1,1) primary key,
  16.     name nvarchar(80)
  17. )
  18.  
  19. CREATE TABLE Cities(
  20.     id int IDENTITY(1,1) primary key,
  21.     name nvarchar(80)
  22. )
  23.  
  24. CREATE TABLE ProductPrices(
  25.     id int IDENTITY(1,1) primary key,
  26.     productId int foreign key references Product(id),
  27.     cityId int foreign key references Cities(id),
  28.     avgPrice money
  29. )
  30.  
  31. CREATE TABLE Cart(
  32.     id int IDENTITY(1,1) primary key,
  33.     productId int foreign key references Product(id),
  34.     amount float
  35. )
  36.  
  37. go
  38. insert into Product values
  39.     (N'Пшеничный хлеб'),
  40.     (N'Молоко'),
  41.     (N'Картофель'),
  42.     (N'Сыр'),
  43.     (N'Мясо (говядина)'),
  44.     (N'Подсолнечное масло');
  45.  
  46. go
  47. insert into Cities values
  48.     (N'Тверь'),
  49.     (N'Липецк'),
  50.     (N'Барнаул');
  51.  
  52. go
  53. insert into ProductPrices values
  54.     (1, 1, 11),
  55.     (1, 2, 12),
  56.     (1, 3, 14),
  57.  
  58.     (2, 1, 26),
  59.     (2, 2, 23),
  60.     (2, 3, 25),
  61.  
  62.     (3, 1, 9),
  63.     (3, 2, 13),
  64.     (3, 3, 16),
  65.  
  66.     (4, 1, 240),
  67.     (4, 2, 215),
  68.     (4, 3, 260),
  69.  
  70.     (5, 1, 260),
  71.     (5, 2, 280),
  72.     (5, 3, 300),
  73.  
  74.     (6, 1, 38),
  75.     (6, 2, 44),
  76.     (6, 3, 50)
  77.  
  78. go
  79. insert into Cart values
  80.     (1, 2),
  81.     (3, 3),
  82.     (5, 1.5),
  83.     (6, 1);
  84.  
  85. go
  86. CREATE FUNCTION GetCartPrice(@city nvarchar(80)) RETURNS FLOAT
  87. BEGIN
  88.     DECLARE @cityId int;  
  89.     SELECT @cityId = Cities.id  
  90.     FROM Cities
  91.     WHERE Cities.name = @city  
  92.  
  93.     DECLARE @price int;  
  94.  
  95.     SELECT @price = SUM(ProductPrices.avgPrice * Cart.amount) FROM Cart
  96.         JOIN Product ON Cart.productId = Product.id
  97.         JOIN ProductPrices ON Cart.productId = ProductPrices.productId WHERE ProductPrices.cityId = @cityId;
  98.  
  99.     RETURN @price;  
  100. END;
  101.  
  102. go
  103. CREATE FUNCTION GetPricesInCities(@product nvarchar(80))
  104. RETURNS @rtnTable TABLE
  105. (
  106.     Name nvarchar(255),
  107.     avgPrices money,
  108.     DiffWithMaxPrice money,
  109.     DiffWithMinPrice money
  110. )
  111. BEGIN
  112.     DECLARE @maxPrice money;  
  113.     SELECT @maxPrice = Max(ProductPrices.avgPrice)  FROM ProductPrices
  114.     JOIN Cities ON Cities.id = ProductPrices.cityId
  115.     JOIN Product ON ProductPrices.productId = Product.id WHERE Product.name = @product
  116.  
  117.     DECLARE @minPrice money;  
  118.     SELECT @minPrice = Min(ProductPrices.avgPrice)  FROM ProductPrices
  119.     JOIN Cities ON Cities.id = ProductPrices.cityId
  120.     JOIN Product ON ProductPrices.productId = Product.id WHERE Product.name = @product
  121.  
  122.     insert into @rtnTable
  123.     SELECT Cities.name, ProductPrices.avgPrice, ProductPrices.avgPrice - @maxPrice, ProductPrices.avgPrice - @minPrice FROM ProductPrices
  124.     JOIN Cities ON Cities.id = ProductPrices.cityId
  125.     JOIN Product ON ProductPrices.productId = Product.id WHERE Product.name = @product
  126.     RETURN
  127. END
  128.  
  129. go
  130. CREATE FUNCTION IsPositive(@money money) RETURNS nvarchar(3)
  131. BEGIN
  132.     IF @money >= 0
  133.         return 'YES'
  134.     return 'NO'
  135. END;
  136.  
  137. go
  138. CREATE FUNCTION IsEnough(@moneyAmount money)
  139. RETURNS @rtnTable TABLE
  140. (
  141.     city nvarchar(255),
  142.     isEnough nvarchar(3),
  143.     moneyDiff money
  144. )
  145. BEGIN
  146.     insert into @rtnTable
  147.     SELECT Cities.name, dbo.IsPositive(@moneyAmount - dbo.GetCartPrice(Cities.name)), @moneyAmount - dbo.GetCartPrice(Cities.name) FROM Cities
  148.     RETURN
  149. END
  150.  
  151. go
  152.  
  153.  
  154. select dbo.GetCartPrice(N'Тверь');
  155. select * from dbo.GetPricesInCities(N'Молоко');
  156. select * from dbo.IsEnough(500);
  157. drop FUNCTION CitiesStr
  158. go
  159.  
  160. CREATE FUNCTION CitiesStr() RETURNS nvarchar(255)
  161. BEGIN
  162.     DECLARE @str nvarchar(255)
  163.     set @str = ''
  164.     select @str=@str+'['+name+'], ' FROM Cities
  165.  
  166.     return SUBSTRING(@str, 1, LEN(@str)-1)
  167. end;
  168. go
  169. print dbo.CitiesStr()
  170.  
  171. DECLARE @str2 nvarchar(355)
  172. set  @str2='select * from (select Product.name as [Наименование продукта], Cities.name, avgPrice from ProductPrices
  173.     JOIN Product ON ProductPrices.productId = Product.id
  174.     JOIN Cities ON Cities.id = ProductPrices.cityId ) p
  175. pivot (avg(avgPrice) for p.name in ('+dbo.CitiesStr()+')) as pvt'
  176.  
  177. exec(@str2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement