Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. use Northwind
  2.  
  3. Select A.CompanyName, Year(D.OrderDate), MONTH(D.OrderDate),Sum(C.UnitPrice * C.Quantity * (1-C.Discount)) as 'Przychod'
  4. From Suppliers as A
  5. INNER JOIN Products as B
  6. ON A.SupplierID = B.SupplierID
  7. INNER JOIN [Order Details] as C
  8. ON B.ProductID = C.ProductID
  9. INNER JOIN ORDERS as D
  10. ON D.OrderID = C.OrderID
  11. GROUP BY A.CompanyName, Year(D.OrderDate) ,MONTH(D.OrderDate)
  12. HAVING Sum(C.UnitPrice * C.Quantity * (1-C.Discount)) > (
  13. SELECT SUM(E.UnitPrice * E.Quantity * (1-E.Discount)) / (Select Count(A1.CompanyName)
  14. From Suppliers as A1
  15. INNER JOIN Products as B1
  16. ON A1.SupplierID = B1.SupplierID
  17. INNER JOIN [Order Details] as C1
  18. ON B1.ProductID = C1.ProductID
  19. INNER JOIN ORDERS as D1
  20. ON D1.OrderID = C1.OrderID
  21. WHERE
  22. Year(D.OrderDate) = YEAR(D1.OrderDate)
  23. AND MONTH(D.OrderDate) = MONTH(D1.OrderDate)
  24. )
  25. FROM [Order Details] as E
  26. INNER JOIN Orders as F
  27. ON F.OrderID = E.OrderID
  28. WHERE
  29. MONTH(F.OrderDate) = MONTH(D.OrderDate)
  30. AND YEAR(F.OrderDate) = YEAR(D.OrderDate)
  31. )
  32.  
  33. USE library
  34.  
  35. SELECT A.firstname, A.lastname, B.street +' '+B.city + ' '+B.state,
  36. 'Adult', NULL as 'Guardian', SUM(ISNULL(D1.fine_paid,0))
  37. FROM MEMBER as A
  38. INNER JOIN adult as B
  39. ON A.member_no = B.member_no
  40. INNER JOIN LOAN as B1
  41. ON A.member_no = B1.member_no
  42. INNER JOIN copy as C1
  43. ON B1.isbn = C1.isbn and B1.copy_no = C1.copy_no
  44. INNER JOIN loanhist as D1
  45. ON D1.copy_no = C1.copy_no and D1.isbn = C1.isbn
  46. Group By A.firstname, A.lastname, B.street +' '+B.city + ' '+B.state
  47. UNION
  48. SELECT A.firstname, A.lastname, C.street +' '+C.city + ' '+C.state,
  49. 'Child', D.firstname + '' + D.lastname as 'Guardian', SUM(ISNULL(D1.fine_paid,0))
  50. FROM MEMBER as A
  51. INNER JOIN juvenile as B
  52. ON A.member_no = B.member_no
  53. INNER JOIN adult as C
  54. ON B.adult_member_no = C.member_no
  55. INNER JOIN member as D
  56. on D.member_no = C.member_no
  57. INNER JOIN LOAN as B1
  58. ON A.member_no = B1.member_no
  59. INNER JOIN copy as C1
  60. ON B1.isbn = C1.isbn and B1.copy_no = C1.copy_no
  61. INNER JOIN loanhist as D1
  62. ON D1.copy_no = C1.copy_no and D1.isbn = C1.isbn
  63. Group By A.firstname, A.lastname, C.street +' '+C.city + ' '+C.state,
  64. D.firstname + '' + D.lastname
  65.  
  66. USE Northwind
  67.  
  68.  
  69. SELECT *
  70. FROM Customers as A
  71. INNER JOIN Orders as B
  72. ON A.CustomerID = B.CustomerID
  73. INNER JOIN Employees as C
  74. ON C.EmployeeID = B.EmployeeID
  75. LEFT JOIN Employees as D
  76. ON C.EmployeeID = D.ReportsTo
  77. WHERE YEAR(OrderDate) = 1997
  78. AND MONTH(B.OrderDate) = 5
  79. AND Day(B.OrderDate) = 23
  80. AND D.EmployeeID is NULL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement