0

所以这个问题与我之前问过的问题相似,但略有不同。

我正在查看被录取和退出项目的客户的数据。对于每次入院和出院,他们都会进行评估并对​​其进行评分,有时他们会在一段时间内多次入院和出院。

我需要能够将每个客户的录取分数与其下一个出院日期配对,这样我就可以查看所有在每次入院和出院时从入院到出院都提高了一定数量的客户。

这是我的数据结果现在如何格式化的虚拟样本: 前

这就是我理想的格式: 在此处输入图像描述

但我会采取任何正确的方向或类似的格式帮助,这将使我能够比较所有客户的所有录取和出院分数的实例。

谢谢!

4

3 回答 3

2

为了获得结果,您可以同时应用 UNPIVOT 和 PIVOT 函数。UNPIVOT 会将您的多列datescore转换为行,然后您可以将这些行转回为列。

然后 unpivot 语法将与此类似:

select person,
  casenumber,
  ScoreType+'_'+col col,
  value,
  rn
from
(
  select person,
    casenumber,
    convert(varchar(10), date, 101) date,
    cast(score as varchar(10)) score,
    scoreType,
    row_number() over(partition by casenumber, scoretype
                      order by case scoretype when 'Admit' then 1 end, date) rn            
  from yourtable
) d
unpivot
(
  value
  for col in (date, score)
) unpiv

请参阅SQL Fiddle with Demo。这给出了一个结果:

| PERSON | CASENUMBER |             COL |      VALUE | RN |
-----------------------------------------------------------
|    Jon |       3412 |  Discharge_date | 01/03/2013 |  1 |
|    Jon |       3412 | Discharge_score |         12 |  1 |
|     Al |       3452 |      Admit_date | 05/16/2013 |  1 |
|     Al |       3452 |     Admit_score |         15 |  1 |
|     Al |       3452 |  Discharge_date | 08/01/2013 |  1 |
|     Al |       3452 | Discharge_score |         13 |  1 |

如您所见,此查询还创建了新列以进行透视。所以最终的代码将是:

select person, casenumber,
  Admit_Date, Admit_Score, Discharge_Date, Discharge_Score
from
(
  select person,
    casenumber,
    ScoreType+'_'+col col,
    value,
    rn
  from
  (
    select person,
      casenumber,
      convert(varchar(10), date, 101) date,
      cast(score as varchar(10)) score,
      scoreType,
      row_number() over(partition by casenumber, scoretype
                        order by case scoretype when 'Admit' then 1 end, date) rn

    from yourtable
  ) d
  unpivot
  (
    value
    for col in (date, score)
  ) unpiv
) src
pivot
(
  max(value)
  for col in (Admit_Date, Admit_Score, Discharge_Date, Discharge_Score)
) piv;

请参阅SQL Fiddle with Demo。这给出了一个结果:

| PERSON | CASENUMBER | ADMIT_DATE | ADMIT_SCORE | DISCHARGE_DATE | DISCHARGE_SCORE |
-------------------------------------------------------------------------------------
|     Al |       3452 | 05/16/2013 |          15 |     08/01/2013 |              13 |
|  Cindy |       6578 | 01/02/2013 |          17 |     03/04/2013 |              14 |
|  Cindy |       6578 | 03/04/2013 |          14 |     03/18/2013 |              12 |
|    Jon |       3412 |     (null) |      (null) |     01/03/2013 |              12 |
|  Kevin |       9868 | 01/18/2013 |          19 |     03/02/2013 |              15 |
|  Kevin |       9868 | 03/02/2013 |          15 |         (null) |          (null) |
|   Pete |       4765 | 02/06/2013 |          15 |         (null) |          (null) |
|  Susan |       5421 | 04/06/2013 |          19 |     05/07/2013 |              15 |
于 2013-05-03T15:00:41.753 回答
1

SELECT 
ad.person, ad.CaseNumber, ad.Date as AdmitScoreDate, ad.Score as AdmitScore,
dis.date as DischargeScoreDate, dis.Score as DischargeScore
From
yourTable ad, yourTable dis

WHERE ad.person=dis.person and ad.ScoreType='Admit' and d is.ScoreType='Discharge';

于 2013-05-03T15:03:31.683 回答
1

如果您提到的所有列都在同一个表中,您可以加入同一个表

 SELECT t1.person,
  t1.caseNumber,
   t1.date adate,
  t1.score  ascore,
  t1.scoreType ascoreType,
  t2.date ddate,
  t2.score  dscore,
  t2.scoreType dscoretype
FROM patient t1
join patient t2
on t1.casenumber=t2.casenumber
and t1.scoreType!=t2.scoreType
and t1.scoreType='Admit'

但这不会向您显示已入院但尚未出院的人的记录。我不知道您是否也在寻找该信息。

SQL 小提琴链接

希望这可以帮助!

于 2013-05-03T15:30:49.707 回答