标签:
;with n AS
(
SELECT skuId,skuName,MAX(skuCount) maxSkuCount,MIN(skuCount) minSkuCount
FROM chm
GROUP BY skuId,skuName
)
SELECT m.*
FROM chm m
INNER JOIN n ON m.skuId=n.skuId
WHERE m.skuCount <>n.maxSkuCount AND m.skuCount <>n.minSkuCount
CREATE TABLE [dbo].[chm](
[id] [int] IDENTITY(1,1) NOT NULL,
[skuId] [int] NOT NULL,
[skuCount] [decimal](18, 2) NOT NULL,
[skuName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_chm] 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 [dbo].[chm] ON
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (1, 1, CAST(1.00 AS Decimal(18, 2)), N‘a‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (2, 1, CAST(2.00 AS Decimal(18, 2)), N‘a‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (3, 1, CAST(3.00 AS Decimal(18, 2)), N‘a‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (4, 2, CAST(3.00 AS Decimal(18, 2)), N‘b‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (5, 2, CAST(2.00 AS Decimal(18, 2)), N‘b‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (6, 2, CAST(1.00 AS Decimal(18, 2)), N‘b‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (8, 3, CAST(2.00 AS Decimal(18, 2)), N‘c‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (9, 3, CAST(3.00 AS Decimal(18, 2)), N‘c‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (10, 3, CAST(1.00 AS Decimal(18, 2)), N‘c‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (11, 2, CAST(4.00 AS Decimal(18, 2)), N‘b‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (12, 1, CAST(0.50 AS Decimal(18, 2)), N‘a‘)
INSERT [dbo].[chm] ([id], [skuId], [skuCount], [skuName]) VALUES (13, 3, CAST(2.50 AS Decimal(18, 2)), N‘c‘)
SET IDENTITY_INSERT [dbo].[chm] OFF
标签:
原文地址:http://www.cnblogs.com/caohuimingfa/p/5437868.html