Advertisement
Azz_142

Mohito

Jun 24th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.32 KB | None | 0 0
  1. create database Mohito
  2.  
  3. use Mohito
  4.  
  5. create table Proizvodi (
  6. ProizvodiID int not null identity(1,1) constraint PK_Proizvodi primary key,
  7. Naziv nvarchar(50) not null,
  8. Kategorija nvarchar(50) not null,
  9. Cijena decimal(18,2) not null
  10. )
  11.  
  12. create table Narudzbe (
  13. NarudzbaID int not null identity(1,1) constraint PK_Narudzbe primary key,
  14. DatumNarudzbe date not null,
  15. DatumIsporuke date not null,
  16. Drzava nvarchar(50) not null,
  17. Ukupno decimal(18,2) not null
  18. )
  19.  
  20. create table StavkeNarudzbe (
  21. NarudzbaID int not null constraint fk_StavkeNarudzbe_Narudzbe foreign key references Narudzbe(NarudzbaID),
  22. ProizvodID int not null constraint fk_StavkeNarudzbe_Proizvodi foreign key references Proizvodi(ProizvodiID),
  23. primary key (NarudzbaID, ProizvodID),
  24. Kolicina int not null,
  25. Cijena decimal(18,2) not null,
  26. Iznos decimal(18,2) not null
  27. )
  28.  
  29. --2
  30. use NORTHWND
  31. go
  32.  
  33. set identity_insert Mohito.dbo.Proizvodi on
  34. insert into Mohito.dbo.Proizvodi (ProizvodiID, Naziv, Kategorija, Cijena)
  35. select distinct P.ProductID, P.ProductName, C.CategoryName, P.UnitPrice
  36. from Products as P inner join Categories as C
  37.     on P.CategoryID = C.CategoryID inner join [Order Details] as OD
  38.     ON OD.ProductID=P.ProductID INNER JOIN Orders AS O
  39.     ON O.OrderID=OD.OrderID
  40. where YEAR(O.OrderDate) = 1996
  41. set identity_insert Mohito.dbo.Proizvodi off
  42.  
  43. set identity_insert Mohito.dbo.Narudzbe on
  44. insert into Mohito.dbo.Narudzbe (NarudzbaID, DatumNarudzbe, DatumIsporuke, Drzava, Ukupno)
  45. select distinct O.OrderID, O.OrderDate, O.ShippedDate, O.ShipCountry,
  46.         sum(OD.Quantity*OD.UnitPrice*OD.Discount)
  47. from [Order Details] as OD inner join Orders as O
  48. on O.OrderID=OD.OrderID
  49. WHERE  YEAR(O.OrderDate) = 1996
  50. group by O.OrderID, O.OrderDate, O.ShippedDate, O.ShipCountry
  51. set identity_insert Mohito.dbo.Narudzbe off
  52.  
  53.  
  54. insert into Mohito.dbo.StavkeNarudzbe(NarudzbaID, ProizvodID, Kolicina, Cijena, Iznos)
  55. select O.OrderID, P.ProductID, OD.Quantity, OD.UnitPrice, OD.Quantity*OD.UnitPrice
  56. from [Order Details] as OD inner join Orders as O
  57. on O.OrderID=OD.OrderID INNER JOIN Products as P on P.ProductID = OD.ProductID
  58. WHERE  YEAR(O.OrderDate) = 1996
  59.  
  60. --3
  61.  
  62. use Mohito
  63.  
  64.  
  65. CREATE TABLE Skladista (
  66. SkladisteID int not null identity(1,1) constraint PK_Skladista primary key,
  67. Naziv nvarchar(20)
  68. )
  69.  
  70. create table SkladistaProizvodi (
  71. ProizvodID int not null constraint fk_SkladistaProizvodi_Proizvodi foreign key references Proizvodi(ProizvodiID),
  72. SkladisteID int not null constraint fk_SkladistaProizvodi_Skladista foreign key references Skladista(SkladisteID),
  73. PRIMARY KEY (ProizvodID, SkladisteID),
  74. Kolicina int not null
  75. )
  76.  
  77. --4
  78.  
  79. insert into Skladista
  80. VALUES ('Mostar'),
  81. ('Sarajevo'),
  82. ('Banja Luka')
  83.  
  84. insert into SkladistaProizvodi
  85. select P.ProizvodiID, (select SkladisteID from Skladista where Naziv='Mostar'), 0
  86. from Proizvodi as P
  87.  
  88. insert into SkladistaProizvodi
  89. select P.ProizvodiID, (select SkladisteID from Skladista where Naziv='Sarajevo'), 0
  90. from Proizvodi as P
  91.  
  92. insert into SkladistaProizvodi
  93. select P.ProizvodiID, (select SkladisteID from Skladista where Naziv='Banja Luka'), 0
  94. from Proizvodi as P
  95.  
  96.  
  97. select* from SkladistaProizvodi
  98.  
  99. --5
  100.  
  101. update Proizvodi
  102. set Cijena=Cijena+5
  103. where Kategorija like 'Confections'
  104.  
  105. select*from Proizvodi
  106. where Kategorija like 'Confections'
  107.  
  108.  
  109. --6
  110.  
  111. select sum(N.Ukupno) as Zarada, month(N.DatumIsporuke) as Mjesec
  112. from Narudzbe as N
  113. group by MONTH(N.DatumIsporuke)
  114.  
  115. --7
  116. select TOP 5 P.Naziv, sum(SN.Kolicina) as Prodano
  117. from Proizvodi as P inner join StavkeNarudzbe as SN
  118.     on P.ProizvodiID=SN.ProizvodID INNER JOIN Narudzbe AS N
  119.     ON N.NarudzbaID=SN.NarudzbaID
  120. GROUP BY P.Naziv
  121. order by Prodano desc
  122.  
  123. --8
  124. select P.Naziv, sum(SN.Kolicina), SUM(SN.Iznos) as Zarada
  125. from Proizvodi as P inner join StavkeNarudzbe as SN
  126.     on P.ProizvodiID=SN.ProizvodID INNER JOIN Narudzbe AS N
  127.     ON N.NarudzbaID=SN.NarudzbaID
  128. where month(N.DatumIsporuke)=7 or month(N.DatumIsporuke)=8
  129. GROUP BY  P.Naziv
  130. order by Zarada desc
  131.  
  132. --9
  133. UPDATE SkladistaProizvodi
  134. SET Kolicina = 100
  135. from Proizvodi as P inner join SkladistaProizvodi as SP
  136. on SP.ProizvodID=P.ProizvodiID
  137. WHERE P.Kategorija LIKE 'Confections'
  138.  
  139. --10
  140.  
  141. delete StavkeNarudzbe
  142. from StavkeNarudzbe as SN inner join Narudzbe as N
  143. ON SN.NarudzbaID=N.NarudzbaID
  144. WHERE N.Drzava LIKE 'Brazil'
  145.  
  146. delete
  147. from Narudzbe
  148. where Drzava like 'Brazil'
  149.  
  150.  
  151. select*from StavkeNarudzbe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement