查询以获得不同的结果

时间:2020-03-25 05:02:50

标签: sql sql-server string-aggregation

我有一个查询要从多个表中获取数据。但是不知何故,由于一列,我从数据库中得到了重复的条目。

查询是:

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 个答案:

答案 0 :(得分:2)

您正在寻找字符串聚合。从SQL Server 2017开始,STRING_AGGhttps://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;