我有下面的查询运行正常,但在查询结束时,当我指定行数时,我没有得到我选择的具体数字。相反,数字要么更高,要么更低。我可能知道原因,但不知道如何解决它并获得确切的数字。
`WITH CTE AS
(
SELECT 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],
case
when mem_member_product like '%Stud%' then 'Associate
Student'
when mem_member_type in ('Associate Corporate','Associate
Member') then 'Private Associate'
When Mem_member_type in ('Associate Under 35
Sustaining','Young Leader Sustaining') Then 'Associate
Member Under 35'
When Mem_member_type = 'Full Under 35 Sustaining ' Then 'Full
Member Under 35'
When Mem_member_type in ('Associate Corporate', 'Associate
Member',
'Associate Private Member', 'Associate Sustaining', 'Supporting
Coordinator',
'Sustaining Coordinator') then 'Private Associate'
When Mem_member_type in ('Full Member', 'Full Member - ULI
GreenPrint Center', 'Full Private Member', 'Full Sustaining')
Then 'Private Full Member'
When Mem_member_type in ('Associate Public Agency', 'Associate
Public Member') then 'Public Associate'
When mem_member_type = 'Full Public Agency' then 'Public Full
Member'
Else 'Exclude'
End as 'Member Type',
ROW_NUMBER() OVER(PARTITION BY mem_member_type ORDER BY NEWID()) as
RN
FROM co_individual WITH (NOLOCK)
INNER JOIN co_individual_ext WITH (NOLOCK)
ON ind_cst_key_ext = ind_cst_key
INNER 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 NOT 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'
)
SELECT *
FROM CTE
where ([Member Type]= 'Private Associate' AND RN <= 3200)
OR ([Member Type]= 'Associate Member Under 35' AND RN <= 1680)
OR ([Member Type]= 'Private Full Member' AND RN <= 1040)
OR ([Member Type]= 'Public Associate' AND RN <= 960)
OR ([Member Type]= 'Associate Student' AND RN <= 640)
OR ([Member Type]= 'Public Full Member' AND RN <= 240)
OR ([Member Type]= 'Full Member Under 35' AND RN <= 80);`
有人可以帮助并修复查询吗?
答案 0 :(得分:0)
这可能是因为您的RN字段的CASE语句中包含回车符。例如,当会员产品类似于&#39;%Stud&#39;您将RN值设置为
'Associate (carriage return, tab, tab, tab, tab, space, space) Student'
而非仅仅是&#39; Associate Student&#39;。你可以分解字符串值之间逗号的行,但不能像下面这样划分中间字符串:
WITH CTE AS
(
SELECT 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],
CASE
WHEN mem_member_product LIKE '%Stud%'
THEN 'Associate Student'
WHEN mem_member_type IN ('Associate Corporate','Associate Member')
THEN 'Private Associate'
WHEN Mem_member_type IN ('Associate Under 35 Sustaining','Young Leader Sustaining')
THEN 'Associate Member Under 35'
WHEN Mem_member_type = 'Full Under 35 Sustaining '
THEN 'Full Member Under 35'
WHEN Mem_member_type IN ('Associate Corporate', 'Associate Member',
'Associate Private Member', 'Associate Sustaining', 'Supporting Coordinator',
'Sustaining Coordinator')
THEN 'Private Associate'
WHEN Mem_member_type IN ('Full Member', 'Full Member - ULI GreenPrint Center', 'Full Private Member', 'Full Sustaining')
THEN 'Private Full Member'
WHEN Mem_member_type IN ('Associate Public Agency', 'Associate Public Member')
THEN 'Public Associate'
WHEN mem_member_type = 'Full Public Agency'
THEN 'Public Full Member'
ELSE 'Exclude'
END
AS 'Member Type',
ROW_NUMBER() OVER(PARTITION BY mem_member_type ORDER BY NEWID()) AS
RN
FROM co_individual WITH (NOLOCK)
INNER JOIN co_individual_ext WITH (NOLOCK)
ON ind_cst_key_ext = ind_cst_key
INNER 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 NOT 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'
)
SELECT *
FROM CTE
WHERE ([Member Type]= 'Private Associate' AND RN <= 3200)
OR ([Member Type]= 'Associate Member Under 35' AND RN <= 1680)
OR ([Member Type]= 'Private Full Member' AND RN <= 1040)
OR ([Member Type]= 'Public Associate' AND RN <= 960)
OR ([Member Type]= 'Associate Student' AND RN <= 640)
OR ([Member Type]= 'Public Full Member' AND RN <= 240)
OR ([Member Type]= 'Full Member Under 35' AND RN <= 80);