2017-02-13 6 views
0

私はweekdat sundayにのみ含めるwhere句を持つ別のテーブルからの値SundayとSundayTimesを持つ日曜日のテーブルを持っています。日曜日は、カウントされた軍事時間00:00から23:00のリストであるSundayTimesの時間内に仕事を記録した病院メンバーのIDの数です。あるので、それはリストの正しい順序でフォーマットカウントが存在しない別のテーブルから行の値を表示する方法

02:00  0 

に表示されるので、何時間では、私が行方不明の時間を占めるだろうか

00:00  3 
01:00  4 
03:00  2 

として、午前2時用のテーブルが表示され記録されません。私は可能性のあるすべての欠落時間を考慮に入れることができなければなりません。なぜなら、これが引き出すパラメータは日付と日付が異なるからです。日曜日のための

結果: 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 

ドロップテーブル#TestingTesting123 ドロップテーブル#Sundays ドロップテーブル#Mondays ドロップテーブル#Tuesdays ドロップテーブル#Wednesdays ドロップテーブル#Thursdays ドロップテーブル#Fridays ドロップテーブル#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 

申し訳ありませんが間違いがある場合は、投稿する前にテストすることができませんでした。

説明:1時間が不足している場合は、すべての時間に0をカウントとして追加します。

NEW ANSWER:

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)。また、テーブル登録の参照が全くない新しいテーブルtestingtesing123を作成している場合、テーブル登録からr.Id値を取得する方法もあります。 –

+0

自分の作成を置き換え、私の例でテストテストを行います123。 – ventik

関連する問題