我现在只能得出其中一个属性不相等的
以下是体积不相等的
select *
from testTB T
where id=(select min(id) from testTB where (Attribute='体积' and AttributeValue=T.AttributeValue))ID ProductID Attribute AttributeValue ProductName
1 1 体积 1M 111
3 2 体积 2M 222
5 3 体积 3M 333
以下是体积不相等的
select *
from testTB T
where id=(select min(id) from testTB where (Attribute='体积' and AttributeValue=T.AttributeValue))ID ProductID Attribute AttributeValue ProductName
1 1 体积 1M 111
3 2 体积 2M 222
5 3 体积 3M 333
少了一个数据
表结构内容如下
ID ProductID Attribute AttributeValue ProductName
1 1 体积 1M 111
2 1 重量 1KG 111
3 2 体积 2M 222
4 2 重量 2KG 222
5 3 体积 3M 333
6 3 重量 3KG 333
7 4 体积 1M 444
8 4 重量 8KG 444
9 4 体积 6M 444
10 4 重量 8KG 444
ID ProductID Attribute AttributeValue ProductName
1 1 体积 1M 111
2 1 重量 1KG 111
3 2 体积 2M 222
4 2 重量 2KG 222
5 3 体积 3M 333
6 3 重量 3KG 333
7 4 体积 1M 444
8 4 重量 8KG 444
9 4 体积 6M 555
10 4 重量 8KG 555
ID int identity(1,1),
ProductID int,
Attribute nvarchar(10),
AttributeValue varchar(10),
ProductName int)
insert t select 1, N'体积', '1M', 111
union all select 1, N'重量', '1KG', 111
union all select 2, N'体积', '2M', 222
union all select 2, N'重量', '2KG', 222
union all select 3, N'体积', '3M', 333
union all select 3, N'重量', '3KG', 333
union all select 4, N'体积', '1M', 444
union all select 4, N'重量', '8KG', 444
GOCREATE FUNCTION f_1
(
@ID int
)
RETURNS nvarchar(1000)
AS
BEGIN
declare @ret nvarchar(1000)
set @ret=''
select @ret=@ret+Attribute+AttributeValue from t where ID=@ID
return @ret
END
GO
select distinct t.ProductID,ProductName from
(select count(1) [count],ProductID from
(
select min(ProductID) [ProductID] from t group by dbo.f_1(ID)
) a group by ProductID) b
join t on b.ProductID=t.ProductID
where b.count=2--如果为2说明最小且全部都保留,而1的或者0的产品则有相同被覆盖的情况。
/*
ProductID ProductName
----------- -----------
1 111
2 222
3 333
*/
drop function f_1
drop table t
ID int identity(1,1),
ProductID int,
Attribute nvarchar(10),
AttributeValue varchar(10),
ProductName int)
insert t select 1, N'体积', '1M', 111
union all select 1, N'重量', '1KG', 111
union all select 2, N'体积', '2M', 222
union all select 2, N'重量', '2KG', 222
union all select 3, N'体积', '3M', 333
union all select 3, N'重量', '3KG', 333
union all select 4, N'体积', '1M', 444
union all select 4, N'重量', '8KG', 444
union all select 5, N'体积', '6M', 555
union all select 5, N'重量', '8KG', 444select id=identity(int, 1, 1), ProductName=t1.ProductName, 体积=t1.attributevalue, 重量=t2.attributevalue
into #temp
from t as t1 , t as t2
where t1.productid=t2.productid
and t1.Attribute='体积'
and t2.Attribute='重量'delete #temp
where exists(
select 1
from #temp as tt
where #temp.id>tt.id
and #temp.体积=tt.体积)delete #temp
where exists(
select 1
from #temp as tt
where #temp.id>tt.id
and #temp.重量=tt.重量)select * from #temp
drop table #temp
drop table t