Advertisement
rakoczyn

SQL AdventureWorks

Jan 25th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.53 KB | None | 0 0
  1. -- ZADANIE 1
  2.  
  3. SELECT d.Name, COUNT(*)
  4. FROM HumanResources.Department AS d
  5.  JOIN HumanResources.EmployeeDepartmentHistory AS edh
  6.  ON d.DepartmentID = edh.DepartmentID
  7. WHERE edh.EndDate IS NULL
  8.  
  9. GROUP BY d.Name;
  10.  
  11.  
  12.  
  13. -- ZADANIE 2
  14.  
  15. SELECT eph.BusinessEntityID,eph.Rate
  16. FROM HumanResources.EmployeePayHistory AS eph
  17. JOIN HumanResources.EmployeeDepartmentHistory AS edh
  18. ON eph.BusinessEntityID = edh.BusinessEntityID
  19. WHERE (eph.RateChangeDate = (SELECT MAX(RateChangeDate) FROM HumanResources.EmployeePayHistory AS eph2 WHERE eph.BusinessEntityID = eph2.BusinessEntityID))
  20.         AND (edh.EndDate IS NULL)
  21.  
  22.  
  23.  
  24. -- ZADANIE 3
  25.  
  26. SELECT d.Name, AVG(Rate)
  27. FROM HumanResources.EmployeePayHistory AS eph
  28. JOIN HumanResources.EmployeeDepartmentHistory AS edh
  29.     ON eph.BusinessEntityID = edh.BusinessEntityID
  30. JOIN HumanResources.Department AS d
  31.     ON d.DepartmentID = edh.DepartmentID
  32. GROUP BY d.Name;
  33.  
  34.  
  35.  
  36. -- ZADANIE 4
  37.  
  38. SELECT eph.BusinessEntityID,eph.Rate
  39. FROM HumanResources.EmployeePayHistory AS eph
  40. JOIN HumanResources.EmployeeDepartmentHistory AS edh
  41. ON eph.BusinessEntityID = edh.BusinessEntityID
  42. WHERE (eph.RateChangeDate = (SELECT MAX(RateChangeDate) FROM HumanResources.EmployeePayHistory AS eph2 WHERE eph.BusinessEntityID = eph2.BusinessEntityID))
  43.         AND (edh.EndDate IS NULL) AND (eph.Rate > (SELECT AVG(Rate) FROM HumanResources.EmployeePayHistory))
  44. ORDER BY eph.Rate DESC
  45.  
  46.  
  47. -- ZADANIE 5
  48.  
  49. CREATE VIEW HumanResources.AverageRates (DepartmentID, AverageRate)
  50. AS
  51. SELECT DepartmentID, AVG(Rate)
  52. FROM HumanResources.EmployeeDepartmentHistory AS edh
  53. JOIN HumanResources.EmployeePayHistory AS eph
  54.     ON eph.BusinessEntityID = edh.BusinessEntityID
  55. GROUP BY DepartmentID
  56.  
  57. SELECT eph.BusinessEntityID,edh.DepartmentID,Rate
  58. FROM HumanResources.EmployeePayHistory AS eph
  59. JOIN HumanResources.EmployeeDepartmentHistory AS edh
  60.     ON eph.BusinessEntityID = edh.BusinessEntityID
  61. JOIN HumanResources.AverageRates ON AverageRates.DepartmentID = edh.DepartmentID
  62. WHERE (eph.RateChangeDate = (SELECT MAX(RateChangeDate) FROM HumanResources.EmployeePayHistory AS eph2 WHERE eph.BusinessEntityID = eph2.BusinessEntityID))
  63.         AND (edh.EndDate IS NULL) AND (eph.Rate > HumanResources.AverageRates.AverageRate)
  64.        
  65. ORDER BY eph.BusinessEntityID
  66.  
  67.  
  68.  
  69. -- ZADANIE 6
  70. SELECT eph.BusinessEntityID, Diff = (SELECT MAX(Rate) FROM HumanResources.EmployeePayHistory) - eph.Rate
  71. FROM HumanResources.EmployeePayHistory AS eph
  72. WHERE (eph.RateChangeDate = (SELECT MAX(RateChangeDate) FROM HumanResources.EmployeePayHistory AS eph2 WHERE eph.BusinessEntityID = eph2.BusinessEntityID ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement