This commit is contained in:
Aelita4 2022-11-08 11:31:42 +01:00
commit e2a44e412e
Signed by: Aelita4
GPG Key ID: C217320B9C5FD53B
1 changed files with 85 additions and 0 deletions

85
lab3.sql Normal file
View File

@ -0,0 +1,85 @@
USE Northwind_2020;
-- 1. Podaj identyfikator najd³u¿ej realizowanego zamówienia
SELECT OrderID, datediff(DAY, OrderDate, ShippedDate) as Days
FROM Orders
ORDER BY Days DESC
SELECT OrderID, datediff(DAY, OrderDate, ShippedDate) as Days
FROM Orders
WHERE datediff(DAY, OrderDate, ShippedDate) =
(SELECT max(datediff(DAY, OrderDate, ShippedDate)) FROM Orders)
-- 2. Podaæ klientów, którzy mieli przerwê w zakupach d³u¿sz¹ ni¿ 2 miesi¹ce
SELECT OrderID, CustomerID, OrderDate, (
SELECT TOP 1 OrderDate FROM Orders
WHERE OrderID > o.OrderID AND
CustomerID = o.CustomerID
) AS NextOrderDate,
datediff(DAY, OrderDate, (
SELECT TOP 1 OrderDate FROM Orders
WHERE OrderID > o.OrderID AND
CustomerID = o.CustomerID
))
FROM Orders AS o WHERE datediff(DAY, OrderDate, (
SELECT TOP 1 OrderDate FROM Orders
WHERE OrderID > o.OrderID AND
CustomerID = o.CustomerID
)) > 62
-- 3. Wypisaæ identyfikatory tych pracowników, którzy realizowali wiêcej zamówieñ
-- ni¿ liczba zamówieñ zrealizowanych przez pracowników z tego samego kraju
-- co spedytorzy.
SELECT EmployeeID, count(OrderID) FROM Orders
GROUP BY EmployeeID
HAVING count(OrderID) >= all (
SELECT count(OrderID) FROM Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
WHERE Country = ShipCountry
GROUP BY Orders.EmployeeID
)
-- 4. W firmie u¿ywaj¹cej bazy Northwind pracownicy (tabela Employees) posiadaj¹
-- adresy emialowe. Za³ó¿my, ¿e standardowo nazwa konta (czêœæ przed znakiem @)
-- zbudowana jest z pierwszej litery imienia (firstname), inicja³u œrodkowego
-- (pole middlinitial) oraz pierwszych oœmiu znaków nazwiska (lastname).
-- Utwórz zapytanie, które wygeneruje te nazwy emailowe. W tym zadaniu nale¿y
-- u¿yæ funkcji ³añcuchowych LOWER i SUBSTRING.
SELECT FirstName, LastName, lower(
concat(
substring(FirstName, 1, 1),
substring(LastName, 1, 8),
'@northwind.com'
)
) FROM Employees
-- 5. Podaj dzieñ w którym by³o najwiêcej zamówieñ
SELECT count(OrderID) AS HowMany, datename(weekday, OrderDate) AS WeekDay
FROM Orders
GROUP BY datename(weekday, OrderDate)
ORDER BY count(OrderID) DESC
-- 6. Wypisz nazwê i adres najczêœciej zamawiaj¹cego klienta -- broken
SELECT CompanyName, City
FROM Customers
WHERE CompanyName = (
SELECT CompanyName FROM Customers
INNER JOIN Orders ON Orders.CustomerID = Customers.CustomerID
HAVING count(OrderID) >= ALL (SELECT count(OrderID) FROM Orders GROUP BY CustomerID)
)
SELECT count(OrderID), Orders.CustomerID
FROM Orders
INNER JOIN Customers ON Customers.CustomerID = Orders.CustomerID
GROUP BY Orders.CustomerID
-- 7. Wskazaæ pracownika który obs³ugiwa³ klienta, którego nie obs³ugiwa³ nikt inny
-- 8. Wypisz dane pracownika którego nazwisko jest najd³u¿sze. Skorzystaj z funkcji LEN
SELECT LastName
FROM Employees
WHERE len(LastName) = (SELECT max(len(LastName)) FROM Employees)