Wacky_rubia

Untitled

Dec 2nd, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. --GO
  2.  
  3. --SET CHARACTER SET 'utf8';
  4. -- SET SESSION collation_connection = 'utf8_general_ci';
  5.  
  6. if OBJECT_ID('prices', 'U') is not null drop table prices
  7. if OBJECT_ID('products_in_bag', 'U') is not null drop table products_in_bag
  8. if OBJECT_ID('cities', 'U') is not null drop table cities
  9. if OBJECT_ID('products', 'U') is not null drop table products
  10.  
  11.  
  12. create table products
  13. (
  14. name nvarchar(30) NOT NULL,
  15. id int NOT NULL primary key IDENTITY(1,1)
  16. )
  17. GO
  18.  
  19. create table cities
  20. (
  21. name nvarchar(30) NOT NULL,
  22. id int NOT NULL primary key IDENTITY(1,1)
  23. )
  24. GO
  25.  
  26. create table prices
  27. (
  28. id int NOT NULL primary key IDENTITY(1,1),
  29. city int NOT NULL foreign key references cities(id),
  30. product int NOT NULL foreign key references products(id),
  31. price int NOT NULL
  32. )
  33. GO
  34.  
  35. create table products_in_bag
  36. (
  37. id int NOT NULL primary key IDENTITY(1,1),
  38. product int NOT NULL foreign key references products(id),
  39. quantity float NOT NULL,
  40. )
  41. GO
  42.  
  43. insert into cities (name)
  44. values (N'Тверь'), (N'Липецк'), (N'Барнаул')
  45.  
  46. insert into products (name)
  47. values (N'Пшеничный хлеб (батон)'),
  48. (N'Молоко (1 литр)'),
  49. (N'Картофель (1 кг)'),
  50. (N'Сыр (1 кг)'),
  51. (N'Мясо (говядина) (1 кг)'),
  52. (N'Подсолнечное масло (1 литр)')
  53.  
  54. insert into prices (city, product, price)
  55. values
  56. (1, 1, 11),
  57. (1, 2, 26),
  58. (1, 3, 9),
  59. (1, 4, 240),
  60. (1, 5, 260),
  61. (1, 6, 38),
  62.  
  63. (2, 1, 12),
  64. (2, 2, 23),
  65. (2, 3, 13),
  66. (2, 4, 215),
  67. (2, 5, 280),
  68. (2, 6, 44),
  69.  
  70. (3, 1, 14),
  71. (3, 2, 25),
  72. (3, 3, 16),
  73. (3, 4, 260),
  74. (3, 5, 300),
  75. (3, 6, 50)
  76.  
  77. insert into products_in_bag (product, quantity)
  78. values (1, 2), (3, 3), (5, 1.5), (6, 1)
  79.  
  80.  
  81. IF OBJECT_ID (N'GetPrice', N'FN') IS NOT NULL
  82. DROP FUNCTION GetPrice;
  83. GO
  84.  
  85. create function GetPrice(@City nvarchar(30), @ProductID int, @Quantity float)
  86. returns float
  87. as
  88. begin
  89. declare @ans float;
  90. declare @cityId int;
  91. set @cityId = (select id from cities where name = @City)
  92. select @ans = price
  93. from prices
  94. where city = @CityID AND product = @ProductID;
  95. if (@ans IS NULL)
  96. set @ans = 0;
  97. return @ans * @Quantity;
  98. end;
  99. go
  100.  
  101.  
  102. select sum(dbo.GetPrice(N'Тверь', product, quantity)) as Сумма
  103. from products_in_bag
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. select product_name as 'Наименование продукта', [Тверь], [Липецк], [Барнаул]
  111. from (select prdcts.name as product_name, cts.name, prcs.price
  112. from prices prcs
  113. join
  114. cities cts
  115. on cts.id = prcs.city
  116. join
  117. products prdcts
  118. on prdcts.id = prcs.product
  119. ) r
  120. pivot
  121. (
  122. max(price) for name in ([Тверь], [Липецк], [Барнаул])
  123. ) as pvt
  124.  
  125.  
  126.  
  127.  
  128.  
  129. --select
  130. -- (select name from products where id = product) as Продукт,
  131. -- quantity as Количество
  132. --from products_in_bag
Advertisement
Add Comment
Please, Sign In to add comment