如何使用CASE那么SQL

时间:2013-10-23 22:35:51

标签: sql tsql

我正在尝试根据产品的高度设置返回结果。如果产品大于86我想要将Transom和结果集一起返回,如果它小于我返回No Transom的那个。

我正在尝试使用下面的CASE WHENT来评估结果,但我的实现不正确。

    SELECT c.ID, 
        (p.Number +'|'+ pr.[Profile] +'|'+ SUBSTRING(CAST((p.Width - (pr.Siteline * 2))AS VARCHAR(9)),0,3)  
        + 'x84' +'|Pivot:'+e.PivotType+'|Hinge:'+e.Hinged +'|Swing:'+e.Swing) as [Name],
        ('Header:'+ CAST(pr.Siteline as VARCHAR(7))+'|Jamb:'+ CAST(pr.Siteline as VARCHAR(7)))as[Sitelines],

       CASE p.Height 
            WHEN > 86 THEN 'Transom'
            ELSE 'No Transom'

FROM            Generic.Part           p WITH(NOLOCK)                          JOIN
                Generic.Profiles      pr WITH(NOLOCK) ON p.ProfileID = pr.ID   JOIN
                Generic.ComponentPart cp WITH(NOLOCK) ON cp.PartID   = p.ID    JOIN
                Generic.Component      c WITH(NOLOCK) ON cp.ComponentID = c.ID JOIN
                Generic.ComponentType ct WITH(NOLOCK) ON ct.ID = c.ComponentTypeID JOIN
                Generic.FramingSystem fs WITH(NOLOCK) ON fs.ID = c.FrameSystemID   JOIN
                Generic.Entrance       e WITH(NOLOCK) ON c.ID = e.DoorFrameID
                WHERE fs.UserID = 'DA91DC34-FA29-4ABD-BCC0-xxxxxxxxxxxx'

2 个答案:

答案 0 :(得分:5)

您没有说实际错误是什么,但请尝试:

CASE
    WHEN p.Height > 86 THEN 'Transom'
    ELSE 'No Transom'
END AS [ColumnName]

答案 1 :(得分:1)

您错过了END

   CASE p.Height 
        WHEN > 86 THEN 'Transom'
        ELSE 'No Transom'
   END