Question

I have problem about combining tables in store procedure.
Note : field "Time" is varchar
First table (tbTime)

Time
08:00:00
08:30:00
09:00:00
09:30:00
10:00:00
10:30:00
11:00:00
11:30:00
12:00:00

Second table (tbClientData)
select Time,Name from tbclientdata where appointment='2013/10/26' order by time

Time        Name
08:30:00    MaLa
08:35:00    Mara
08:40:00    Sarah
09:00:00    Nafira
09:00:00    Sarah AmeLia
09:00:00    Denur Jumaran
10:00:00    MuLyono
10:00:00    Lucky Strada Nova

I want the result with condition schedule like this

Time        Name
08:00:00    -
08:30:00    MaLa, Mara, Sarah
09:00:00    Nafira, Sarah AmeLia, Denur Jumaran
09:30:00    -
10:00:00    MuLyono, Lucky Strada Nova
10:30:00    -
11:00:00    -
11:30:00    -
12:00:00    -

Thanks for reading or answer ^_^

GBU

I`ve tried this

select c1.Time, 
stuff((select distinct ', '+cast(Name as varchar(200))
from tbclientdata c2 where c2.time=c1.time
for xml path('')),1,1,'')
from tbclientdata c1
where c1.Appointment='2013/10/26'
group by c1.Time

The result is....

Time         Name
08:30:00     MaLa
08:35:00     Mara, Mr
08:40:00     Sarah
09:00:00     Denur Jumaran, Nafira, Sarah AmeLia, Yason
10:00:00     Lucky Strada Nova, MuLyono
Was it helpful?

Solution

WITH HalfHours AS (
    SELECT CONVERT(datetime, Time, 108) As HalfHour
    FROM tbTime
)
,HalfHourNames AS (
    SELECT DATEADD(second, -SecondsSinceHalfHour, RealTime) As HalfHour
          ,Name
    FROM tbclientdata
         CROSS APPLY (
             SELECT CONVERT(datetime, Time, 108) As RealTime
         ) AS CA1
         CROSS APPLY (
             SELECT (DATEPART(minute, RealTime) % 30) * 60 + DATEPART(second, RealTime) As SecondsSinceHalfHour
         ) AS CA2
    WHERE Appointment = '20131026'
)
SELECT HalfHours.HalfHour
      ,COALESCE(CA2.CommaSeparatedNames, '-') AS Names
FROM HalfHours
     CROSS APPLY (
         SELECT (SELECT ', ' + HalfHourNames.Name
                 FROM HalfHourNames
                 WHERE HalfHours.HalfHour = HalfHourNames.HalfHour
                 FOR XML PATH('')
                ) AS RawCommaSeparatedNames
     ) AS CA1
     CROSS APPLY (
         SELECT STUFF(CA1.RawCommaSeparatedNames, 1, 2, '') AS CommaSeparatedNames
     ) AS CA2
GROUP BY HalfHours.HalfHour
        ,CA2.CommaSeparatedNames

OTHER TIPS

Try this:

;WITH HalfHourIntervals AS
(
    SELECT CAST('2013-10-26 00:00:00' AS DATETIME) interval
    UNION ALL
    SELECT DATEADD(MINUTE, 30, interval)
    FROM HalfHourIntervals
    WHERE interval < '2013-01-01 23:59:59'
)    

select hi.Interval, stuff(
    (select distinct ', '+cast(Name as varchar(200))
     from tbclientdata c2 where c2.time=c1.time
     for xml path('')),1,1,'')
from tbclientdata c1
right join HalfHourIntervals hi on hi.interval = cast(round(cast(cast(convert(varchar(30),c1.[Time],121) as datetime) as float) * (24.0/0.5),0)/(24.0/0.5) as datetime)
where c1.Appointment='2013-10-26'
group by c1.[Time]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top