2017-02-13 34 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