日期:2014-05-18  浏览次数:20527 次

SQL分组随机取数据
KID A1 A2
nc_13250380221109 25.06 2793.30
nc_13250380221109 25.06 3990.42
nc_13250380221109 25.06 3990.42
nc_13250380221142 25.06 3990.42
nc_13250380221142 25.06 3990.42
nc_13250380221142 25.06 2793.30
nc_13250380221186 25.06 2793.30
nc_13250380221186 25.06 3990.42
nc_13250380221188 25.06 3990.42
nc_13250380221188 25.06 2793.30
nc_13250380221188 25.06 3990.42
nc_13250380221189 25.06 2793.30
nc_13250380221193 25.06 2793.30
nc_13250380221193 25.06 3990.42
nc_13250380221193 25.06 3990.42
nc_13250380221195 25.06 2793.30
nc_13250380221195 25.06 3990.42
nc_13250380221195 25.06 3990.42
nc_13250380221301 16.55 3021.15
nc_13250380221301 16.55 3021.15

如例子,要按照KID分组,分组后,每组随机取一条记录

------解决方案--------------------
SQL code
select
  distinct b.*
from
  tb a
cross apply
  (select top 1 * from tb where kid=a.kid order by newid())b