我有一個表星期日值與星期日和SundayTimes來自另一個表,其中有一個where子句,只包括weekdat星期日。星期日是在星期日時間登錄工作的醫院成員的ID的計數,這是與00:00至23:00相關的軍事時間列表。由於沒有記錄任何小時02:00,該表顯示爲如何從缺少count的另一個表中顯示缺少的行值?
00:00 3
01:00 4
03:00 2
我怎麼會佔到缺少時間,所以它顯示的格式
02:00 0
在列表中的正確順序。我必須能夠考慮所有可能的缺失時間,因爲這些參數可能與日期不同並且與日期不同。
結果星期日: enter image description here
這裏是我到目前爲止所。
CREATE TABLE #TestingTesting123 (Id bigint, WkDay varchar(30), Admittime varchar(5), primary key(Id))
INSERT INTO #TestingTesting123 (Id, WkDay, Admittime)
SELECT r.Id,
datename(dw, r.AdmitDate) As WkDay,
CASE
WHEN r.Admittime = ':'
THEN '00:00'
ELSE
isnull(LEFT(r.Admittime,2),0) + ':00 '
END as AdmitTime
FROM registrations r
WHERE r.AdmitDate between @fromdate AND @todate
AND r.registrationtypeid = @RegistrationTypeId
ORDER BY AdmitTime Asc
create table #Sundays (Sunday BIGINT, SundayTimes varchar(5))
INSERT INTO #Sundays (Sunday, SundayTimes)
SELECT Count(t.Id) As Sunday,
t.Admittime as SundayTimes
FROM #TestingTesting123 t
WHERE t.WkDay = 'Sunday'
GROUP BY t.Admittime
create table #Mondays (Monday bigint, MondayTimes varchar(5))
insert into #Mondays (Monday, MondayTimes)
Select count(t.Id) As Monday,
t.Admittime as MondayTimes
from #TestingTesting123 t
where t.WkDay = 'Monday'
group by t.Admittime
create table #Tuesdays (Tuesday bigint, TuesdayTimes varchar(5))
insert into #Tuesdays (Tuesday, TuesdayTimes)
select count(t.Id) as Tuesday,
t.Admittime as TuesdayTimes
from #TestingTesting123 t
where t.WkDay = 'Tuesday'
group by t.Admittime
create table #Wednesdays (Wednesday bigint, WednesdayTimes varchar(5))
insert into #Wednesdays (Wednesday, WednesdayTimes)
select count(t.Id) AS Wednesday,
t.Admittime as WednesdayTimes
from #TestingTesting123 t
where t.WkDay = 'Wednesday'
group by t.Admittime
create table #Thursdays (Thursday bigint, ThursdayTimes varchar(5))
insert into #Thursdays (Thursday, ThursdayTimes)
select count(t.Id) as Thursday,
t.Admittime as ThursdayTimes
from #TestingTesting123 t
where t.WkDay = 'Thursday'
group by t.Admittime
create table #Fridays (Friday bigint, FridayTimes varchar(5))
insert into #Fridays (Friday, FridayTimes)
select count(t.Id) As Friday,
t.Admittime as FridayTimes
from #TestingTesting123 t
where t.WkDay = 'Friday'
group by t.Admittime
create table #Saturdays (Saturday bigint, SaturdayTimes varchar(5))
insert into #Saturdays (Saturday, SaturdayTimes)
select count(t.Id) as Saturday,
t.Admittime as SaturdayTimes
from #TestingTesting123 t
where t.WkDay = 'Saturday'
group by t.Admittime
declare @FinalResults table(AdmitTime varchar(5), Sunday bigint, Monday bigint, Tuesday bigint, Wednesday bigint, Thursday bigint, Friday bigint, Saturday bigint)
insert into @FinalResults (AdmitTime, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
select su.SundayTimes as AdmitTime,
su.Sunday as Sunday,
m.Monday as Monday,
t.Tuesday as Tuesday,
w.Wednesday as Wednesday,
th.Thursday as Thursday,
f.Friday as Friday,
s.Saturday as Saturday
from #Sundays su
inner join #Mondays m on m.MondayTimes = su.SundayTimes
inner join #Tuesdays t on t.TuesdayTimes = su.SundayTimes
inner join #Wednesdays w on w.WednesdayTimes = su.SundayTimes
inner join #Thursdays th on th.ThursdayTimes = su.SundayTimes
inner join #Fridays f on f.FridayTimes = su.SundayTimes
inner join #Saturdays s on s.SaturdayTimes = su.SundayTimes
select fr.AdmitTime,
fr.Sunday,
fr.Monday,
fr.Tuesday,
fr.Wednesday,
fr.Thursday,
fr.Friday,
fr.Saturday
from @FinalResults fr
DROP TABLE#TestingTesting123 DROP TABLE #Sundays DROP TABLE #Mondays DROP TABLE #Tuesdays DROP TABLE #Wednesdays DROP TABLE #Thursdays DROP TABLE #Fridays DROP TABLE #Saturdays
你使用什麼SQL Server版本? – ventik