在DataBase中组合行信息

时间:2014-09-03 19:06:40

标签: sql sql-server ssms

我目前正在处理一项需要合并

的信息的查询

以下是我目前的查询

SELECT DISTINCT
    s.SiteID,
    s.GroupID,
    sa.StartDateTime,
    u.UserFirstName + SPACE(1) + UserLastName ScanName,
    CONVERT(VARCHAR,sa.StartDateTime,101) ApptStartDate,
    CONVERT(VARCHAR,sa.EndDateTime,101) ApptEndDate,
    CONVERT(VARCHAR(15),CAST(sa.StartDateTime AS TIME),100) ApptStartTime,
    saa.Time ArrivalTime,
    sl.Address,
    st.StateShortCode,
    ct.CityName,
    CASE saa.CheckInCallType 
        WHEN 1 THEN 'Inbound' 
        WHEN 2 THEN 'Outbound' END AS CallCheckInType,
    CASE WHEN saa.CheckInStatus = 1 THEN 'Arrived' 
        WHEN saa.CheckInStatus = 2 THEN 'No Show'
        WHEN saa.CheckInStatus = 3 THEN 'Reschedule'
        WHEN saa.CheckInStatus = 4 THEN 'Provider Reschedule'
        WHEN sa.SiteAppointmentStatusID=998 THEN 'Cancelled'
        ELSE 'Pending' END AS CheckInStatus,
    ISNULL(saa.CheckInComments,'') CheckInComments,
    um.ManagerName,
    u.UserPhone,
    u.UserCell,
    u.UserAddress,
    ci.CityName as ScannerCityName,
    st.StateShortCode as ScannerState
FROM 
    dbo.Sites s
    INNER JOIN dbo.SiteAppointments sa ON s.SiteID = sa.SiteID
    INNER JOIN dbo.SiteAppointmentsScanners sas ON sa.SiteAppointmentID = sas.SiteAppointmentID
    INNER JOIN dbo.Users u ON sas.ScannerUserID = u.UserID
    INNER JOIN dbo.SiteLocation sl ON sa.SiteID = sl.SiteID
    INNER JOIN dbo.States st ON sl.StateID = st.StateID
    INNER JOIN dbo.Cities ct ON sl.CityID = ct.CityID
    LEFT JOIN dbo.SiteAppointmentArrivals saa ON sa.SiteAppointmentID = saa.SiteAppointmentId 
                                              AND saa.ScannerUserID = sas.ScannerUserID
    LEFT JOIN dbo.UserManagers um ON u.UserManagerID=um.ManagerID
    LEFT JOIN dbo.Cities ci ON u.CityID=ci.CityID
    LEFT JOIN dbo.States st2 ON st2.StateID=u.StateID
WHERE 
    ProjectID = 110
    AND 
        (
            (CAST(sa.StartDateTime AS DATE) >= '09/03/2014' AND CAST(sa.StartDateTime AS DATE) <= '09/03/2014')
            OR
            (CAST(sa.EndDateTime AS DATE) >= '09/03/2014' AND CAST(sa.EndDateTime AS DATE) <= '09/03/2014')
        )
    AND ((CAST(saa.Date AS DATE) >= '09/03/2014' AND CAST(saa.Date AS DATE) <= '09/03/2014') OR saa.Date IS NULL)

信息很好,但我想将一些行信息合并到一个信息块中。

以下是一个例子:

实施例1,

Site ID | Group ID | StateDateTime            | ScanName | ApptStartDate | ApptEndDate | ApptStartTime | ArrivalTime             | Address | StateShortCode | CityName | CallCheckInType | CheckInComments | Manager Name | UserPhone | UserCell | UserAddress | ScannerCityName | Scanner State
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
12345   | 67890    | 2014-09-03 09:30:00.000  | Jane Doe | 09/03/2014    | 09/03/2014  | 9:30AM        | 2014-09-03 09:13:01.507 | 123 St  | NY             | New York | Inbound         |                 | Manager1     | 1234567   |          | 321 St      | New York        | NY 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
54321   | 67890    | 2014-09-03 09:30:00.000  | Jane Doe | 09/03/2014    | 09/03/2014  | 9:30AM        | 2014-09-03 09:13:01.507 | 123 St  | NY             | New York | Inbound         |                 | Manager1     | 1234567   |          | 321 St      | New York        | NY 

GroupID显示许多SiteID(都具有唯一编号)实际上组合在一起成为一个GroupID。我想将这些行组合成一行(而不是显示多个SiteID的多行),只显示一个SiteID(不管哪一个)

我也可以获得这样的实例

例2

