我的查询中关键字“FROM”附近的语法不正确

时间:2014-06-23 07:56:23

标签: sql sql-server sql-server-2008

SELECT dbo.ScrCalcr$.AgingDays, dbo.ScrCalcr$.EndingCount, dbo.ScrCalcr$.Priority, dbo.ScrCalcr$.TPDUNS, Score=(
CASE 
  WHEN dbo.ScrCalcr$.AgingDays >= 150 THEN 
    CASE 
    WHEN dbo.ScrCalcr$.EndingCount >= 150 THEN 
    ((10 * dbo.ScrCalcr$.EndingCount) + (15 * dbo.ScrCalcr$.AgingDays))/ (dbo.ScrCalcr$.Priority * 0.1) * 1 
    ELSE((10 * dbo.ScrCalcr$.EndingCount) + (15 * dbo.ScrCalcr$.AgingDays))/ (dbo.ScrCalcr$.Priority * 0.1) * 0.1 
    END 
  ELSE 0 

END
FROM dbo.ScrCalcr$
ORDER BY Score DESC
GO

我收到错误信息:

  

Msg 156,Level 15,State 1,Line 12

     

关键字'FROM'附近的语法不正确。

4 个答案:

答案 0 :(得分:1)

我们没有你的数据模型,所以我不能肯定地说,但似乎你需要这样的东西:

SELECT dbo.ScrCalcr$.AgingDays, dbo.ScrCalcr$.EndingCount, dbo.ScrCalcr$.Priority, dbo.ScrCalcr$.TPDUNS
FROM dbo.ScrCalcr$
WHERE Score=(
CASE 
  WHEN dbo.ScrCalcr$.AgingDays >= 150 THEN 
    CASE 
    WHEN dbo.ScrCalcr$.EndingCount >= 150 THEN 
    ((10 * dbo.ScrCalcr$.EndingCount) + (15 * dbo.ScrCalcr$.AgingDays))/ (dbo.ScrCalcr$.Priority * 0.1) * 1 
    ELSE((10 * dbo.ScrCalcr$.EndingCount) + (15 * dbo.ScrCalcr$.AgingDays))/ (dbo.ScrCalcr$.Priority * 0.1) * 0.1 
    END 
  ELSE 0 
END )
ORDER BY Score DESC

我将条件移到了where子句并删除了换行符。另外,在)之后添加了END

如果您想将Score作为结果中的字段,请使用:

SELECT dbo.ScrCalcr$.AgingDays, dbo.ScrCalcr$.EndingCount, dbo.ScrCalcr$.Priority, dbo.ScrCalcr$.TPDUNS
,      CASE 
       WHEN dbo.ScrCalcr$.AgingDays >= 150 THEN 
            CASE 
            WHEN dbo.ScrCalcr$.EndingCount >= 150 THEN 
            ((10 * dbo.ScrCalcr$.EndingCount) + (15 * dbo.ScrCalcr$.AgingDays))/ (dbo.ScrCalcr$.Priority * 0.1) * 1 
            ELSE((10 * dbo.ScrCalcr$.EndingCount) + (15 * dbo.ScrCalcr$.AgingDays))/ (dbo.ScrCalcr$.Priority * 0.1) * 0.1 
            END 
     ELSE 0 
     END
     Score
FROM dbo.ScrCalcr$
ORDER BY Score DESC

答案 1 :(得分:1)

我错过了正确的括号:Score=(

所以这应该有效:

SELECT dbo.ScrCalcr$.Agingdays, 
       dbo.ScrCalcr$.Endingcount, 
       dbo.ScrCalcr$.Priority, 
       dbo.ScrCalcr$.Tpduns, 
       Score=( CASE 
                 WHEN dbo.ScrCalcr$.Agingdays >= 150 THEN 
                   CASE 
                     WHEN dbo.ScrCalcr$.Endingcount >= 150 THEN 
                     ( 
                     ( 10 * dbo.ScrCalcr$.Endingcount ) + ( 15 * 
                     dbo.ScrCalcr$.Agingdays ) ) / 
                     ( 
                     dbo.ScrCalcr$.Priority * 0.1 
                     ) 
                     * 1 
                     ELSE( ( 10 * dbo.ScrCalcr$.Endingcount ) + 
                           ( 15 * dbo.ScrCalcr$.Agingdays ) ) / ( 
                               dbo.ScrCalcr$.Priority * 0.1 ) * 0.1 
                   END 
                 ELSE 0 
               END ) 
FROM   dbo.ScrCalcr$ 
ORDER  BY Score DESC 

答案 2 :(得分:0)

SELECT  S.AgingDays, 
        S.EndingCount, 
        S.Priority, 
        S.TPDUNS,
        CASE    WHEN S.AgingDays >= 150 THEN (
                CASE WHEN S.EndingCount >= 150 THEN ((10 * S.EndingCount) + (15 * S.AgingDays))/ (S.Priority * 0.1) * 1 
                ELSE((10 * S.EndingCount) + (15 * S.AgingDays))/ (S.Priority * 0.1) * 0.1 
        END)
        ELSE 0 END Score
FROM dbo.ScrCalcr$ S
ORDER BY Score DESC
GO

答案 3 :(得分:0)

您在END 语句后给出括号 像这样

SELECT dbo.ScrCalcr$.AgingDays,
 dbo.ScrCalcr$.EndingCount
 , dbo.ScrCalcr$.Priority, 
 dbo.ScrCalcr$.TPDUNS, 
 Score=
 (
CASE 
  WHEN dbo.ScrCalcr$.AgingDays >= 150 THEN 
    CASE 
    WHEN dbo.ScrCalcr$.EndingCount >= 150 THEN 
    ((10 * dbo.ScrCalcr$.EndingCount) + (15 * dbo.ScrCalcr$.AgingDays))/ (dbo.ScrCalcr$.Priority * 0.1) * 1 
    ELSE((10 * dbo.ScrCalcr$.EndingCount) + (15 * dbo.ScrCalcr$.AgingDays))/ (dbo.ScrCalcr$.Priority * 0.1) * 0.1 
    END 
  ELSE 0 

END)
FROM dbo.ScrCalcr$
ORDER BY Score DESC
GO