从一个字段分组的笛卡尔连接中选择一个随机记录

时间:2016-02-01 20:27:41

标签: sql-server

假设有一个有意笛卡尔连接的随机数据集,是否可以为每个用户名提取唯一记录?

例如,我的数据集(#t2):

UserName     City           State      Zip
bradley      Eagle Lake     TX         77434
bradley      Hudsonville    MI         49426
bradley      Egan           LA         70531
will756      Twin Rocks     PA         15960
will756      Palm           PA         18070
will756      Thornton       KY         41855

这是使用笛卡尔连接创建的。现在我想为bradley选择一条记录,为will756选择一条记录,而我并不关心它是哪条记录。

如何做到这一点?

为了完整起见,这里是我写的SQL,以便让我在哪里:

select top 10 percent row_number() OVER (ORDER BY tmpZipCodes.Place,tmpZipCodes.StateCode,tmpZipCodes.ZipCode) n,tmpZipCodes.Place as UserCity, tmpZipCodes.StateCode as UserState, tmpZipCodes.ZipCode as UserZip 
into #t1
from [tmpDropDowns]
inner join tmpZipCodes on tmpZipCodes.uStateCode = tmpdropdowns.StoredValue
where tmpdropdowns.DDLName = 'ddlState' and CountryCode = 'US'
order by newid()


select member_login.username as UserName, #t1.UserCity, #t1.UserState, #t1.UserZip 
into #t2
from member_login, #t1

0 个答案:

没有答案