日期:2014-05-17 浏览次数:20761 次
if exists (select * from sysobjects where id = OBJECT_ID('[testtb]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [testtb]
CREATE TABLE [testtb] (
[shelfid] [varchar] (50) NOT NULL,
[goodsid] [varchar] (50) NOT NULL,
[statusid] [int] NOT NULL)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'1',N'1',13)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'1',N'2',13)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'1',N'3',13)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'2',N'4',13)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'2',N'5',13)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'2',N'6',13)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'1',N'1',14)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'1',N'2',14)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'1',N'3',14)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'2',N'4',14)
INSERT [testtb] ([shelfid],[goodsid],[statusid]) VALUES ( N'2',N'5',14)
select shelfid,count(distinct goodsid) 'goodsidcount'
from testtb
group by shelfid
/*
shelfid goodsidcount
-------------------------------------------------- ------------
1 3
2 3
(2 row(s) affected)
*/
select shelfid,COUNT(goodsidcount )goodsidcount
FROM (
SELECT shelfid,COUNT(goodsid) goodsidcount
FROM testtb
GROUP BY shelfid,goodsid
HAVING COUNT(goodsid)>1)a