Запрос, чтобы получить отличные результаты - PullRequest
0 голосов
/ 25 марта 2020

У меня есть запрос для получения данных из нескольких таблиц. Но каким-то образом я получил повторяющиеся записи из базы данных из-за одного столбца.

Запрос:

SELECT
  BH.BusinessName, AppointmentStartTime, AppointmentEndTime, AppointmentStatus,
  AppointmentFor, AppointmentForID, AppointmentStatus, S.ServiceName
from Appointment A
INNER JOIN BusinessHost BH ON A.BusinessHostID = BH.BusinessHostID
INNER JOIN BusinessHostService BHS ON BHS.BusinessHostID=BH.BusinessHostID
INNER JOIN Services S ON S.ServiceID=BHS.ServiceID

и вывод выглядит так:

+ -------------+----------------------+--------------------+-------------------+----------------+------------------+-------------------+---------------------+
| BusinessName | AppointmentStartTime | AppointmentEndTime | AppointmentStatus | AppointmentFor | AppointmentForID | AppointmentStatus | ServiceName         |
+ -------------+----------------------+--------------------+-------------------+----------------+------------------+-------------------+---------------------+
| Amit & Sons  | 02:00:00.0000000     | 02:15:00.0000000   | Added             | Motor-Cycle    | 1006             | Added             | Arboriculture       |
| Amit & Sons  | 02:00:00.0000000     | 02:15:00.0000000   | Added             | Motor-Cycle    | 1006             | Added             | Landscaping Service |
| Rohit & Sons | 02:30:00.0000000     | 02:45:00.0000000   | Added             | Motor-Cycle    | 1006             | Added             | Arboriculture       |
| Rohit & Sons | 02:30:00.0000000     | 02:45:00.0000000   | Added             | Motor-Cycle    | 1006             | Added             | Landscaping Service |
+ -------------+----------------------+--------------------+-------------------+----------------+------------------+-------------------+---------------------+

можете ли вы помогите мне с написанием запроса, чтобы получить желаемые результаты, такие как:

+ -------------+----------------------+--------------------+-------------------+----------------+------------------+-------------------+------------------------------------+
| BusinessName | AppointmentStartTime | AppointmentEndTime | AppointmentStatus | AppointmentFor | AppointmentForID | AppointmentStatus | ServiceNames                       |
+ -------------+----------------------+--------------------+-------------------+----------------+------------------+-------------------+------------------------------------+
| Amit & Sons  | 02:30:00.0000000     | 02:45:00.0000000   | Added             | Motor-Cycle    | 1006             | Added             | Arboriculture, Landscaping Service |
| Rohit & Sons | 02:30:00.0000000     | 02:45:00.0000000   | Added             | Motor-Cycle    | 1006             | Added             | Arboriculture, Landscaping Service |
+ -------------+----------------------+--------------------+-------------------+----------------+------------------+-------------------+------------------------------------+

1 Ответ

2 голосов
/ 25 марта 2020

Вы ищете агрегирование строк . Начиная с SQL Server 2017 это доступно с STRING_AGG (https://docs.microsoft.com/de-de/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-ver15):

SELECT
  bh.businessname, a.appointmentstarttime, a.appointmentendtime, a.appointmentstatus,
  a.appointmentfor, a.appointmentforid, a.appointmentstatus, bs.servicenames
FROM appointment a
JOIN businesshost bh ON bh.businesshostid = a.businesshostid
JOIN
(
  SELECT
    bhs.businesshostid,
    STRING_AGG(s.servicename, ', ') WITHIN GROUP (ORDER BY s.servicename) AS servicenames
  FROM businesshostservice bhs
  JOIN services s ON s.serviceid = bhs.serviceid
  GROUP BY bhs.businesshostid
) bs ON bs.businesshostid = bh.businesshostid
ORDER BY bh.businessname, a.appointmentstarttime;
...