从表

时间:2017-08-23 13:52:05

标签: sql random rows

我有以下查询,返回超过10万行,如何从(例如)vw_client_uli_member_type中选择随机8000行?有人可以提供我的查询示例吗?

    SELECT ind_cst_key,
ind_int_code as 'Individual Type',
ind_first_name as 'First Name',
ind_last_name as 'Last Name',
cst_recno as 'Member ID',
cst_eml_address_dn as 'Email Address',
adr_city as 'City',
adr_state as 'State' ,
adr_country as 'Country',
cst_org_name_dn as 'Company',
cst_ixo_title_dn as 'Job Title',
mem_member_type as 'Member Type'
FROM 
co_individual  WITH (NOLOCK) 
JOIN co_individual_ext  WITH (NOLOCK)  ON ind_cst_key_ext=ind_cst_key 
JOIN co_customer  WITH (NOLOCK)  ON cst_key=ind_cst_key and ind_delete_flag=0
and ind_deceased_flag=0 
LEFT JOIN co_customer_x_address  WITH (NOLOCK)  ON cst_cxa_key=cxa_key 
LEFT JOIN co_address  WITH (NOLOCK)  ON adr_key=cxa_adr_key 
LEFT JOIN vw_client_uli_member_type  WITH (NOLOCK)  ON cst_key=mem_cst_key 
WHERE mem_member_type Is Null AND adr_country = N'UNITED STATES' AND ind_deceased_flag != 1 AND ind_key_leader_flag_ext != 1 AND ind_int_code != N'Staff' AND ind_int_code != N'Spouse' AND ind_int_code != N'Press'
ORDER BY adr_country DESC

2 个答案:

答案 0 :(得分:1)

您只需添加到您的查询:

SELECT TOP 8000 (columns) FROM table
ORDER BY NEWID()

这应该有效。

答案 1 :(得分:0)

这是@brunofernandes建议

后代码的样子
    SELECT top 8000 ind_cst_key,
ind_int_code as 'Individual Type',
ind_first_name as 'First Name',
ind_last_name as 'Last Name',
cst_recno as 'Member ID',
cst_eml_address_dn as 'Email Address',
adr_city as 'City',
adr_state as 'State' ,
adr_country as 'Country',
cst_org_name_dn as 'Company',
cst_ixo_title_dn as 'Job Title',
mem_member_type as 'Member Type'
FROM 
co_individual  WITH (NOLOCK) 
JOIN co_individual_ext  WITH (NOLOCK)  ON ind_cst_key_ext=ind_cst_key 
JOIN co_customer  WITH (NOLOCK)  ON cst_key=ind_cst_key and ind_delete_flag=0
and ind_deceased_flag=0 
LEFT JOIN co_customer_x_address  WITH (NOLOCK)  ON cst_cxa_key=cxa_key 
LEFT JOIN co_address  WITH (NOLOCK)  ON adr_key=cxa_adr_key 
LEFT JOIN vw_client_uli_member_type  WITH (NOLOCK)  ON cst_key=mem_cst_key 
WHERE mem_member_type Is Null AND adr_country = N'UNITED STATES' AND ind_deceased_flag != 1 AND ind_key_leader_flag_ext != 1 AND ind_int_code != N'Staff' AND ind_int_code != N'Spouse' AND ind_int_code != N'Press'
ORDER BY NEWID(), adr_country DESC