Site ID | Group ID | StateDateTime            | ScanName | ApptStartDate | ApptEndDate | ApptStartTime | ArrivalTime             | Address | StateShortCode | CityName | CallCheckInType | CheckInComments | Manager Name | UserPhone | UserCell | UserAddress | ScannerCityName | Scanner State
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
09876   | 24680    | 2014-09-03 08:00:00.000  | John Doe | 09/03/2014    | 09/03/2014  | 8:00AM        | NULL                    | 123 St  | NY             | New York | Pending         |                 | Manager1     | 1234567   |          | 321 St      | New York        | NY 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
09877   | 24680    | 2014-09-03 08:00:00.000  | John Doe | 09/03/2014    | 09/03/2014  | 8:00AM        | 2014-09-03 09:09:48.053 | 123 St  | NY             | New York | Inbound         |                 | Manager1     | 1234567   |          | 321 St      | New York        | NY 

如果用户到达约会但未登录组内的所有站点(因此我获得了到达时间的空值) - 但是,如果用户注册到组内的一个站点,他们仍然应该被视为进入整个集团。在这种情况下,我也希望&#34;结合&#34; - 或删除Null案例,但保留非空值(在此示例中,只有一个站点具有日期,但可能是该组中可以登录的多个站点,但是一些为空的扼杀者)。我知道有一个JOIN而不是LEFT JOIN可以帮助解决这个问题。但是,我仍然希望获得实例的NULL值,因为该人未在组中的任何站点中显示为全部。

1 个答案:

答案 0 :(得分:0)

**根据评论更新**

执行此操作的绝对最便宜的方法如下:

SELECT DISTINCT
    max(s.SiteID),
    s.GroupID,
    sa.StartDateTime,
    u.UserFirstName + SPACE(1) + UserLastName ScanName,
    CONVERT(VARCHAR,sa.StartDateTime,101) ApptStartDate,
    CONVERT(VARCHAR,sa.EndDateTime,101) ApptEndDate,
    CONVERT(VARCHAR(15),CAST(sa.StartDateTime AS TIME),100) ApptStartTime,
    saa.Time ArrivalTime,
    sl.Address,
    st.StateShortCode,
    ct.CityName,
    CASE saa.CheckInCallType 
        WHEN 1 THEN 'Inbound' 
        WHEN 2 THEN 'Outbound' END AS CallCheckInType,
    CASE WHEN saa.CheckInStatus = 1 THEN 'Arrived' 
        WHEN saa.CheckInStatus = 2 THEN 'No Show'
        WHEN saa.CheckInStatus = 3 THEN 'Reschedule'
        WHEN saa.CheckInStatus = 4 THEN 'Provider Reschedule'
        WHEN sa.SiteAppointmentStatusID=998 THEN 'Cancelled'
        ELSE 'Pending' END AS CheckInStatus,
    ISNULL(saa.CheckInComments,'') CheckInComments,
    um.ManagerName,
    u.UserPhone,
    u.UserCell,
    u.UserAddress,
    ci.CityName as ScannerCityName,
    st.StateShortCode as ScannerState
FROM 
    dbo.Sites s
    INNER JOIN dbo.SiteAppointments sa ON s.SiteID = sa.SiteID
    INNER JOIN dbo.SiteAppointmentsScanners sas ON sa.SiteAppointmentID = sas.SiteAppointmentID
    INNER JOIN dbo.Users u ON sas.ScannerUserID = u.UserID
    INNER JOIN dbo.SiteLocation sl ON sa.SiteID = sl.SiteID
    INNER JOIN dbo.States st ON sl.StateID = st.StateID
    INNER JOIN dbo.Cities ct ON sl.CityID = ct.CityID
    LEFT JOIN dbo.SiteAppointmentArrivals saa ON sa.SiteAppointmentID = saa.SiteAppointmentId 
                                              AND saa.ScannerUserID = sas.ScannerUserID
    LEFT JOIN dbo.UserManagers um ON u.UserManagerID=um.ManagerID
    LEFT JOIN dbo.Cities ci ON u.CityID=ci.CityID
    LEFT JOIN dbo.States st2 ON st2.StateID=u.StateID
WHERE 
    ProjectID = 110
    AND 
        (
            (CAST(sa.StartDateTime AS DATE) >= '09/03/2014' AND CAST(sa.StartDateTime AS DATE) <= '09/03/2014')
            OR
            (CAST(sa.EndDateTime AS DATE) >= '09/03/2014' AND CAST(sa.EndDateTime AS DATE) <= '09/03/2014')
        )
    AND ((CAST(saa.Date AS DATE) >= '09/03/2014' AND CAST(saa.Date AS DATE) <= '09/03/2014') OR saa.Date IS NULL)
GROUP BY
    2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, 19 -- short hand for grouping columns together, because there are 20 columns, and only the first one is excluded;