想在我的sql中重新创建我的asp.net代码

时间:2011-06-14 18:32:23

标签: asp.net sql sql-server-2005

我目前有一个asp.net页面,它接收来自此视图的数据,并根据它将更改金额字段文本的百分比。这意味着如果用户达到某个百分比,他们就会获得一定的激励金额。

但是,我目前正在我的程序中执行此操作,但出于报告目的,我希望能够直接在sql中执行此操作,而不是在我的asp.net页面中执行此操作。

  

我想做的几乎就是   如果百分比列=   65%到70%然后金额列=   如果百分比列= 70到75,则为50   百分数然后数量列= 75

     

直接在我的数据视图中而不是在我的数据视图中   asp.net页面

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
                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 

这是我的查看查询,我几乎想添加一个if语句,但我不确定这是否是最好的方法。

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

1 个答案:

答案 0 :(得分:2)

从概念上讲,这应该相当简单。只需对您的查询采用以下公式。

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

修改

因为我不能(没有时间)重新创建你的桌子&amp;数据。

你不应该做类似的事情:

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