-
Notifications
You must be signed in to change notification settings - Fork 0
лабораторная работа 3 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
plidan123
wants to merge
1
commit into
main
Choose a base branch
from
lab_3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| --Лабораторная работа 3 | ||
| --Задание 1. SELECT к нескольким таблицам | ||
| --Фильтрация должна производится на исходных данных столбцов (не на вычисляемых выражениях)! | ||
| -- | ||
| --1. Выведите информацию о тех транспортных компаниях, которые доставляли заказы в декабре 2006 года в Швецию, а в декабре 2007 года в Данию | ||
| -- | ||
| select ord.shipperid, cust.companyname, cust.phone, ord.orderdate, ord.shipcountry | ||
| from "Sales"."Shippers" as cust join "Sales"."Orders" as ord on cust.shipperid=ord.shipperid | ||
| where (ord.shippeddate between '2006-12-01' and '2006-12-31' and ord.shipcountry='Sweden') | ||
| or (ord.shippeddate between '2007-12-01' and '2007-12-31' and ord.shipcountry='Denmark'); | ||
| -- | ||
| --2. Сформируйте выборку следующего вида: | ||
| -- | ||
| --В столбцах Название продукта и Поставщик необходимо исключить слова Product и Supplier, соответственно. | ||
| --В выборке должны присутствовать только те продукты, которые относятся к категориям с 1 по 5 и при этом их поставщики находятся в Европе. | ||
| -- | ||
| select btrim(split_part(prod.productname, ' ', 2)), prod.unitprice, cat.categoryname, btrim(split_part(sup.companyname, ' ', 2)), sup.phone, sup.country | ||
| from "Production"."Suppliers" as sup join "Production"."Products" as prod on | ||
| sup.supplierid=prod.supplierid join "Production"."Categories" as cat on prod.categoryid=cat.categoryid | ||
| where sup.country in ('Spain', 'Russia', 'UK', 'Sweden', 'Germany', 'Italy', 'Norway', 'France', 'Denmark', | ||
| 'Netherlands', 'Finland') and prod.categoryid between 1 and 5 | ||
| -- | ||
| --3. Сформируйте выборку следующего вида: | ||
| --В столбцах Название продукта и Заказчик необходимо исключить слова Product и Customer, соответственно | ||
| --В столбце Стоимость с учетом скидки необходимо рассчитать сумму, которую должен заплатить за данный товар клиент с учетом количества товара и предоставленной скидки | ||
| --Выборка должна содержать информацию о Заказчиках из Бразилии и Канады, которые сделали заказы весной 2007 года, при этом адрес доставки должен совпадать с адресом Заказчика (под адресом подразумевается страна и город). | ||
| -- | ||
| select btrim(split_part(cust.companyname, ' ', 2)), concat(cust.country, cust.city), concat(ord.shipcountry, ord.shipcity),btrim(split_part(prod.productname, ' ', 2)), (orddet.unitprice * orddet.qty * (1-orddet.discount)) as cost | ||
| from "Sales"."Orders" as ord join "Sales"."OrderDetails" as orddet on orddet.orderid = ord.orderid | ||
| join "Production"."Products" as prod on orddet.productid=prod.productid | ||
| join "Sales"."Customers" as cust on cust.custid=ord.custid | ||
| where cust.country in ('Brazil','Canada') and ord.orderdate between '2007-03-01' and '2007-05-31' | ||
| and cust.country=ord.shipcountry and cust.city=ord.shipcity | ||
| --4. Сформируйте выборку следующего вида: | ||
| --Выборка должна содержать набор уникальных записей с информацией о сотрудниках магазина и их клиентах при условии, что: | ||
| --a) Сотрудник является мужчиной | ||
| select distinct concat(e.firstname,e.lastname), e.title, c.companyname, c.contactname, c.contacttitle | ||
| from "HR"."Employees" as e | ||
| join "Sales"."Orders" as o on e.empid=o.empid | ||
| join "Sales"."Customers" as c on o.custid=c.custid | ||
| where e.titleofcourtesy ilike 'mr%' and not e.titleofcourtesy ilike 'mrs%' | ||
| --b) Сотрудник проживает в том же городе где находится клиент (Учтите тот факт, что | ||
| --в разных странах могут быть города с одинаковыми названиями) | ||
| select distinct concat(e.firstname,e.lastname), e.title, c.companyname, c.contactname, c.contacttitle | ||
| from "HR"."Employees" as e | ||
| join "Sales"."Orders" as o on e.empid=o.empid | ||
| join "Sales"."Customers" as c on o.custid=c.custid | ||
| where c.country=e.country and e.city=c.city | ||
| --c) Должность сотрудника и должность контактного лица клиента совпадают | ||
| -- | ||
| select distinct concat(e.firstname,e.lastname), e.title, c.companyname, c.contactname, c.contacttitle | ||
| from "HR"."Employees" as e | ||
| join "Sales"."Orders" as o on e.empid=o.empid | ||
| join "Sales"."Customers" as c on o.custid=c.custid | ||
| where c.contacttitle=e.title | ||
| --5. Выведите полную информацию о сотрудниках, которые не оформили ни одного заказа | ||
| -- | ||
| select * | ||
| from "HR"."Employees" as e | ||
| left join "Sales"."Orders" as o on e.empid=o.empid | ||
| where o.empid is null | ||
| --6. Напишите запрос, возвращающий список заказов, которые не являются международными | ||
| -- (страна доставки заказа и страна клиента совпадают) | ||
| select * | ||
| from "Sales"."Orders" as o | ||
| join "Sales"."Customers" as c on c.custid=o.custid | ||
| where c.country=o.shipcountry | ||
| -- | ||
| --7. Выведите информацию о тех товарах, которые никогда не продавались (не вошли ни в один заказ) | ||
| -- | ||
| select p.productname, cat.categoryname, s.companyname, p.unitprice,p.discontinued | ||
| from "Production"."Products" as p | ||
| left join "Sales"."OrderDetails" as od on p.productid=od.productid | ||
| left join "Sales"."Orders" as o on o.orderid=od.orderid | ||
| left join "Production"."Categories" as cat on p.categoryid=cat.categoryid | ||
| left join "Production"."Suppliers" as s on p.supplierid=s.supplierid | ||
| where od.orderid is null | ||
| --8. Выведите уникальный список сотрудников, у которых есть в подчинении другие сотрудники | ||
| -- | ||
| select distinct e.empid, e.lastname, e.firstname, e.title | ||
| from "HR"."Employees" as e | ||
| left join "HR"."Employees" as emp on e.empid=emp.mgrid | ||
| where emp.empid is not null | ||
| -- | ||
| --9. Сформируйте выборки следующего вида: | ||
| -- | ||
| --a. Выборка должна содержать уникальный список товаров, которые находятся в одной категории и при этом имеют одинаковую цену | ||
| select distinct p.productid, p.productname, p.supplierid, p.categoryid, p.unitprice,p.discontinued | ||
| from "Production"."Products" as p | ||
| join "Production"."Products" as pr on pr.categoryid = p.categoryid and p.unitprice=pr.unitprice and p.productid!=pr.productid | ||
| --b. Выборка должна содержать уникальный список товаров, которые поставляются одним и тем же | ||
| --поставщиком и при этом имеют одинаковую цену | ||
| select distinct p.productid, p.productname, p.supplierid, p.categoryid, p.unitprice,p.discontinued | ||
| from "Production"."Products" as p | ||
| join "Production"."Products" as pr on p.unitprice=pr.unitprice and p.supplierid = pr.supplierid and p.productid!=pr.productid | ||
| --c. Выборка должна содержать уникальный список товаров, которые находятся в одной категории, | ||
| -- поставляются одним и тем же поставщиком и при этом имеют одинаковую цену | ||
| select distinct p.productid, p.productname, p.supplierid, p.categoryid, p.unitprice,p.discontinued | ||
| from "Production"."Products" as p | ||
| join "Production"."Products" as pr on pr.categoryid = p.categoryid and p.unitprice=pr.unitprice and p.supplierid = pr.supplierid and p.productid!=pr.productid | ||
| --d. Выборка должна содержать уникальный список товаров, которые находятся в одной категории, | ||
| --поставляются одним и тем же поставщиком и при этом имеют разную цену | ||
| -- | ||
| select distinct p.productid, p.productname, p.supplierid, p.categoryid, p.unitprice,p.discontinued | ||
| from "Production"."Products" as p | ||
| join "Production"."Products" as pr on pr.categoryid = p.categoryid and p.unitprice!=pr.unitprice and p.supplierid = pr.supplierid and p.productid!=pr.productid | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно было воспользоваться JOIN, тогда фильтрация null значений не нужна.