【发布时间】:2021-11-21 11:34:42
【问题描述】:
原始数据
| Orders | Service | ID | Date | State | Name | Results |
|---|---|---|---|---|---|---|
| 7 | Vitals | 12345 | 10/2/2019 | Alabama | Systole | 115 |
| 39 | Vitals | 12345 | 10/2/2019 | Alabama | BMI | 27.5 |
| 62 | Vitals | 12345 | 10/2/2019 | Alabama | Diastole | 64 |
| 519 | Vitals | 12345 | 10/2/2019 | Alabama | Diastole | 70 |
| 114 | Vitals | 12345 | 11/5/2019 | Alabama | Systole | 111 |
| 442 | Vitals | 12345 | 11/5/2019 | Alabama | BMI | 28 |
| 10 | Vitals | 12345 | 12/19/2019 | Alabama | BMI | 29 |
| 89 | Vitals | 12345 | 12/19/2019 | Alabama | Systole | 100 |
| 90 | Vitals | 12345 | 12/19/2019 | Alabama | Diastole | 62 |
| 529 | Vitals | 12345 | 12/19/2019 | Alabama | Diastole | 90 |
| 417 | Vitals | 12345 | 12/19/2019 | Alabama | Systole | 99 |
| 77 | Vitals | 12345 | 4/7/2020 | Alabama | Systole | 110 |
| 78 | Vitals | 12345 | 4/7/2020 | Alabama | Diastole | NULL |
| 343 | Vitals | 12345 | 4/7/2020 | Alabama | BMI | 23 |
| 120 | Vitals | 12345 | 4/7/2020 | Alabama | Diastole | NULL |
| 321 | Vitals | 12345 | 4/7/2020 | Alabama | Systole | 106 |
| 39 | Vitals | 12345 | 9/29/2020 | Alabama | Diastole | 82 |
| 97 | Vitals | 12345 | 9/29/2020 | Alabama | Systole | 120 |
| 815 | Vitals | 12345 | 9/29/2020 | Alabama | BMI | 22 |
| 19 | Vitals | 12345 | 2/2/2021 | Alabama | Systole | 108 |
| 20 | Vitals | 12345 | 2/2/2021 | Alabama | Diastole | 59 |
| 983 | Vitals | 12345 | 2/2/2021 | Alabama | BMI | 24 |
| 984 | Vitals | 12345 | 2/2/2021 | Alabama | Systole | 133 |
| 210 | Vitals | 12345 | 2/2/2021 | Alabama | Diastole | 68 |
期望的结果
| Service | ID | Date | State | Name | Results |
|---|---|---|---|---|---|
| Vitals | 12345 | 10/2/2019 | Alabama | Blood Pressure | 115/70 |
| Vitals | 12345 | 10/2/2019 | Alabama | BMI | 27.5 |
| Vitals | 12345 | 11/5/2019 | Alabama | Systole | 111 |
| Vitals | 12345 | 11/5/2019 | Alabama | BMI | 28 |
| Vitals | 12345 | 12/19/2019 | Alabama | BMI | 29 |
| Vitals | 12345 | 12/19/2019 | Alabama | Blood Pressure | 100/90 |
| Vitals | 12345 | 4/7/2020 | Alabama | Systole | 110 |
| Vitals | 12345 | 4/7/2020 | Alabama | Diastole | NULL |
| Vitals | 12345 | 4/7/2020 | Alabama | BMI | 23 |
| Vitals | 12345 | 4/7/2020 | Alabama | Diastole | NULL |
| Vitals | 12345 | 4/7/2020 | Alabama | Systole | 106 |
| Vitals | 12345 | 9/29/2020 | Alabama | BMI | 22 |
| Vitals | 12345 | 9/29/2020 | Alabama | Blood Pressure | 120/82 |
| Vitals | 12345 | 2/2/2021 | Alabama | BMI | 24 |
| Vitals | 12345 | 2/2/2021 | Alabama | Blood Pressure | 133/68 |
免责声明 - 这只是属于 ID 12345 的数据子集,还有数千个。
我想要一个使用 sql server where 返回结果所需图片的查询:
- 基于 ID、日期和状态 - 在结果列中以“systole”/“diastole”的格式合并 systole 和 diastole 的 MAX
- 合并后的名称列归类为血压
- 有时舒张期或收缩期缺失或无效。如果是这样,那就离开吧。 (在结果期望的图片中 - 请参阅日期 2019 年 11 月 5 日和 2020 年 4 月 7 日以进行澄清
我使用了this link as a reference 来提供帮助,但我做不到。不确定如何在结果中包含上述条件。
With test as
(
select service,ID, Date, state, Name, results, case when name = 'Systole' or t.BiometricName= 'Diastole'then 'Blood Pressure' else Name end [New_Name]
from mytable t )
SELECT DIStiNCT service, ID, Date, state, Name, results, new_name
FROM test
对不起,这个降价不太好(我会尝试编辑这个),我希望我不会把它弄得太臃肿。
使用 SQL Server 2014 - 12.0.5223.6
提前致谢
【问题讨论】:
-
图片如果数据不能帮助我们帮助你。花时间以 consumable 格式发布示例数据;更可取的 DDL 和 DML 语句。至于这个问题,到目前为止,你尝试过什么来解决这个问题?为什么它不起作用?
-
@Larnu 你好。感谢您的回复和礼仪信息 - 我已经发布了一个消耗品格式。我是 sql 的零星用户,老实说不太确定从哪里开始。我使用“With”函数来获取我想要的数据子集,但没有什么值得一提的。
-
你的 SQL server 版本是多少?
-
@ArunPalanisamy 嘿-在我的 SSMS 的 about 部分中显示我正在使用版本。 15.0.18369.0
-
这是 SSMS 版本。在查询窗口中执行
SELECT @@Version以获取您连接到的实例的实际 SQL Server 版本。你试过什么,你坚持什么?向我们展示您的代码,以便我们为您提供帮助。
标签: sql sql-server merge concatenation