2017-02-13 103 views
0

我有一个表星期日值与星期日和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

+0

你使用什么SQL Server版本? – ventik

回答

0

在上次查询之前,您可以添加此步骤:

DECLARE @i INT; 
DECLARE @time VARCHAR(5); 
SET @i=0; 
WHILE @i < 24 
BEGIN 
    SET @time = RIGHT('00'+CAST(@i AS VARCHAR(2)),2)+':00' 
    INSERT INTO @Sundays 
    SELECT 0, @time 
    SET @i = @i + 1; 
END 

对不起,如果出现错误,我无法在发布前测试。

解释:为了以防万一,我们将所有小时添加为0作为计数。

新的答案:

create table #empty_hours (Days BIGINT, Times varchar(5)) 
DECLARE @i INT; 
DECLARE @time VARCHAR(5); 
SET @i=0; 
WHILE @i < 24 
BEGIN 
    SET @time = RIGHT('00'+CAST(@i AS VARCHAR(2)),2)+':00' 
    INSERT INTO #empty_hours 
    SELECT 0, @time 
    SET @i = @i + 1; 
END 

INSERT INTO #Sundays SELECT * from #empty_hours ; 
INSERT INTO #Mondays SELECT * from #empty_hours ; 
INSERT INTO #Tuesdays SELECT * from #empty_hours ; 
INSERT INTO #Wednesdays SELECT * from #empty_hours ; 
INSERT INTO #Thursdays SELECT * from #empty_hours ; 
INSERT INTO #Fridays SELECT * from #empty_hours ; 
INSERT INTO #Saturdays SELECT * from #empty_hours ; 

新的解释:

我们创建一个#empty_hours临时表;我们使用0作为帐户填写所​​有小时(结果没有发生)。然后在所有表格中插入这些“空”值,以防缺失一些小时。

+0

我在@ BEGIN之后的行中得到@time,'00',@i和2下面的红色不正确的语法行。 –

+0

我忘了添加'SET'。我刚刚编辑了我的帖子。希望它有效。 – devoh

+0

这是有效的,但唯一的问题是它使这些表中我的group by子句过时,所以时间一遍又一遍地重复着。 –

0

我认为这是你需要的。你不需要每天都使用单独的表格。

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 

-- table with all hours in day 
declare @Hours table 
(
    T_Hour varchar(5) 
) 

declare @i smallint 
set @i = 0 
while @i < 24 
begin 
    insert into @Hours 
    select right('00' + cast(@i as varchar), 2) + ':00' 

    set @i = @i + 1 
end 

-- result query 
select 
    T_Hour, 
    ISNULL(SUM(case when WkDay = 'Sunday' then CountValue else 0 end), 0) as Sunday, 
    ISNULL(SUM(case when WkDay = 'Monday' then CountValue else 0 end), 0) as Monday, 
    ISNULL(SUM(case when WkDay = 'Tuesday' then CountValue else 0 end), 0) as Tuesday, 
    ISNULL(SUM(case when WkDay = 'Wednesday' then CountValue else 0 end), 0) as Wednesday, 
    ISNULL(SUM(case when WkDay = 'Thursday' then CountValue else 0 end), 0) as Thursday, 
    ISNULL(SUM(case when WkDay = 'Friday' then CountValue else 0 end), 0) as Friday, 
    ISNULL(SUM(case when WkDay = 'Saturday' then CountValue else 0 end), 0) as Saturday 
from @Hours 
    left outer join 
    (
     select WkDay, Admittime, count(Id) as CountValue     
     from #TestingTesting123 
     group by WkDay, Admittime 
    ) T 
    on T_Hour = Admittime 
group by T_Hour  
+0

因此,我将在星期日的所有时段,星期一的所有时段,星期二的所有时段以及星期六继续进行表格测试。 –

+0

我收到错误消息,已经有一个表名(testingtesting123)。另外如果我正在创建一个没有引用表注册的新表testtesing123,我将如何从表注册中获取r.Id值。 –

+0

在我的例子中,替换你自己的创建并填充testingtesting123。 – ventik