QUESTION: Choose the name of clients, concluded more than one loan agreement in 2010, as the numbers and dates of issue of these contracts. Sort by name of the client and date of issue of the loan.
My question: Is it the correct way of solving this task? Sqlfiddle: http://www.sqlfiddle.com/#!18/c140c/1
DDL:
create table client(
cl_id int primary key identity (1,1),
cl_full_name nvarchar(100) not null,
);
create table deal(
dl_id int primary key identity (1,1),
dl_client_id int not null,
dl_code nvarchar(100) not null,
dl_valutation_date datetime not null,
foreign key(dl_client_id) references client(cl_id)
);
QUERY:
SELECT c.cl_full_name AS "ФИО Клиента",
d2.dl_code AS "Номер договора",
Cast(d2.dl_valutation_date AS DATE) AS "Дата выдачи"
FROM (
SELECT d.dl_client_id
FROM deal AS d
WHERE Year(d.dl_valutation_date) = 2010
GROUP BY d.dl_client_id
HAVING Count(*) > 1
) AS t1
INNER JOIN deal AS d2
ON d2.dl_client_id = t1.dl_client_id
INNER JOIN client AS c
ON c.cl_id = d2.dl_client_id
ORDER BY c.cl_full_name,
d2.dl_valutation_date