SQL 分组后只获取每组的一条数据

/****** Object:  Table [dbo].[TEMP]    Script Date: 2018-8-22 星期三 23:33:09 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[TEMP]([ID] [nvarchar](36) NULL,[SerialNo] [nvarchar](36) NULL,[Qty] [numeric](10, 1) NULL,[ForeignId] [nvarchar](32) NULL,[Flag] [bit] NOT NULL,[CreateDate] [datetime] NOT NULL) ON [PRIMARY]GOINSERT [dbo].[TEMP] ([ID], [SerialNo], [Qty], [ForeignId], [Flag], [CreateDate]) VALUES (N'6073188b18574593b847dd0cac936dd5', N'1bf1c0f4f5034b2588b1c2e2b4f80294', CAST(10.0 AS Numeric(10, 1)), N'f6df43bdc3ef4eaeb8c0d98c9abea7eb', 0, CAST(0x0000A944017BBFEF AS DateTime))GOINSERT [dbo].[TEMP] ([ID], [SerialNo], [Qty], [ForeignId], [Flag], [CreateDate]) VALUES (N'6073188b18574593b847dd0cac936dd5', N'1bf1c0f4f5034b2588b1c2e2b4f80294', CAST(10.0 AS Numeric(10, 1)), N'b01892f11a284eea990afe0a4e4398b4', 0, CAST(0x0000A944017BDEF9 AS DateTime))GOINSERT [dbo].[TEMP] ([ID], [SerialNo], [Qty], [ForeignId], [Flag], [CreateDate]) VALUES (N'136eba69477c41879e4d0cf9a791d460', N'e60cffd61c314d248403cfd08370ed8c', CAST(10.0 AS Numeric(10, 1)), N'4ca2da2abf434910b5d3d6ed6c343637', 0, CAST(0x0000A944017C3F82 AS DateTime))GOINSERT [dbo].[TEMP] ([ID], [SerialNo], [Qty], [ForeignId], [Flag], [CreateDate]) VALUES (N'136eba69477c41879e4d0cf9a791d460', N'e60cffd61c314d248403cfd08370ed8c', CAST(10.0 AS Numeric(10, 1)), N'b6fa2439e5bb44c58cff03b2725558d8', 0, CAST(0x0000A944017C5604 AS DateTime))GOINSERT [dbo].[TEMP] ([ID], [SerialNo], [Qty], [ForeignId], [Flag], [CreateDate]) VALUES (N'136eba69477c41879e4d0cf9a791d460', N'e60cffd61c314d248403cfd08370ed8c', CAST(11.0 AS Numeric(10, 1)), N'c465a72d36b5450e9a53e565ed95012d', 1, CAST(0x0000A944017B4EC2 AS DateTime))GOINSERT [dbo].[TEMP] ([ID], [SerialNo], [Qty], [ForeignId], [Flag], [CreateDate]) VALUES (N'136eba69477c41879e4d0cf9a791d460', N'e60cffd61c314d248403cfd08370ed8c', CAST(20.0 AS Numeric(10, 1)), N'd68efcd219634579966801b036ca8e29', 1, CAST(0x0000A944017C7370 AS DateTime))GOINSERT [dbo].[TEMP] ([ID], [SerialNo], [Qty], [ForeignId], [Flag], [CreateDate]) VALUES (N'6073188b18574593b847dd0cac936dd5', N'1bf1c0f4f5034b2588b1c2e2b4f80294', CAST(8.0 AS Numeric(10, 1)), N'2178fc8b537e4841a505e5e5a960ed83', 1, CAST(0x0000A944017C94CD AS DateTime))GOINSERT [dbo].[TEMP] ([ID], [SerialNo], [Qty], [ForeignId], [Flag], [CreateDate]) VALUES (N'6073188b18574593b847dd0cac936dd5', N'1bf1c0f4f5034b2588b1c2e2b4f80294', CAST(22.0 AS Numeric(10, 1)), N'7306f311b69e425cba13991030d7a2d3', 1, CAST(0x0000A944017CB571 AS DateTime))GOSELECT * FROM dbo.TEMP AWHERE A.ForeignId IN (SELECT TOP 1 B.ForeignId FROM dbo.TEMP B WHERE B.ID = A.ID AND B.SerialNo = A.SerialNoORDER BY B.Flag,B.CreateDate)

  

(0)

相关推荐