1

这是我的查询sql server

SELECT     dbo.tbScheduleDetail.CareProfessionalIDF, 
                dbo.tbScheduleDetail.CareProviderIDF,
                dbo.tbCareProvider.CareProviderName, 
                dbo.tbCitizen.FirstName + ' ' + dbo.tbCitizen.MiddleName + ' ' + dbo.tbCitizen.FamilyName AS CareProfessionalName,  
                dbo.tbScheduleTimeSlot.ScheeduleWeekDays, 
                dbo.tbScheduleTimeSlot.ScheduleStartTime, dbo.tbScheduleTimeSlot.ScheduleEndTime, 

                    dbo.tbScheduleDetail.ScheduleStartDate, dbo.tbScheduleDetail.ScheduleEndDate          
FROM         dbo.tbScheduleTimeSlot INNER JOIN
                      dbo.tbScheduleDetail ON dbo.tbScheduleTimeSlot.ScheduleDetailIDF = dbo.tbScheduleDetail.ScheduleDetailIDP INNER JOIN
                      dbo.tbCareProvider ON dbo.tbScheduleDetail.CareProviderIDF = dbo.tbCareProvider.CareProviderIDP INNER JOIN
                      dbo.tbCareProfessional ON dbo.tbScheduleDetail.CareProfessionalIDF = dbo.tbCareProfessional.CareProfessionalIDP INNER JOIN
                      dbo.tbCitizen ON dbo.tbCareProfessional.CitizenIDF = dbo.tbCitizen.CitizenIDP
WHERE     (dbo.tbScheduleDetail.IsActive = 1) and CareProviderIDF=4 order by CareProfessionalIDF

输出 :

在此处输入图像描述

最后我想像

所以我将 scheduleweekdays 拆分为“,”而不是我得到真实的地方,对于那个索引,我想将 strtime 和 endtime 存储在day[index]

在这里我只取其中一个组合(CareProfessionalIDF=11)我得到了 4 行和存储时间

====================================
         StartTime TO EndTime    
====================================
 day[0] = 00:00TO22:00
 day[1] = 00:00TO22:00
 day[2] = 04:15TO19:15
 day[3] = 04:00TO20:00 
 day[4] = 08:00TO15:00
 day[5] = ""
 day[6] = ""

那么我如何为所有组合存储这样的数据。对于data structure我必须使用的那个?

4

4 回答 4

1

怎么样

Select Sum(CareProfessionalIDF) as CareProfessionalID ,...... from {
-- Your query

} group by CareProfessionalIDF ,......
于 2013-09-09T09:26:15.243 回答
1

尝试这样的事情 -

SELECT  sd.CareProfessionalIDF
    ,   CareProviderIDF = MAX(sd.CareProviderIDF)
    ,   ScheeduleWeekDays = MAX(sts.ScheeduleWeekDays)
    ,   ScheduleTimeSlotIDP = MAX(sts.ScheduleTimeSlotIDP)
    ,   ScheduleStartTime = MAX(sts.ScheduleStartTime)
    ,   ScheduleEndTime = MAX(sts.ScheduleEndTime)
    ,   CareProviderName = MAX(cp.CareProviderName)
    ,   CareProfessionalName = MAX(c.FirstName + ' ' + c.MiddleName + ' ' + c.FamilyName)
    ,   ScheduleFor = MAX(sd.ScheduleFor)
    ,   ScheduleStartDate = MAX(sd.ScheduleStartDate)
    ,   ScheduleEndDate = MAX(sd.ScheduleEndDate)
    ,   SplitIntervalStart = MAX(sts.SplitIntervalStart)
    ,   SplitIntervalEnd = MAX(sts.SplitIntervalEnd)
    ,   SlotInterval = MAX(sts.SlotInterval)
    ,   IsActive = MAX(sd.IsActive)
    ,   IsSplitSchedule = MAX(sts.IsSplitSchedule)
FROM dbo.tbScheduleTimeSlot sts
JOIN dbo.tbScheduleDetail sd ON sts.ScheduleDetailIDF = sd.ScheduleDetailIDP
JOIN dbo.tbCareProvider cp ON sd.CareProviderIDF = cp.CareProviderIDP
JOIN dbo.tbCareProfessional cp2 ON sd.CareProfessionalIDF = cp2.CareProfessionalIDP
JOIN dbo.tbCitizen c ON cp2.CitizenIDF = c.CitizenIDP
WHERE sts.IsActive = 1
    AND CareProviderIDF = 4
GROUP BY sd.CareProfessionalIDF
于 2013-09-09T09:43:08.403 回答
1

当您使用 时Groub by,您需要在 Group By 子句中包含您选择的所有列,或者您需要aggregate其余列,例如

使用聚合查询

SELECT  FieldToGroup ,
        SUM(FieldToSum)
FROM    TableName
GROUP BY FieldToGroup

查询所有字段分组

SELECT  FieldToGroup ,
        Field2ToGroup
FROM    TableName
GROUP BY FieldToGroup ,
        Field2ToGroup
于 2013-09-09T09:11:25.053 回答
0

试试这个...

SELECT  dbo.tbScheduleDetail.CareProfessionalIDF ,
        dbo.tbScheduleDetail.CareProviderIDF ,
        dbo.tbScheduleTimeSlot.ScheeduleWeekDays ,
        dbo.tbScheduleTimeSlot.ScheduleTimeSlotIDP ,
        dbo.tbScheduleTimeSlot.ScheduleStartTime ,
        dbo.tbScheduleTimeSlot.ScheduleEndTime ,
        dbo.tbCareProvider.CareProviderName ,
        dbo.tbCitizen.FirstName + ' ' + dbo.tbCitizen.MiddleName + ' '
        + dbo.tbCitizen.FamilyName AS CareProfessionalName ,
        dbo.tbScheduleDetail.ScheduleFor ,
        dbo.tbScheduleDetail.ScheduleStartDate ,
        dbo.tbScheduleDetail.ScheduleEndDate ,
        dbo.tbScheduleTimeSlot.SplitIntervalStart ,
        dbo.tbScheduleTimeSlot.SplitIntervalEnd ,
        dbo.tbScheduleTimeSlot.SlotInterval ,
        dbo.tbScheduleDetail.IsActive ,
        dbo.tbScheduleTimeSlot.IsSplitSchedule
FROM    dbo.tbScheduleTimeSlot
        INNER JOIN dbo.tbScheduleDetail ON dbo.tbScheduleTimeSlot.ScheduleDetailIDF = dbo.tbScheduleDetail.ScheduleDetailIDP
        INNER JOIN dbo.tbCareProvider ON dbo.tbScheduleDetail.CareProviderIDF = dbo.tbCareProvider.CareProviderIDP
        INNER JOIN dbo.tbCareProfessional ON dbo.tbScheduleDetail.CareProfessionalIDF = dbo.tbCareProfessional.CareProfessionalIDP
        INNER JOIN dbo.tbCitizen ON dbo.tbCareProfessional.CitizenIDF = dbo.tbCitizen.CitizenIDP
WHERE   ( dbo.tbScheduleTimeSlot.IsActive = 1 )
        AND CareProviderIDF = 4 ORDER BY dbo.tbScheduleDetail.CareProfessionalIDF
于 2013-09-09T09:51:41.613 回答