在平常的开发中常会碰到这样的情景数据格式如下销售人员 销售产品 销售日期 数量 金额 A ProductaA 2007-1-1 900 5000A ProductaA 2007-1-12 900 9000A ProductaA 2007-1-23 800 8000B ProductaB 2007-1-13 300 9000B ProductaB 2007-1-20 100 6000B ProductaB 2007-1-3 0 950 7000
因为销售人员, 销售产品 很多都是重复的,在显示查看的时候觉得有些重复,希望以下面的格式显示销售人员 销售产品 销售日期 数量 金额 A ProductaA 2007-1-1 900 5000 2007-1-12 900 9000 2007-1-23 800 8000B ProductaB 2007-1-13 300 9000 2007-1-20 100 6000 2007-1-30 950 700
思路:想要消除重复出现的数据,那么应该可以定义一个消除的依据,上情景情况下是将相同的销售人员和销售产品消除我们可以通过表自己的关联比较,同时加上一个可以区别的字段,那么这个字段可以通过identity(int, 1, 1)来实现select identity(int ,1, 1) as findex, FSaler, FSaleProduct, FSaleDate, FSaleQty, fSaleAmountinto #tmpfrom tableAorder by FSaler, FSaleProduct, FSaleDate --定义排序,因为到了后面修改的时候就无法排序了
update t1 set FSaler = '', FSaleProduct = ''from #tmp t1, #tmp t2where t1.FSaler = t2.FSaler and t1.FSaleProduct = t2.FSaleProduct and t1.FIndex > t2.FIndex --这句是关键所在
select * from #tmp
