CREATE TABLE XX (
A int,B varchar(10));
INSERT INTO XX
Values(10,'apple'),
(10,'pen'),
(13,'pig'),
(13,'pig'),
(14,'MM');
SELECT A
FROM (
SELECT DISTINCT A,B
FROM XX
) AS YY
GROUP BY A
HAVING COUNT(A)>1
CREATE TABLE [dbo].[_Tmp_0414](
[idx] [int] IDENTITY(1,1) NOT NULL,
[A] [nvarchar](10) NULL,
[B] [nvarchar](10) NULL
) ON [PRIMARY]
GO
select distinct A
,(select distinct B+',' from [_Tmp_0414] where A=t0.A for xml path(''))
as result
from [_Tmp_0414] t0
A result
10 apple,pen,
13 pen,
恩~你期望結果是 10
所以是這樣嗎??
declare @Tmp table(
A nvarchar(50)
,B nvarchar(50)
)
insert into @Tmp
values('10','apple')
,('10','pen')
,('13','pig')
,('13','pig')
select *
from @Tmp a
group by A,B
having Count(0) = 1
select A
from (
select *
from @Tmp a
group by A,B
having Count(0) = 1
) k
group by A