这是一个“为组选择前n个”问题,除了我有4个表并且需要几个字段的MAX。
SELECT DISTINCT r.Id, p.Description AS Pathway, CONVERT(Varchar(10), s.CreateDate, 101)
AS [Date], s.OptionId, o.Description AS Options, s.Score, o.Risk
FROM ucare.Responses r
INNER JOIN ucare.RiskScores s ON s.ScheduleId = r.ScheduleId
INNER JOIN ucare.PathwayOptions o ON o.Id = s.OptionId
INNER JOIN ucare.Pathways p ON p.Id = r.PathwayId
WHERE r.PathwayId IN
(SELECT DISTINCT PathwayId
FROM ucare.Responses
WHERE r.ScheduleId = '541')
AND Score IN
(SELECT DISTINCT MAX(Score) OVER (PARTITION BY s.OptionId)
FROM ucare.RiskScores WHERE ScheduleId = '541')
AND o.Risk IN
(SELECT DISTINCT MAX(Risk) OVER (PARTITION BY o.Id) AS Risk
FROM ucare.PathwayOptions)
GROUP BY r.Id, s.OptionId, p.Description, s.Score, s.CreateDate, o.Description, o.Risk
结果:
**Id Pathway Date OptionId Options Score Risk**
1 Test pathway 1 6/19/2017 4 Test Option 1 160 10
10 Test pathway 2 6/19/2017 4 Test Option 1 160 10
17 Test pathway 3 6/19/2017 4 Test Option 1 160 10
我需要的是:
**Id Pathway Date OptionId Options Score Risk**
1 Test pathway 1 6/19/2017 4 Test Option 1 160 10
10 Test pathway 2 6/19/2017 34 Test Option 2 62 10
17 Test pathway 3 6/19/2017 49 Test Option 3 50 2
当Id更改时,OptionId,Option(o.Description),Score和Risk会发生变化。 任何帮助将不胜感激,谢谢,
杰克
就叙述而言,这总结了我正在寻找的东西。首先,我们获取与特定ScheduleId相关的所有响应(ucare.Responses),在本例中为541.这将为我们提供1,10和17的DISTINCT PathwayId,它们链接到ucare.Pathways的查找表,用于路径描述。
从这里,541的ScheduleId链接ucare.RiskScores,它为我们提供了PathwayOptionsId,CreateDate和Scores。 PathwayOptionId用于链接到ucare.PathwayOptions表中的Id字段,在该表中我们可以获得匹配的PathwayOption描述,最新的(MAX(CreateDate))和匹配的MAX(Risk)。
MAX风险用于打破RiskScore存在的任何关系。例如,如果我们查询带有541的ScheduleId的ucare.RiskScores,则有4个匹配的PathwayOptionId,1,2,4和5,得分为160,但风险为8,7,10和9.选择#4有MAX得分为160,MAX风险为10,这是我们的选择。 这将完成ScheduleId 541的链接链。
这是新的CREATE / INSERT脚本,其中包含要匹配的所有字段和相应的数据。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [ucare].[PathwayOptions](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](max) NOT NULL,
[Description] [nvarchar](max) NOT NULL,
[Risk] [int] NOT NULL,
[PathwayId] [int] NOT NULL,
[Range] [varchar](50) NULL,
CONSTRAINT [PK_ucare.PathwayOptions] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [ucare].[PathwayOptions] ON
INSERT [ucare].[PathwayOptions] ([Id], [Title], [Description], [Risk], [PathwayId], [Range]) VALUES
(1, N'COPD B', N'GOLD Risk Level B - Low Risk, High Symptom', 8, 1, NULL),
(2, N'COPD A', N'GOLD Risk Level A - Low Risk, Low Symptom', 7, 1, NULL),
(4, N'COPD D', N'GOLD Risk Level D - High Risk, High Symptom', 10, 1, NULL),
(5, N'COPD C', N'GOLD Risk Level C - High Risk, Low Symptom', 9, 1, NULL),
(6, N'TKA PT High Fxn', N'High FxnTKA', 7, 2, NULL),
(7, N'TKA PT', N'TKA High Function', 7, 2, NULL),
(9, N'TKA PT', N'TKA Med', 9, 2, NULL),
(12, N'Fall Risk - High', N'Rehab Fall Risk - High', 10, 5, NULL),
(13, N'Fall Risk - Medium', N'Rehab Fall Risk - Medium', 9, 5, NULL),
(14, N'Fall Risk - Low', N'Rehab Fall Risk - Low', 8, 5, NULL),
(15, N'High Fall Risk', N'High Risk of Falling', 10, 4, NULL),
(16, N'Low Fall Risk', N'Low Risk of Falling', 8, 4, NULL),
(17, N'Primary Prevention 2', N'Primary Prevention Level 2', 6, 7, NULL),
(18, N'Primary Prevention 1', N'Primary Prevention Level 1', 5, 7, NULL),
(21, N'Stable/Improving', N'No significant changes', 7, 8, NULL),
(22, N'Mixed Change', N'Mixed results from assessment', 8, 8, NULL),
(23, N'Significant Decline', N'Observation shows marked decline in condition', 9, 8, NULL),
(24, N'High Alert', N'Results require an immediate alert', 10, 8, NULL),
(25, N'Secondary Prevention 1', N'Secondary Prevention Level 1', 7, 7, NULL),
(26, N'Secondary Prevention 2', N'Secondary Prevention Level 2', 8, 7, NULL),
(27, N'Tertiary Prevention', N'Tertiary Prevention', 9, 7, NULL),
(28, N'Catastrophic Care', N'Catastrophic Care', 10, 7, NULL),
(29, N'Grade 0', N'Grade 0', 0, 9, NULL),
(30, N'Grade 1', N'Grade 1', 1, 9, NULL),
(31, N'Grade 2', N'Grade 2', 2, 9, NULL),
(32, N'Grade 3', N'Grade 3', 3, 9, NULL),
(33, N'Grade 4', N'Grade 4', 4, 9, NULL),
(34, N'High Risk', N'High Risk of OSA', 10, 10, N'5-8'),
(35, N'Low Risk', N'Low Risk of OSA', 8, 10, N'0-2'),
(36, N'Low Fall Risk', N'Low Risk of Falling', 9, 6, NULL),
(37, N'High Fall Risk', N'High Risk of Falling', 10, 6, NULL),
(38, N'Current Placement Appropriate', N'The current care setting is appropriate', 8, 11, NULL),
(39, N'Consider New Placement', N'Higher level care may be necessary', 9, 11, NULL),
(40, N'New Placement Needed', N'Higher level of care is strongly recommended', 10, 11, NULL),
(41, N'Driving is Appropriate', N'No significant signs of driving danger', 7, 12, NULL),
(42, N'Monitor Driving Safety', N'Continue to Monitor the Driving Safety', 8, 12, NULL),
(43, N'Driving Safety is a Concern', N'Consider restricting or removing from driving', 9, 12, NULL),
(44, N'Driving is Not Appropriate', N'Driving should not be allowed', 10, 12, NULL),
(45, N'No current needs', N'No home service needs', 9, 13, NULL),
(46, N'Some Home Service Needs', N'Home service needed', 10, 13, NULL),
(47, N'Hi Risk', N'High Fall Risk', 10, 16, NULL),
(48, N'Lo Risk ', N'Low Fall Risk', 9, 16, NULL),
(49, N'Low Risk', N'Low risk range', 0, 17, N'1-2'),
(50, N'Medium Risk', N'Medium risk option', 1, 17, N'6-10'),
(51, N'High Risk', N'High risk option', 2, 17, N'11-15'),
(52, N'Intermediate Risk', N'Intermediate Rsk of OSA', 9, 10, N'3-4'),
(53, N'Lo Fall', N'Low Fall Risk', 9, 18, NULL),
(54, N'Very High', N'Very High Impact', 10, 25, N'31-50'),
(55, N'High', N'High Impact Level', 9, 25, N'21-30'),
(56, N'Medium', N'Medium Impact Level', 8, 25, N'10-20'),
(57, N'Low', N'Low Impact Level', 7, 25, N'6-9'),
(58, N'None', N'No Impact', 6, 25, N'0-5')
SET IDENTITY_INSERT [ucare].[PathwayOptions] OFF
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [ucare].[Responses](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Text] [nvarchar](max) NULL,
[PathwayOptionId] [int] NOT NULL,
[DecisionInputId] [int] NOT NULL,
[ScheduleId] [int] NOT NULL,
[EntryDate] [datetime] NULL,
[PathwayId] [int] NOT NULL,
[EncounterId] [int] NOT NULL,
[EpisodeId] [int] NOT NULL,
CONSTRAINT [PK_ucare.Responses] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [ucare].[Responses] ON
INSERT [ucare].[Responses] ([Id], [Text], [PathwayOptionId], [DecisionInputId], [ScheduleId], [EntryDate], [PathwayId], [EncounterId], [EpisodeId]) VALUES
(1179, N'True', 0, 17, 543, CAST(0x0000A7940067AC73 AS DateTime), 17, 543, 1),
(1180, N'True', 0, 130, 543, CAST(0x0000A7940067AC73 AS DateTime), 17, 543,1),
(1181, N'True', 0, 16, 543, CAST(0x0000A7940067AC73 AS DateTime), 17, 543, 1),
(1182, N'True', 0, 76, 543, CAST(0x0000A7940067AC73 AS DateTime), 17, 543, 1),
(1183, N'34', 0, 38, 543, CAST(0x0000A79400578978 AS DateTime), 17, 543, 1),
(1184, N'True', 0, 82, 543, CAST(0x0000A79400578978 AS DateTime), 17, 543, 1),
(1185, N'True', 0, 17, 543, CAST(0x0000A79400578978 AS DateTime), 17, 543, 1),
(1186, N'True', 0, 130, 543, CAST(0x0000A79400578978 AS DateTime), 17, 543, 1),
(1187, N'True', 0, 16, 543, CAST(0x0000A79400578978 AS DateTime), 17, 543, 1),
(1188, N'True', 0, 76, 543, CAST(0x0000A79400578978 AS DateTime), 17, 543, 1),
(1189, N'34', 0, 38, 543, CAST(0x0000A79400579A40 AS DateTime), 17, 543, 1),
(1190, N'True', 0, 82, 543, CAST(0x0000A79400579A40 AS DateTime), 17, 543, 1),
(1191, N'True', 0, 17, 543, CAST(0x0000A79400579A40 AS DateTime), 17, 543, 1),
(1192, N'False', 0, 130, 543, CAST(0x0000A79400579A40 AS DateTime), 17, 543, 1),
(1193, N'False', 0, 16, 543, CAST(0x0000A79400579A40 AS DateTime), 17, 543, 1),
(1194, N'False', 0, 76, 543, CAST(0x0000A79400579A40 AS DateTime), 17, 543, 1),
(1195, N'2', 0, 66, 541, CAST(0x0000A7970091A833 AS DateTime), 1, 541, 1),
(1196, N'2', 0, 67, 541, CAST(0x0000A7970091A833 AS DateTime), 1, 541, 1),
(1197, N'3', 0, 68, 541, CAST(0x0000A7970091A833 AS DateTime), 1, 541, 1),
(1198, N'False', 0, 65, 541, CAST(0x0000A7970091A833 AS DateTime), 1, 541, 1),
(1199, N'12', 0, 39, 541, CAST(0x0000A7970091A833 AS DateTime), 1, 541, 1),
(1200, N'1', 0, 66, 541, CAST(0x0000A797009281A8 AS DateTime), 1, 541, 1),
(1201, N'1', 0, 67, 541, CAST(0x0000A797009281A8 AS DateTime), 1, 541, 1),
(1202, N'1', 0, 68, 541, CAST(0x0000A797009281A8 AS DateTime), 1, 541, 1),
(1203, N'False', 0, 65, 541, CAST(0x0000A797009281A8 AS DateTime), 1, 541, 1),
(1204, N'9', 0, 39, 541, CAST(0x0000A797009281A8 AS DateTime), 1, 541, 1),
(1205, N'', 0, 38, 541, CAST(0x0000A7970095C348 AS DateTime), 17, 541, 1),
(1206, N'True', 0, 82, 541, CAST(0x0000A7970095C348 AS DateTime), 17, 541, 1),
(1207, N'True', 0, 17, 541, CAST(0x0000A7970095C348 AS DateTime), 17, 541, 1),
(1208, N'False', 0, 130, 541, CAST(0x0000A7970095C348 AS DateTime), 17, 541, 1),
(1209, N'False', 0, 16, 541, CAST(0x0000A7970095C348 AS DateTime), 17, 541, 1),
(1210, N'True', 0, 76, 541, CAST(0x0000A7970095C348 AS DateTime), 17, 541, 1),
(1211, N'True', 0, 92, 541, CAST(0x0000A79700973842 AS DateTime), 10, 541, 1),
(1212, N'False', 0, 93, 541, CAST(0x0000A79700973842 AS DateTime), 10, 541, 1),
(1213, N'False', 0, 94, 541, CAST(0x0000A79700973842 AS DateTime), 10, 541, 1),
(1214, N'True', 0, 95, 541, CAST(0x0000A79700973842 AS DateTime), 10, 541, 1),
(1215, N'True', 0, 96, 541, CAST(0x0000A79700973842 AS DateTime), 10, 541, 1),
(1216, NULL, 0, 97, 541, CAST(0x0000A79700973842 AS DateTime), 10, 541, 1),
(1217, N'True', 0, 98, 541, CAST(0x0000A79700973842 AS DateTime), 10, 541, 1),
(1218, N'True', 0, 99, 541, CAST(0x0000A79700973842 AS DateTime), 10, 541, 1),
(1219, N'True', 0, 92, 541, CAST(0x0000A79700974DEC AS DateTime), 10, 541, 1),
(1220, N'False', 0, 93, 541, CAST(0x0000A79700974DEC AS DateTime), 10, 541, 1),
(1221, N'False', 0, 94, 541, CAST(0x0000A79700974DEC AS DateTime), 10, 541, 1),
(1222, N'True', 0, 95, 541, CAST(0x0000A79700974DEC AS DateTime), 10, 541, 1),
(1223, N'False', 0, 96, 541, CAST(0x0000A79700974DEC AS DateTime), 10, 541, 1),
(1224, NULL, 0, 97, 541, CAST(0x0000A79700974DEC AS DateTime), 10, 541, 1),
(1225, N'True', 0, 98, 541, CAST(0x0000A79700974DEC AS DateTime), 10, 541, 1),
(1226, N'True', 0, 99, 541, CAST(0x0000A79700974DEC AS DateTime), 10, 541, 1),
(1227, N'False', 0, 92, 541, CAST(0x0000A797009756C0 AS DateTime), 10, 541, 1),
(1228, N'False', 0, 93, 541, CAST(0x0000A797009756C0 AS DateTime), 10, 541, 1),
(1229, N'False', 0, 94, 541, CAST(0x0000A797009756C0 AS DateTime), 10, 541, 1),
(1230, N'False', 0, 95, 541, CAST(0x0000A797009756C0 AS DateTime), 10, 541, 1),
(1231, N'False', 0, 96, 541, CAST(0x0000A797009756C0 AS DateTime), 10, 541, 1),
(1232, NULL, 0, 97, 541, CAST(0x0000A797009756C0 AS DateTime), 10, 541, 1),
(1233, N'True', 0, 98, 541, CAST(0x0000A797009756C0 AS DateTime), 10, 541, 1),
(1234, N'True', 0, 99, 541, CAST(0x0000A797009756C0 AS DateTime), 10, 541, 1),
(1235, N'True', 0, 92, 540, CAST(0x0000A79800A381F1 AS DateTime), 10, 540, 1),
(1236, N'False', 0, 93, 540, CAST(0x0000A79800A381F1 AS DateTime), 10, 540, 1),
(1237, N'False', 0, 94, 540, CAST(0x0000A79800A381F1 AS DateTime), 10, 540, 1),
(1238, N'True', 0, 95, 540, CAST(0x0000A79800A381F1 AS DateTime), 10, 540, 1),
(1239, N'True', 0, 96, 540, CAST(0x0000A79800A381F1 AS DateTime), 10, 540, 1),
(1240, NULL, 0, 97, 540, CAST(0x0000A79800A381F1 AS DateTime), 10, 540, 1),
(1241, NULL, 0, 98, 540, CAST(0x0000A79800A381F1 AS DateTime), 10, 540, 1),
(1242, NULL, 0, 99, 540, CAST(0x0000A79800A381F1 AS DateTime), 10, 540, 1),
(1243, N'False', 0, 92, 540, CAST(0x0000A79800AE690E AS DateTime), 10, 540, 1),
(1244, N'True', 0, 93, 540, CAST(0x0000A79800AE690E AS DateTime), 10, 540, 1),
(1245, N'False', 0, 94, 540, CAST(0x0000A79800AE690E AS DateTime), 10, 540, 1),
(1246, N'True', 0, 95, 540, CAST(0x0000A79800AE690E AS DateTime), 10, 540, 1),
(1247, NULL, 0, 96, 540, CAST(0x0000A79800AE690E AS DateTime), 10, 540, 1),
(1248, N'False', 0, 97, 540, CAST(0x0000A79800AE690E AS DateTime), 10, 540, 1),
(1249, N'True', 0, 98, 540, CAST(0x0000A79800AE690E AS DateTime), 10, 540, 1),
(1250, NULL, 0, 99, 540, CAST(0x0000A79800AE690E AS DateTime), 10, 540, 1),
(1251, N'False', 0, 92, 540, CAST(0x0000A79800AFFEE6 AS DateTime), 10, 540, 1),
(1252, N'True', 0, 93, 540, CAST(0x0000A79800AFFEE6 AS DateTime), 10, 540, 1),
(1253, N'False', 0, 94, 540, CAST(0x0000A79800AFFEE6 AS DateTime), 10, 540, 1),
(1254, N'True', 0, 95, 540, CAST(0x0000A79800AFFEE6 AS DateTime), 10, 540, 1),
(1255, NULL, 0, 96, 540, CAST(0x0000A79800AFFEE6 AS DateTime), 10, 540, 1),
(1256, N'True', 0, 97, 540, CAST(0x0000A79800AFFEE6 AS DateTime), 10, 540, 1),
(1257, N'True', 0, 98, 540, CAST(0x0000A79800AFFEE6 AS DateTime), 10, 540, 1),
(1258, N'True', 0, 99, 540, CAST(0x0000A79800AFFEE6 AS DateTime), 10, 540, 1),
(1259, N'2', 0, 66, 540, CAST(0x0000A79800B0493B AS DateTime), 1, 540, 1),
(1260, N'3', 0, 67, 540, CAST(0x0000A79800B0493B AS DateTime), 1, 540, 1),
(1261, N'3', 0, 68, 540, CAST(0x0000A79800B0493B AS DateTime), 1, 540, 1),
(1262, N'True', 0, 65, 540, CAST(0x0000A79800B0493B AS DateTime), 1, 540, 1),
(1263, N'12', 0, 39, 540, CAST(0x0000A79800B0493B AS DateTime), 1, 540, 1),
(1264, N'0', 0, 66, 540, CAST(0x0000A79800B087C5 AS DateTime), 1, 540, 1),
(1265, N'3', 0, 67, 540, CAST(0x0000A79800B087C5 AS DateTime), 1, 540, 1),
(1266, N'4', 0, 68, 540, CAST(0x0000A79800B087C5 AS DateTime), 1, 540, 1),
(1267, N'True', 0, 65, 540, CAST(0x0000A79800B087C5 AS DateTime), 1, 540, 1),
(1268, N'12', 0, 39, 540, CAST(0x0000A79800B087C5 AS DateTime), 1, 540, 1),
(1269, N'23', 0, 38, 356, CAST(0x0000A79900D5FDCC AS DateTime), 17, 356, 1),
(1270, N'True', 0, 82, 356, CAST(0x0000A79900D5FDCC AS DateTime), 17, 356, 1),
(1271, N'False', 0, 17, 356, CAST(0x0000A79900D5FDCC AS DateTime), 17, 356, 1),
(1272, N'True', 0, 130, 356, CAST(0x0000A79900D5FDCC AS DateTime), 17, 356, 1),
(1273, N'True', 0, 16, 356, CAST(0x0000A79900D5FDCC AS DateTime), 17, 356, 1),
(1274, N'True', 0, 76, 356, CAST(0x0000A79900D5FDCC AS DateTime), 17, 356, 1),
(1275, N'1', 0, 66, 356, CAST(0x0000A79A0098DF24 AS DateTime), 1, 356, 1),
(1276, N'1', 0, 67, 356, CAST(0x0000A79A0098DF24 AS DateTime), 1, 356, 1),
(1277, N'1', 0, 68, 356, CAST(0x0000A79A0098DF24 AS DateTime), 1, 356, 1),
(1278, N'False', 0, 65, 356, CAST(0x0000A79A0098DF24 AS DateTime), 1, 356, 1),
(1279, N'9', 0, 39, 356, CAST(0x0000A79A0098DF24 AS DateTime), 1, 356, 1)
SET IDENTITY_INSERT [ucare].[Responses] OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [ucare].[RiskScores](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ScheduleId] [int] NOT NULL,
[PathwayOptionId] [int] NOT NULL,
[Score] [int] NOT NULL,
[CreateDate] [datetime] NULL,
[CreatedBy] [varchar](200) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [ucare].[RiskScores] ON
INSERT [ucare].[RiskScores] ([Id], [ScheduleId], [PathwayOptionId], [Score], [CreateDate], [CreatedBy]) VALUES
(1, 1, 14, 0, CAST(0x0000A77500E986A9 AS DateTime), N'Admin'),
(2, 1, 14, 0, CAST(0x0000A77500E9CD16 AS DateTime), N'Admin'),
(3, 1, 14, 0, CAST(0x0000A77500EA13CB AS DateTime), N'Admin'),
(4, 1, 14, 0, CAST(0x0000A77600A7A764 AS DateTime), N'Admin'),
(5, 1, 14, 0, CAST(0x0000A77600A7B604 AS DateTime), N'Admin'),
(6, 1, 14, 0, CAST(0x0000A77600A83653 AS DateTime), N'Admin'),
(7, 1, 14, 0, CAST(0x0000A77600A9486B AS DateTime), N'Admin'),
(8, 1, 14, 0, CAST(0x0000A77600A981A9 AS DateTime), N'Admin'),
(9, 1, 14, 0, CAST(0x0000A77600AB4821 AS DateTime), N'Admin'),
(10, 2, 14, 0, CAST(0x0000A77600AC0883 AS DateTime), N'Admin'),
(11, 2, 14, 0, CAST(0x0000A77600B25DF2 AS DateTime), N'Admin'),
(12, 2, 14, 0, CAST(0x0000A77600B2F07C AS DateTime), N'Admin'),
(13, 2, 14, 0, CAST(0x0000A77600DEDD85 AS DateTime), N'Admin'),
(14, 2, 14, 0, CAST(0x0000A77600DF5E89 AS DateTime), N'Admin'),
(15, 2, 14, 0, CAST(0x0000A77600DF8575 AS DateTime), N'Admin'),
(16, 2, 1, 0, CAST(0x0000A77B00B0F7C4 AS DateTime), N'Admin'),
(17, 2, 2, 0, CAST(0x0000A77B00B0F7C4 AS DateTime), N'Admin'),
(18, 2, 4, 20, CAST(0x0000A77B00B0F7C4 AS DateTime), N'Admin'),
(19, 2, 5, 20, CAST(0x0000A77B00B0F7C4 AS DateTime), N'Admin'),
(20, 2, 1, 60, CAST(0x0000A77B00B150AE AS DateTime), N'Admin'),
(21, 2, 2, 60, CAST(0x0000A77B00B150AE AS DateTime), N'Admin'),
(22, 2, 4, 40, CAST(0x0000A77B00B150AE AS DateTime), N'Admin'),
(23, 2, 5, 40, CAST(0x0000A77B00B150AE AS DateTime), N'Admin'),
(24, 2, 1, 60, CAST(0x0000A77B00BD3FDA AS DateTime), N'Admin'),
(25, 2, 2, 60, CAST(0x0000A77B00BD3FDA AS DateTime), N'Admin'),
(26, 2, 4, 40, CAST(0x0000A77B00BD3FDA AS DateTime), N'Admin'),
(27, 2, 5, 40, CAST(0x0000A77B00BD3FDA AS DateTime), N'Admin'),
(28, 2, 15, 14, CAST(0x0000A77B00BD832E AS DateTime), N'Admin'),
(29, 2, 16, 14, CAST(0x0000A77B00BD832E AS DateTime), N'Admin'),
(1002, 2, 14, 0, CAST(0x0000A77C0070AE72 AS DateTime), N'Admin'),
(1003, 2, 1, 60, CAST(0x0000A77C0070C1E6 AS DateTime), N'Admin'),
(1004, 2, 2, 60, CAST(0x0000A77C0070C1E6 AS DateTime), N'Admin'),
(1005, 2, 4, 40, CAST(0x0000A77C0070C1E6 AS DateTime), N'Admin'),
(1006, 2, 5, 40, CAST(0x0000A77C0070C1E6 AS DateTime), N'Admin'),
(1007, 2, 49, 16, CAST(0x0000A79100E87601 AS DateTime), N'Admin'),
(1008, 2, 50, 16, CAST(0x0000A79100E87601 AS DateTime), N'Admin'),
(1009, 2, 51, 16, CAST(0x0000A79100E87601 AS DateTime), N'Admin'),
(1016, 2, 49, 50, CAST(0x0000A79100FADB8A AS DateTime), N'Admin'),
(1017, 2, 50, 100, CAST(0x0000A79100FD7090 AS DateTime), N'Admin'),
(1018, 2, 49, 16, CAST(0x0000A79100FDB842 AS DateTime), N'Admin'),
(1025, 543, 50, 100, CAST(0x0000A794006340B9 AS DateTime), N'Admin'),
(1026, 543, 50, 100, CAST(0x0000A79400639285 AS DateTime), N'Admin'),
(1027, 543, 50, 100, CAST(0x0000A7940063D4EE AS DateTime), N'Admin'),
(1028, 543, 50, 100, CAST(0x0000A7940063E528 AS DateTime), N'Admin'),
(1029, 543, 50, 100, CAST(0x0000A79400644580 AS DateTime), N'Admin'),
(1030, 543, 50, 100, CAST(0x0000A79400649B97 AS DateTime), N'Admin'),
(1031, 543, 50, 100, CAST(0x0000A79400663C8F AS DateTime), N'Admin'),
(1033, 543, 50, 100, CAST(0x0000A79400578978 AS DateTime), N'Admin'),
(1034, 543, 49, 50, CAST(0x0000A79400579A40 AS DateTime), N'Admin'),
(1035, 541, 1, 160, CAST(0x0000A7970091A833 AS DateTime), N'Admin'),
(1036, 541, 2, 0, CAST(0x0000A7970091A833 AS DateTime), N'Admin'),
(1037, 541, 4, 160, CAST(0x0000A7970091A833 AS DateTime), N'Admin'),
(1038, 541, 5, 0, CAST(0x0000A7970091A833 AS DateTime), N'Admin'),
(1039, 541, 1, 0, CAST(0x0000A797009281A8 AS DateTime), N'Admin'),
(1040, 541, 2, 160, CAST(0x0000A797009281A8 AS DateTime), N'Admin'),
(1041, 541, 4, 0, CAST(0x0000A797009281A8 AS DateTime), N'Admin'),
(1042, 541, 5, 160, CAST(0x0000A797009281A8 AS DateTime), N'Admin'),
(1043, 541, 49, 50, CAST(0x0000A7970095C348 AS DateTime), N'Admin'),
(1044, 541, 34, 62, CAST(0x0000A79700973842 AS DateTime), N'Admin'),
(1045, 541, 52, 50, CAST(0x0000A79700974DEC AS DateTime), N'Admin'),
(1046, 541, 35, 25, CAST(0x0000A797009756C0 AS DateTime), N'Admin'),
(1049, 540, 34, 62, CAST(0x0000A79800AFFEE6 AS DateTime), N'Admin'),
(1050, 540, 1, 160, CAST(0x0000A79800B0493B AS DateTime), N'Admin'),
(1051, 540, 2, 0, CAST(0x0000A79800B0493B AS DateTime), N'Admin'),
(1052, 540, 4, 160, CAST(0x0000A79800B0493B AS DateTime), N'Admin'),
(1053, 540, 5, 0, CAST(0x0000A79800B0493B AS DateTime), N'Admin'),
(1054, 540, 1, 160, CAST(0x0000A79800B087C5 AS DateTime), N'Admin')
SET IDENTITY_INSERT [ucare].[RiskScores] OFF
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [ucare].[Pathways](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](max) NOT NULL,
[Description] [nvarchar](max) NOT NULL,
[ProviderType] [nvarchar](max) NULL,
[PatientPathway_Id] [int] NULL,
[CalculationType] [varchar](50) NOT NULL,
[Instructions] [varchar](400) NULL,
[RecordStatus] [char](1) NOT NULL,
CONSTRAINT [PK_ucare.Pathways] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [ucare].[Pathways] ON
INSERT [ucare].[Pathways] ([Id], [Title], [Description]) VALUES
(1, N'COPD GOLD Risk Classification', N'COPD GOLD Classification'),
(2, N'TKA PT', N'Physical Therapy TKA'),
(4, N'Fall Assess - RN', N'Medical Fall Risk Screen'),
(5, N'Fall Assess - Rehab', N'Rehab Specialist Fall Assessment'),
(6, N'Fall Assess - Caregiver', N'Caregiver Fall Assessment'),
(7, N'Global Risk', N'Global Risk Stratification'),
(8, N'COPD Assess - RT', N'COPD Assessment - Respiratory Therapist'),
(9, N'MMRC Test', N'MMRC Dyspnea Test'),
(10, N'STOP BANG ', N'Stop Bang Assessment'),
(11, N'Care Placement Needs Assessment', N'Assessment for Patient Placement'),
(12, N'Driving Assessment', N'Driving Ability Assessemnt'),
(13, N'Home Care Needs Assessment', N'Home Care Needs Assessment'),
(14, N'Home Services Needs Assessment', N'Assessment to the specific needs for home services'),
(15, N'DbCustomerTest', N'Testing-1'),
(16, N'FRAT Assessment', N'Fall Risk Assessment'),
(17, N'A - Test Count Pathway', N'Test pathway for updated count scoring algorithm'),
(18, N'FRAT Assessment', N'Fall Risk Assessment'),
(21, N'AAA Test', N'Test'),
(25, N'CAT', N'COPD Assessment Test (CAT)')
SET IDENTITY_INSERT [ucare].[Pathways] OFF
答案 0 :(得分:0)
看看这是否是你正在寻找的......(对于那些想要在没有向他们的测试环境添加新模式和真实表格的情况下玩的人,也将表更改为#TempTables)
编辑:已根据OP的新数据添加了新的解决方案。
IF OBJECT_ID('tempdb..#PathwayOptions', 'U') IS NOT NULL
DROP TABLE #PathwayOptions;
GO
CREATE TABLE #PathwayOptions(
Id int NOT NULL PRIMARY KEY CLUSTERED ,
Title nvarchar(max) NOT NULL,
Description nvarchar(max) NOT NULL,
Risk int NOT NULL,
PathwayId int NOT NULL,
Range varchar(50) NULL
);
GO
INSERT #PathwayOptions (Id, Title, Description, Risk, PathwayId, Range) VALUES
(1, N'COPD B', N'GOLD Risk Level B - Low Risk, High Symptom', 8, 1, NULL),
(2, N'COPD A', N'GOLD Risk Level A - Low Risk, Low Symptom', 7, 1, NULL),
(4, N'COPD D', N'GOLD Risk Level D - High Risk, High Symptom', 10, 1, NULL),
(5, N'COPD C', N'GOLD Risk Level C - High Risk, Low Symptom', 9, 1, NULL),
(6, N'TKA PT High Fxn', N'High FxnTKA', 7, 2, NULL),
(7, N'TKA PT', N'TKA High Function', 7, 2, NULL),
(9, N'TKA PT', N'TKA Med', 9, 2, NULL),
(12, N'Fall Risk - High', N'Rehab Fall Risk - High', 10, 5, NULL),
(13, N'Fall Risk - Medium', N'Rehab Fall Risk - Medium', 9, 5, NULL),
(14, N'Fall Risk - Low', N'Rehab Fall Risk - Low', 8, 5, NULL),
(15, N'High Fall Risk', N'High Risk of Falling', 10, 4, NULL),
(16, N'Low Fall Risk', N'Low Risk of Falling', 8, 4, NULL),
(17, N'Primary Prevention 2', N'Primary Prevention Level 2', 6, 7, NULL),
(18, N'Primary Prevention 1', N'Primary Prevention Level 1', 5, 7, NULL),
(21, N'Stable/Improving', N'No significant changes', 7, 8, NULL),
(22, N'Mixed Change', N'Mixed results from assessment', 8, 8, NULL),
(23, N'Significant Decline', N'Observation shows marked decline in condition', 9, 8, NULL),
(24, N'High Alert', N'Results require an immediate alert', 10, 8, NULL),
(25, N'Secondary Prevention 1', N'Secondary Prevention Level 1', 7, 7, NULL),
(26, N'Secondary Prevention 2', N'Secondary Prevention Level 2', 8, 7, NULL),
(27, N'Tertiary Prevention', N'Tertiary Prevention', 9, 7, NULL),
(28, N'Catastrophic Care', N'Catastrophic Care', 10, 7, NULL),
(29, N'Grade 0', N'Grade 0', 0, 9, NULL),
(30, N'Grade 1', N'Grade 1', 1, 9, NULL),
(31, N'Grade 2', N'Grade 2', 2, 9, NULL),
(32, N'Grade 3', N'Grade 3', 3, 9, NULL),
(33, N'Grade 4', N'Grade 4', 4, 9, NULL),
(34, N'High Risk', N'High Risk of OSA', 10, 10, N'5-8'),
(35, N'Low Risk', N'Low Risk of OSA', 8, 10, N'0-2'),
(36, N'Low Fall Risk', N'Low Risk of Falling', 9, 6, NULL),
(37, N'High Fall Risk', N'High Risk of Falling', 10, 6, NULL),
(38, N'Current Placement Appropriate', N'The current care setting is appropriate', 8, 11, NULL),
(39, N'Consider New Placement', N'Higher level care may be necessary', 9, 11, NULL),
(40, N'New Placement Needed', N'Higher level of care is strongly recommended', 10, 11, NULL),
(41, N'Driving is Appropriate', N'No significant signs of driving danger', 7, 12, NULL),
(42, N'Monitor Driving Safety', N'Continue to Monitor the Driving Safety', 8, 12, NULL),
(43, N'Driving Safety is a Concern', N'Consider restricting or removing from driving', 9, 12, NULL),
(44, N'Driving is Not Appropriate', N'Driving should not be allowed', 10, 12, NULL),
(45, N'No current needs', N'No home service needs', 9, 13, NULL),
(46, N'Some Home Service Needs', N'Home service needed', 10, 13, NULL),
(47, N'Hi Risk', N'High Fall Risk', 10, 16, NULL),
(48, N'Lo Risk ', N'Low Fall Risk', 9, 16, NULL),
(49, N'Low Risk', N'Low risk range', 0, 17, N'1-2'),
(50, N'Medium Risk', N'Medium risk option', 1, 17, N'6-10'),
(51, N'High Risk', N'High risk option', 2, 17, N'11-15'),
(52, N'Intermediate Risk', N'Intermediate Rsk of OSA', 9, 10, N'3-4'),
(53, N'Lo Fall', N'Low Fall Risk', 9, 18, NULL),
(54, N'Very High', N'Very High Impact', 10, 25, N'31-50'),
(55, N'High', N'High Impact Level', 9, 25, N'21-30'),
(56, N'Medium', N'Medium Impact Level', 8, 25, N'10-20'),
(57, N'Low', N'Low Impact Level', 7, 25, N'6-9'),
(58, N'None', N'No Impact', 6, 25, N'0-5');
GO
IF OBJECT_ID('tempdb..#RiskScores', 'U') IS NOT NULL
DROP TABLE #RiskScores;
go
CREATE TABLE #RiskScores(
Id int NOT NULL PRIMARY KEY CLUSTERED,
ScheduleId int NOT NULL,
PathwayOptionId int NOT NULL,
Score int NOT NULL,
CreateDate datetime NULL,
CreatedBy varchar(200) NULL
);
GO
INSERT #RiskScores (Id, ScheduleId, PathwayOptionId, Score, CreateDate, CreatedBy) VALUES
(1, 1, 14, 0, CAST(0x0000A77500E986A9 AS DateTime), N'Admin'),
(2, 1, 14, 0, CAST(0x0000A77500E9CD16 AS DateTime), N'Admin'),
(3, 1, 14, 0, CAST(0x0000A77500EA13CB AS DateTime), N'Admin'),
(4, 1, 14, 0, CAST(0x0000A77600A7A764 AS DateTime), N'Admin'),
(5, 1, 14, 0, CAST(0x0000A77600A7B604 AS DateTime), N'Admin'),
(6, 1, 14, 0, CAST(0x0000A77600A83653 AS DateTime), N'Admin'),
(7, 1, 14, 0, CAST(0x0000A77600A9486B AS DateTime), N'Admin'),
(8, 1, 14, 0, CAST(0x0000A77600A981A9 AS DateTime), N'Admin'),
(9, 1, 14, 0, CAST(0x0000A77600AB4821 AS DateTime), N'Admin'),
(10, 2, 14, 0, CAST(0x0000A77600AC0883 AS DateTime), N'Admin'),
(11, 2, 14, 0, CAST(0x0000A77600B25DF2 AS DateTime), N'Admin'),
(12, 2, 14, 0, CAST(0x0000A77600B2F07C AS DateTime), N'Admin'),
(13, 2, 14, 0, CAST(0x0000A77600DEDD85 AS DateTime), N'Admin'),
(14, 2, 14, 0, CAST(0x0000A77600DF5E89 AS DateTime), N'Admin'),
(15, 2, 14, 0, CAST(0x0000A77600DF8575 AS DateTime), N'Admin'),
(16, 2, 1, 0, CAST(0x0000A77B00B0F7C4 AS DateTime), N'Admin'),
(17, 2, 2, 0, CAST(0x0000A77B00B0F7C4 AS DateTime), N'Admin'),
(18, 2, 4, 20, CAST(0x0000A77B00B0F7C4 AS DateTime), N'Admin'),
(19, 2, 5, 20, CAST(0x0000A77B00B0F7C4 AS DateTime), N'Admin'),
(20, 2, 1, 60, CAST(0x0000A77B00B150AE AS DateTime), N'Admin'),
(21, 2, 2, 60, CAST(0x0000A77B00B150AE AS DateTime), N'Admin'),
(22, 2, 4, 40, CAST(0x0000A77B00B150AE AS DateTime), N'Admin'),
(23, 2, 5, 40, CAST(0x0000A77B00B150AE AS DateTime), N'Admin'),
(24, 2, 1, 60, CAST(0x0000A77B00BD3FDA AS DateTime), N'Admin'),
(25, 2, 2, 60, CAST(0x0000A77B00BD3FDA AS DateTime), N'Admin'),
(26, 2, 4, 40, CAST(0x0000A77B00BD3FDA AS DateTime), N'Admin'),
(27, 2, 5, 40, CAST(0x0000A77B00BD3FDA AS DateTime), N'Admin'),
(28, 2, 15, 14, CAST(0x0000A77B00BD832E AS DateTime), N'Admin'),
(29, 2, 16, 14, CAST(0x0000A77B00BD832E AS DateTime), N'Admin'),
(1002, 2, 14, 0, CAST(0x0000A77C0070AE72 AS DateTime), N'Admin'),
(1003, 2, 1, 60, CAST(0x0000A77C0070C1E6 AS DateTime), N'Admin'),
(1004, 2, 2, 60, CAST(0x0000A77C0070C1E6 AS DateTime), N'Admin'),
(1005, 2, 4, 40, CAST(0x0000A77C0070C1E6 AS DateTime), N'Admin'),
(1006, 2, 5, 40, CAST(0x0000A77C0070C1E6 AS DateTime), N'Admin'),
(1007, 2, 49, 16, CAST(0x0000A79100E87601 AS DateTime), N'Admin'),
(1008, 2, 50, 16, CAST(0x0000A79100E87601 AS DateTime), N'Admin'),
(1009, 2, 51, 16, CAST(0x0000A79100E87601 AS DateTime), N'Admin'),
(1016, 2, 49, 50, CAST(0x0000A79100FADB8A AS DateTime), N'Admin'),
(1017, 2, 50, 100, CAST(0x0000A79100FD7090 AS DateTime), N'Admin'),
(1018, 2, 49, 16, CAST(0x0000A79100FDB842 AS DateTime), N'Admin'),
(1025, 543, 50, 100, CAST(0x0000A794006340B9 AS DateTime), N'Admin'),
(1026, 543, 50, 100, CAST(0x0000A79400639285 AS DateTime), N'Admin'),
(1027, 543, 50, 100, CAST(0x0000A7940063D4EE AS DateTime), N'Admin'),
(1028, 543, 50, 100, CAST(0x0000A7940063E528 AS DateTime), N'Admin'),
(1029, 543, 50, 100, CAST(0x0000A79400644580 AS DateTime), N'Admin'),
(1030, 543, 50, 100, CAST(0x0000A79400649B97 AS DateTime), N'Admin'),
(1031, 543, 50, 100, CAST(0x0000A79400663C8F AS DateTime), N'Admin'),
(1033, 543, 50, 100, CAST(0x0000A79400578978 AS DateTime), N'Admin'),
(1034, 543, 49, 50, CAST(0x0000A79400579A40 AS DateTime), N'Admin'),
(1035, 541, 1, 160, CAST(0x0000A7970091A833 AS DateTime), N'Admin'),
(1036, 541, 2, 0, CAST(0x0000A7970091A833 AS DateTime), N'Admin'),
(1037, 541, 4, 160, CAST(0x0000A7970091A833 AS DateTime), N'Admin'),
(1038, 541, 5, 0, CAST(0x0000A7970091A833 AS DateTime), N'Admin'),
(1039, 541, 1, 0, CAST(0x0000A797009281A8 AS DateTime), N'Admin'),
(1040, 541, 2, 160, CAST(0x0000A797009281A8 AS DateTime), N'Admin'),
(1041, 541, 4, 0, CAST(0x0000A797009281A8 AS DateTime), N'Admin'),
(1042, 541, 5, 160, CAST(0x0000A797009281A8 AS DateTime), N'Admin'),
(1043, 541, 49, 50, CAST(0x0000A7970095C348 AS DateTime), N'Admin'),
(1044, 541, 34, 62, CAST(0x0000A79700973842 AS DateTime), N'Admin'),
(1045, 541, 52, 50, CAST(0x0000A79700974DEC AS DateTime), N'Admin'),
(1046, 541, 35, 25, CAST(0x0000A797009756C0 AS DateTime), N'Admin'),
(1049, 540, 34, 62, CAST(0x0000A79800AFFEE6 AS DateTime), N'Admin'),
(1050, 540, 1, 160, CAST(0x0000A79800B0493B AS DateTime), N'Admin'),
(1051, 540, 2, 0, CAST(0x0000A79800B0493B AS DateTime), N'Admin'),
(1052, 540, 4, 160, CAST(0x0000A79800B0493B AS DateTime), N'Admin'),
(1053, 540, 5, 0, CAST(0x0000A79800B0493B AS DateTime), N'Admin'),
(1054, 540, 1, 160, CAST(0x0000A79800B087C5 AS DateTime), N'Admin');
GO
IF OBJECT_ID('tempdb..#Pathways', 'U') IS NOT NULL
DROP TABLE #Pathways;
GO
CREATE TABLE #Pathways(
Id int NOT NULL PRIMARY KEY CLUSTERED,
Title nvarchar(max) NOT NULL,
Description nvarchar(max) NOT NULL
--ProviderType nvarchar(max) NULL,
--PatientPathway_Id int NULL,
--CalculationType varchar(50) NOT NULL,
--Instructions varchar(400) NULL,
--RecordStatus char(1) NOT NULL
);
GO
INSERT #Pathways (Id, Title, Description) VALUES
(1, N'COPD GOLD Risk Classification', N'COPD GOLD Classification'),
(2, N'TKA PT', N'Physical Therapy TKA'),
(4, N'Fall Assess - RN', N'Medical Fall Risk Screen'),
(5, N'Fall Assess - Rehab', N'Rehab Specialist Fall Assessment'),
(6, N'Fall Assess - Caregiver', N'Caregiver Fall Assessment'),
(7, N'Global Risk', N'Global Risk Stratification'),
(8, N'COPD Assess - RT', N'COPD Assessment - Respiratory Therapist'),
(9, N'MMRC Test', N'MMRC Dyspnea Test'),
(10, N'STOP BANG ', N'Stop Bang Assessment'),
(11, N'Care Placement Needs Assessment', N'Assessment for Patient Placement'),
(12, N'Driving Assessment', N'Driving Ability Assessemnt'),
(13, N'Home Care Needs Assessment', N'Home Care Needs Assessment'),
(14, N'Home Services Needs Assessment', N'Assessment to the specific needs for home services'),
(15, N'DbCustomerTest', N'Testing-1'),
(16, N'FRAT Assessment', N'Fall Risk Assessment'),
(17, N'A - Test Count Pathway', N'Test pathway for updated count scoring algorithm'),
(18, N'FRAT Assessment', N'Fall Risk Assessment'),
(21, N'AAA Test', N'Test'),
(25, N'CAT', N'COPD Assessment Test (CAT)');
GO
--SELECT * FROM #Responses r;
--SELECT * FROM #RiskScores rs;
--SELECT * FROM #PathwayOptions po;
--SELECT * FROM #Pathways p;
--====================================================================
--====================================================================
WITH
cte_PoRs AS (
SELECT
rs.ScheduleId,
po.PathwayId,
rs.PathwayOptionId,
rs.Score,
po.Risk,
rs.CreateDate,
po.Description,
RN = ROW_NUMBER() OVER (PARTITION BY po.PathwayId ORDER BY rs.Score DESC, po.Risk DESC, rs.CreateDate DESC)
FROM
#PathwayOptions po
JOIN #RiskScores rs
ON po.id = rs.PathwayOptionId
WHERE
rs.ScheduleId = 541
)
SELECT
pr.PathwayId,
Pathway = p.Description,
[Date] = cast(pr.CreateDate AS DATE),
OptionId = pr.PathwayOptionId,
Options = pr.Description,
pr.Score,
pr.Risk
FROM
cte_PoRs pr
JOIN #Pathways p
ON pr.PathwayId = p.Id
WHERE
pr.RN = 1;
结果...
PathwayId Pathway Date OptionId Options Score Risk
----------- -------------------------------------------------- ---------- ----------- -------------------------------------------------- ----------- -----------
1 COPD GOLD Classification 2017-06-19 4 GOLD Risk Level D - High Risk, High Symptom 160 10
10 Stop Bang Assessment 2017-06-19 34 High Risk of OSA 62 10
17 Test pathway for updated count scoring algorithm 2017-06-19 49 Low risk range 50 0
除了PathwayId 17的风险为2之外,所有内容似乎都符合您的预期输出...我仔细检查了数据......数据中唯一的[Pathway 17 / Risk 2]组合用于[ScheduleId] 2 /选项51]它的得分只有16分。希望这会有所帮助。