查询以获得不同的结果
Query to get distinct results
我有一个查询要从多个表中获取数据。但是不知何故,由于一列,我从数据库中得到了重复的条目。
查询是:
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
输出是这样的:
+ --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ------+
|企业名称 |约会开始时间 |约会结束时间 |预约状态 |预约|预约ID |预约状态 |服务名称 |
+ --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ------+
|阿米特父子 | 02:00:00.0000000 | 02:15:00.0000000 |已添加 |摩托车 | 1006 |已添加 |树木栽培 |
|阿米特父子 | 02:00:00.0000000 | 02:15:00.0000000 |已添加 |摩托车 | 1006 |已添加 |园林绿化服务 |
|罗希特父子 | 02:30:00.0000000 | 02:45:00.0000000 |已添加 |摩托车 | 1006 |已添加 |树木栽培 |
|罗希特父子 | 02:30:00.0000000 | 02:45:00.0000000 |已添加 |摩托车 | 1006 |已添加 |园林绿化服务 |
+ --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ------+
你能帮我写查询以获得想要的结果吗:
+ --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ----------------------+
|企业名称 |约会开始时间 |约会结束时间 |预约状态 |预约|预约ID |预约状态 |服务名称 |
+ --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ----------------------+
|阿米特父子 | 02:30:00.0000000 | 02:45:00.0000000 |已添加 |摩托车 | 1006 |已添加 |树木栽培、景观美化服务 |
|罗希特父子 | 02:30:00.0000000 | 02:45:00.0000000 |已添加 |摩托车 | 1006 |已添加 |树木栽培、景观美化服务 |
+ --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ----------------------+
您正在寻找字符串聚合。从 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;
我有一个查询要从多个表中获取数据。但是不知何故,由于一列,我从数据库中得到了重复的条目。
查询是:
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
输出是这样的:
+ --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ------+ |企业名称 |约会开始时间 |约会结束时间 |预约状态 |预约|预约ID |预约状态 |服务名称 | + --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ------+ |阿米特父子 | 02:00:00.0000000 | 02:15:00.0000000 |已添加 |摩托车 | 1006 |已添加 |树木栽培 | |阿米特父子 | 02:00:00.0000000 | 02:15:00.0000000 |已添加 |摩托车 | 1006 |已添加 |园林绿化服务 | |罗希特父子 | 02:30:00.0000000 | 02:45:00.0000000 |已添加 |摩托车 | 1006 |已添加 |树木栽培 | |罗希特父子 | 02:30:00.0000000 | 02:45:00.0000000 |已添加 |摩托车 | 1006 |已添加 |园林绿化服务 | + --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ------+
你能帮我写查询以获得想要的结果吗:
+ --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ----------------------+ |企业名称 |约会开始时间 |约会结束时间 |预约状态 |预约|预约ID |预约状态 |服务名称 | + --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ----------------------+ |阿米特父子 | 02:30:00.0000000 | 02:45:00.0000000 |已添加 |摩托车 | 1006 |已添加 |树木栽培、景观美化服务 | |罗希特父子 | 02:30:00.0000000 | 02:45:00.0000000 |已添加 |摩托车 | 1006 |已添加 |树木栽培、景观美化服务 | + --------------+--------------------+------------ ------+--------------------+----------------+---- --------------+--------------------+-------------- ----------------------+
您正在寻找字符串聚合。从 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;