我有一个与报告服务一起使用的存储过程,并且该报告有一些用户过滤器将值传递到存储过程中。由于这些参数,显示的计数会发生变化,所以我想在分区中使用它们来更改计数。我尝试了以下不起作用。
编辑:
计数是在 ss_number 上完成的,而不是在 @SearchBy 上完成的。所以如果我有
SS# | Name | City | Amount
123456789 | Mike Smith | Trenton | 100.00
123456789 | Mike Smith | Trenton | 200.00
123456789 | Mike Smith | Jackson | 100.00
我的计数是 3,即使 @SearchBy = City 并且我在 Trenton 上进行过滤。
CASE WHEN @SearchBy = 'Product Name' THEN count(ss_number) OVER (PARTITION BY ss_number, @SearchBy)
现在我正在使用 case 语句,但它大大减慢了我的查询速度。
这是没有尝试在我的分区中使用变量的 case 语句的代码。
CREATE PROCEDURE [dbo].[sp_My_sp]
@SearchBy VARCHAR(MAX),
@SearchString VARCHAR(MAX),
@SearchNum Int,
@ClaimDate Datetime2
AS
WITH MyCTE AS
(
SELECT val.Claim_date
, val.Claim_Status
, val.Status_Desc
, ES_Claim_Status
, val.ss_number
, val.name_field1
, val.street_add1
, val.street_add2
, val.city
, val.state
, val.zip_code_pre
, val.reference_number
, val.Game_Name
, val.val_agent
, val.home_number
, val.work_phone
, val.county_desc
, o.agent_num
, count(ss_number) OVER (PARTITION BY ss_number, @SearchBy) as count
, prize_amount
FROM Sec_Claims val left outer join vw_owners_concat_agent_num o
ON val.SS_NUMBER = convert(varchar(15),o.SS_NO)
where convert(numeric,prize_amount) >= 600)
SELECT Claim_date
, CASE WHEN agent_num IS NULL THEN 'NO' ELSE 'YES' END as "IsRetailer"
, Claim_Status
, Status_Desc
, ES_Claim_Status
, ss_number
, name_field1
, street_add1
, street_add2
, city
, state
, zip_code_pre
, reference_number
, Game_Name
, val_agent
, home_number
, work_phone
, county_desc
, agent_num
, count
, ROW_NUMBER() OVER(PARTITION BY Name_Field1 ORDER BY Name_Field1) As RowNumber
, convert(decimal(10,2),prize_amount) as prize_amount
, sum(Convert(decimal(9,2),prize_amount)) OVER (PARTITION BY ss_number, Name_Field1) AS prizesum
FROM MyCTE
WHERE
(CASE
WHEN @SearchBy = 'Agent Number' THEN agent_num
WHEN @SearchBy = 'SS#' THEN SS_NUMBER
WHEN @SearchBy = 'Name' THEN Name_Field1
WHEN @SearchBy = 'Address' THEN STREET_ADD1
WHEN @SearchBy = 'City' THEN City
WHEN @SearchBy = 'Claim#' THEN convert(varchar(max),REFERENCE_NUMBER)
WHEN @SearchBy = 'Validating Retailer' THEN convert(varchar(max),VAL_AGENT)
WHEN @SearchBy = 'County' THEN COUNTY_DESC
WHEN @SearchBy = 'Home Phone' THEN convert(varchar(max),HOME_NUMBER)
WHEN @SearchBy = 'Work Phone' THEN convert(varchar(max),WORK_PHONE)
WHEN @SearchBy = 'Game Name' THEN GAME_NAME
END
like (@SearchString))
and
count>= @SearchNum
and
claim_date > @ClaimDate
ORDER BY ss_number