开发者

want to recreate my asp.net code in my sql

I currently have an asp.net page, that takes in the data from this view and based on the percentage it will change the text of an amount field. Meaning that if a user reaches a certain percentage they get a certain incentive amount.

However, I am currently doing this in my program however, for reporting purposes i want to be able to do this straight directly in sql, and not in my asp.net page.

what i am trying to do is almost something like if percentage column = 65 to 70 percent then amount column = 50 if percentage column = 70 to 75 percent then amount column = 75

straight in my dataview and not in my asp.net page

If Decimal.Parse(percent.Text) >= 0.65 And percent.Text < 0.7 Then
                Amount.Text = 50
            ElseIf Decimal.Parse(percent.Text) >= 0.7 And percent.Text < 0.75 Then
    开发者_StackOverflow社区            Amount.Text = 75
            ElseIf Decimal.Parse(percent.Text) >= 0.75 And percent.Text < 0.8 Then
                Amount.Text = 200
            ElseIf Decimal.Parse(percent.Text) >= 0.8 And percent.Text < 0.85 Then
                Amount.Text = 500
            ElseIf Decimal.Parse(percent.Text) >= 0.85 And percent.Text < 0.9 Then
                Amount.Text = 625
            ElseIf Decimal.Parse(percent.Text) >= 0.9 And percent.Text < 0.95 Then
                Amount.Text = 750
            ElseIf Decimal.Parse(percent.Text) >= 0.95 And percent.Text < 1.0 Then
                Amount.Text = 1000 

Here is my view query, i almost want to add an if statement but im not sure if it is the best way of doing this.

SELECT     TOP (100) PERCENT SUM(yes) AS Countreported, SUM(no) AS Countnotreported, SUM(yes) + SUM(no) AS count, BMM, BYYYY, userid, SUM(pax) 
                          AS party, CAST(SUM(yes) AS decimal(4, 1)) / (SUM(yes) + SUM(no)) AS percentage
    FROM         (SELECT     SUM(CASE WHEN [insreported] IS NULL THEN 0 WHEN [insreported] = 'YES' THEN 1 ELSE 0 END) AS yes, 
                                                  SUM(CASE WHEN [insreported] IS NULL THEN 1 WHEN [insreported] = 'YES' THEN 0 ELSE 1 END) AS no, CASE WHEN USERID = 'chrisn' OR
                                                  USERID = 'CHRISN' THEN 'chrism' ELSE USERID END AS userid, dbo.agent_insurance_incentive_data.BMM, 
                                                  dbo.agent_insurance_incentive_data.BYYYY, SUM(dbo.agent_insurance_incentive_data.PARTY) AS pax, 
                                                  dbo.incentive_agents.department
                           FROM          dbo.agent_insurance_incentive_data LEFT OUTER JOIN
                                                  dbo.incentive_agents ON dbo.agent_insurance_incentive_data.USERID = dbo.incentive_agents.agent
                           WHERE      (dbo.agent_insurance_incentive_data.DYYYY >= '2009') AND (dbo.agent_insurance_incentive_data.BYYYY > 2008)
                           GROUP BY dbo.agent_insurance_incentive_data.USERID, dbo.agent_insurance_incentive_data.BMM, dbo.agent_insurance_incentive_data.BYYYY, 
                                                  dbo.agent_insurance_incentive_data.DMM, dbo.agent_insurance_incentive_data.DYYYY, dbo.incentive_agents.department) 
                          AS derived
    WHERE     (userid IN
                              (SELECT     agent
                                FROM          dbo.incentive_agents AS incentive_agents_1)) OR
                          (userid = 'chrisn')
    GROUP BY BMM, BYYYY, userid
    ORDER BY userid, BYYYY, BMM


Conceptually this should be fairly simple. Just adopt the following formula to your query.

Declare @Tmp Table(
    Percentage float 
)

Insert Into @Tmp Values (.65)
Insert Into @Tmp Values (.7)
Insert Into @Tmp Values (.75)
Insert Into @Tmp Values (.8)
Insert Into @Tmp Values (.85)
Insert Into @Tmp Values (.9)
Insert Into @Tmp Values (.95)

SELECT 
    CASE
        WHEN Percentage >= .65 AND Percentage < .7 THEN 50
        WHEN Percentage >= .7 AND Percentage < .75 THEN 75
        WHEN Percentage >= .75 AND Percentage < .8 THEN 200
        WHEN Percentage >= .8 AND Percentage < .85 THEN 500
        WHEN Percentage >= .85 AND Percentage < .9 THEN 625
        WHEN Percentage >= .9 AND Percentage < .95 THEN 750
        WHEN Percentage >= .95 AND Percentage < 1 THEN 1000
    END AS Amount,
    Percentage
FROM 
    @Tmp

EDIT:

Since I can't (don't have the time) to re-create your tables & data.

Shouldn't you be able to do something like:

SELECT 
    CASE
        WHEN Percentage >= .65 AND Percentage < .7 THEN 50
        WHEN Percentage >= .7 AND Percentage < .75 THEN 75
        WHEN Percentage >= .75 AND Percentage < .8 THEN 200
        WHEN Percentage >= .8 AND Percentage < .85 THEN 500
        WHEN Percentage >= .85 AND Percentage < .9 THEN 625
        WHEN Percentage >= .9 AND Percentage < .95 THEN 750
        WHEN Percentage >= .95 AND Percentage < 1 THEN 1000
    END AS Amount,
    *   
FROM 
(
    --Insert your query here
)Tmp2

--OR IF SQL doesn't like the GroupBy/OrderBy statements in the Sub Select 

Declare @Tmp3 Table(
    Countreported float,
    Countnotreported float,
    [count] float,
    BMM float,
    BYYYY float,
    userid int, 
    party int, 
    percentage decimal
)

INSERT INTO @Tmp3
    --Insert your query here

SELECT 
    CASE
        WHEN Percentage >= .65 AND Percentage < .7 THEN 50
        WHEN Percentage >= .7 AND Percentage < .75 THEN 75
        WHEN Percentage >= .75 AND Percentage < .8 THEN 200
        WHEN Percentage >= .8 AND Percentage < .85 THEN 500
        WHEN Percentage >= .85 AND Percentage < .9 THEN 625
        WHEN Percentage >= .9 AND Percentage < .95 THEN 750
        WHEN Percentage >= .95 AND Percentage < 1 THEN 1000
    END AS Amount,
    *   
FROM 
    @Tmp3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