SQL SERVER 2014_两个表之间的区别

时间:2017-02-17 20:20:12

标签: sql sql-server

enter image description here

表A-为了扭伤而去医院的运动员 表B-在家中进行随访/随访访问/访问的运动员

DATASET要大得多,只是给了一个片段作为例子

我如何在SQL SERVER ....

  1. 如果在几天内有跟进< 7,将值1赋给名为COL_IND的col,否则为0
  2. 列出第一次跟进的DATE,否则留空,放入名为follow_up的列表
  3. 住院访问和首次在家跟进之间的天数,如果没有后续留空,请将其放入名为DAYS_TO_FOLLOW的新COL中
  4. 我已经做到了:

    1. 根据访问
    2. 设定SQL中的值
    3. 我的代码已将日期分为年,月和日
    4. 我被困的地方 3.我不知道是否要加入这两个表,因为我需要让名字匹配。也在考虑IFF声明,但我不知道如何让名字匹配。

      以下是我的尝试:

      SELECT 
         CONVERT (float,#test1.["DATE"]) - CONVERT(float,#test2.["DATE"]) AS DIFF
      FROM #test1
      JOIN #test2 ON #test1.["NAME"] = #test2.["NAME"]
      

1 个答案:

答案 0 :(得分:0)

内部查询识别每个人的第一次访问,然后将其加入到医院访问的人员中。

USE tempdb;
CREATE TABLE dbo.HospitalVisit (Name VARCHAR(10), Code1 INT, VisitDate DATE);
CREATE TABLE dbo.FollowUpVisit (Name VARCHAR(10), Code1 INT, VisitDate DATE);

INSERT INTO dbo.HospitalVisit
        ( Name ,
          Code1 ,
          VisitDate
        )
VALUES  ('Chris',85,'20140905'),
('Bob',85,'20140905'),
('Angel',85,'20140905'),
('Tyler',85,'20140905'),
('Rick',85,'20140905');
INSERT INTO dbo.FollowUpVisit
        ( Name ,
          Code1 ,
          VisitDate
        )
VALUES  ('Chris',83,'20140915'),
('Chris',83,'20141015'),
('Chris',83,'20141018'),
('Bob',83,'20140910'),
('Angel',83,'20141006');


SELECT  hv.Name,
        HospitalVisitDate = hv.VisitDate,
        Flag = CASE
                 WHEN DATEDIFF(DAY, hv.VisitDate, FUV.FirstVisit) < 7 THEN
                   1
                 ELSE
                   0
               END,
        FUV.FirstVisit,
        DaystoFirstVisit = DATEDIFF(DAY, hv.VisitDate, FUV.FirstVisit)
FROM    dbo.HospitalVisit AS hv
  LEFT JOIN
        ( SELECT    FUV.Name,
                    FirstVisit = MIN(FUV.VisitDate)
          FROM  dbo.FollowUpVisit AS FUV
          GROUP BY FUV.Name
        ) AS FUV
    ON hv.Name = FUV.Name;

但是,您可以通过将所有访问放在一个表中并使用其他列来指示访问类型来简化您的设计。

提供新要求后的代码:

USE tempdb;
IF OBJECT_ID('tempdb..HospitalVisit') IS NOT NULL
  DROP TABLE dbo.HospitalVisit;
GO
IF OBJECT_ID('tempdb..FollowUpVisit') IS NOT NULL
  DROP TABLE dbo.FollowUpVisit;
GO


CREATE TABLE dbo.HospitalVisit (Name VARCHAR(10),Code1 INT,VisitDate DATE);
CREATE TABLE dbo.FollowUpVisit (Name VARCHAR(10),Code1 INT,VisitDate DATE);

INSERT INTO dbo.HospitalVisit ( Name,Code1,VisitDate)
    VALUES ('Chris', 85, '20140905'),
    ('Bob', 85, '20140905'),
    ('Bob', 85, '20141005'),
    ('Angel', 85, '20140905'),
    ('Tyler', 85, '20140905'),
    ('Rick', 85, '20140905');
INSERT INTO dbo.FollowUpVisit ( Name,Code1,VisitDate)
    VALUES ('Chris', 83, '20140915'),
    ('Chris', 83, '20141015'),
    ('Chris', 83, '20141018'),
    ('Bob', 83, '20140920'),
    ('Bob', 83, '20140910'),
    ('Bob', 83, '20141010'),
    ('Bob', 83, '20141020'),
    ('Angel', 83, '20140901'),
    ('Tyler', 85, '20140904'),
    ('Angel', 83, '20141006');

WITH cte
AS (SELECT  hv.Name,
            HospitalCode = hv.Code1,
            HospitalVisit = hv.VisitDate,
            FUCode = fuv.Code1,
            FirstFollowUpVisit = fuv.VisitDate,
            FUSeq = ROW_NUMBER() OVER (PARTITION BY hv.Name, hv.VisitDate ORDER BY fuv.VisitDate),
            DaystoFirstFollowUp = DATEDIFF(DAY,hv.VisitDate, fuv.VisitDate)
    FROM    dbo.HospitalVisit AS hv
      LEFT JOIN dbo.FollowUpVisit AS fuv
        ON hv.Name = fuv.Name
           AND  hv.VisitDate <= fuv.VisitDate
   )
SELECT  cte.Name ,
        cte.HospitalCode ,
        cte.HospitalVisit ,
        cte.FUCode ,
        cte.FirstFollowUpVisit ,
        cte.DaystoFirstFollowUp
FROM    cte
WHERE   cte.FUSeq = 1;

我添加了一些新的示例数据。 鲍勃有两次医院就诊和三次新的随访。

天使在医院就诊前后进行了随访。

泰勒在医院就诊前进行了随访:

结果:

Name    HospitalCode    HospitalVisit   FUCode  FirstFollowUpVisit  DaystoFirstFollowUp
Angel   85  2014-09-05  83  2014-10-06  31
Bob 85  2014-09-05  83  2014-09-10  5
Bob 85  2014-10-05  83  2014-10-10  5
Chris   85  2014-09-05  83  2014-09-15  10
Rick    85  2014-09-05  NULL    NULL    NULL
Tyler   85  2014-09-05  NULL    NULL    NULL

注意:由于访问时间不包括在内,因此在医院就诊后的同一天进行FU访问计算。您可以将访问条件从AND hv.VisitDate&lt; = fuv.VisitDate更改为AND hv.VisitDate&lt; fuv.VisitDate如果不正确。