如何根据某些列值对行进行分组

时间:2018-07-06 09:53:57

标签: sql sql-server tsql sql-server-2014

我在下表中有一个数据,如下所示。这只是一个样本数据,只有1个准入编号,实际数据可能有数百个具有自己的集合。因此,我想基于AdmissionNo和WardCode对行进行分组,这意味着WardCode为'KINT'的前13行应将grpValue设置为1,然后将下两行设置为2,然后是1,依此类推。

CREATE TABLE [dbo].[tbl_H](
    [AdmissionNo] [nvarchar](50) NULL,
    [WardCode] [nvarchar](255) NULL,
    [AdmissionDateTime] [datetime] NULL,
    [DischargeDateTime] [datetime] NULL,
    [date] [datetime] NULL
) ON [PRIMARY]

GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-06-29 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-06-30 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-01 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-02 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-03 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-04 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-05 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-06 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-07 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-08 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-09 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-10 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KINT', CAST(N'2016-06-29 01:59:00.000' AS DateTime), CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-11 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-12 03:38:00.000' AS DateTime), CAST(N'2016-07-11 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-11 17:44:00.000' AS DateTime), CAST(N'2016-07-12 03:38:00.000' AS DateTime), CAST(N'2016-07-12 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KEHH', CAST(N'2016-07-12 03:38:00.000' AS DateTime), CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-07-12 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-12 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-13 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-14 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-15 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-16 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-17 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-19 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-20 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-21 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-22 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-23 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-24 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-25 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-26 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-27 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-28 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-29 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-30 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-07-31 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-01 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-02 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-03 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-04 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-05 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-06 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-07 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-08 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-09 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-10 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-11 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-12 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-13 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-14 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-15 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-16 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-17 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCAR', CAST(N'2016-07-12 13:25:00.000' AS DateTime), CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-18 10:59:00.000' AS DateTime), CAST(N'2016-08-18 13:35:00.000' AS DateTime), CAST(N'2016-08-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-18 13:35:00.000' AS DateTime), CAST(N'2016-08-24 18:56:00.000' AS DateTime), CAST(N'2016-08-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-18 13:35:00.000' AS DateTime), CAST(N'2016-08-24 18:56:00.000' AS DateTime), CAST(N'2016-08-19 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-18 13:35:00.000' AS DateTime), CAST(N'2016-08-24 18:56:00.000' AS DateTime), CAST(N'2016-08-20 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-18 13:35:00.000' AS DateTime), CAST(N'2016-08-24 18:56:00.000' AS DateTime), CAST(N'2016-08-21 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-18 13:35:00.000' AS DateTime), CAST(N'2016-08-24 18:56:00.000' AS DateTime), CAST(N'2016-08-22 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-18 13:35:00.000' AS DateTime), CAST(N'2016-08-24 18:56:00.000' AS DateTime), CAST(N'2016-08-23 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-18 13:35:00.000' AS DateTime), CAST(N'2016-08-24 18:56:00.000' AS DateTime), CAST(N'2016-08-24 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KICA - IC', CAST(N'2016-08-24 18:56:00.000' AS DateTime), CAST(N'2016-08-26 13:23:00.000' AS DateTime), CAST(N'2016-08-24 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KICA - IC', CAST(N'2016-08-24 18:56:00.000' AS DateTime), CAST(N'2016-08-26 13:23:00.000' AS DateTime), CAST(N'2016-08-25 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KICA - IC', CAST(N'2016-08-24 18:56:00.000' AS DateTime), CAST(N'2016-08-26 13:23:00.000' AS DateTime), CAST(N'2016-08-26 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-26 13:23:00.000' AS DateTime), CAST(N'2016-08-26 13:30:00.000' AS DateTime), CAST(N'2016-08-26 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-26 13:30:00.000' AS DateTime), CAST(N'2016-08-30 15:12:00.000' AS DateTime), CAST(N'2016-08-26 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-26 13:30:00.000' AS DateTime), CAST(N'2016-08-30 15:12:00.000' AS DateTime), CAST(N'2016-08-27 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-26 13:30:00.000' AS DateTime), CAST(N'2016-08-30 15:12:00.000' AS DateTime), CAST(N'2016-08-28 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-26 13:30:00.000' AS DateTime), CAST(N'2016-08-30 15:12:00.000' AS DateTime), CAST(N'2016-08-29 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[tbl_H] ([AdmissionNo], [WardCode], [AdmissionDateTime], [DischargeDateTime], [date]) VALUES (N'0000001742', N'KCH2', CAST(N'2016-08-26 13:30:00.000' AS DateTime), CAST(N'2016-08-30 15:12:00.000' AS DateTime), CAST(N'2016-08-30 00:00:00.000' AS DateTime))
GO

这是表格的外观: Table

但是问题在于,对于相同的AdmissinoNo,WardCode在以后的行中再次重复。因此,如何将这些行指定为不同的GroupValue,因为当我尝试对它们进行分组时,即使它们之间还存在其他WardCode,它们也会一起分组。可以基于AdmissionDateTime然后DischargeDateTime对行进行排序。 首先,我在列表中通过“ ROW_NUMBER()OVER(ORDER BY AdmissionNo,AdmissionDateTime,DischargeDateTime)作为rownum”添加行号,然后进行以下查询:

;WITH cte as (
SELECT *,
       ROW_NUMBER() OVER (PARTITION BY AdmissionNo,WardCode ORDER BY rownum) as rn,
       rownum - ROW_NUMBER() OVER (PARTITION BY AdmissionNo,WardCode ORDER BY rownum) as grp
FROM tbl_h
)    
SELECT *, DENSE_RANK() OVER (ORDER BY grp) as new_Grp
FROM cte

但是这也将所有同名的病区代码分组,即使它们之间还有其他病区代码。

2 个答案:

答案 0 :(得分:1)

由于使用了Ajay Gupta,我通过以下查询解决了该问题:

;WITH cte as (
SELECT *,
   ROW_NUMBER() OVER (PARTITION BY AdmissionNo,WardCode ORDER BY rownum) as rn,
   rownum - ROW_NUMBER() OVER (PARTITION BY AdmissionNo,WardCode ORDER BY rownum) as 
grp
FROM tbl_h
)    
SELECT *, DENSE_RANK() OVER (ORDER BY grp) as new_Grp
FROM cte
ORDER BY rownum

答案 1 :(得分:-1)

我想这就是你想要的。

SELECT * 
FROM TABLE1 
GROUP BY AdmissinoNo,WardCode
ORDER BY AdmissinoNo,WardCode

首先按AdmissinoNo和WardCode对它进行分组 然后按AdmissinoNo和WardCode对其进行排序