使用CASE语句和Alias创建另一列

时间:2018-11-12 20:24:42

标签: sql-server sql-server-2008

我试图根据我的长CASE语句创建一个名为“ Size”的新列。我使用“项目数”作为别名。

你能帮我吗?

我的CASE语句是:

  CASE WHEN (dbo.ItemSpecs.ConfigProptext LIKE 'Run%') THEN (CEILING(dbo.WorkOrderDetails.QtyOrdered / 4)) WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Pure' THEN (CEILING(dbo.WorkOrderDetails.QtyOrdered / 4)) 
                     WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Team%' THEN (CEILING(dbo.WorkOrderDetails.QtyOrdered / 4)) ELSE dbo.WorkOrderDetails.QtyOrdered END AS NumberofItems

,我要添加的新列是项目数<5时,显示“ car”,否则显示“ van”。该列应具有别名“ SIZE”

我坚持下去。

1 个答案:

答案 0 :(得分:0)

您需要将此内容嵌套在另一个application.assets.open(file_name).apply { json_string = this.readBytes().toString(Charsets.UTF_8) }.close() 中。

CASE

或者,另一种方法是使用CASE WHEN CASE WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Run%' THEN (CEILING(dbo.WorkOrderDetails.QtyOrdered / 4)) WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Pure' THEN (CEILING(dbo.WorkOrderDetails.QtyOrdered / 4)) WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Team%' THEN (CEILING(dbo.WorkOrderDetails.QtyOrdered / 4)) ELSE dbo.WorkOrderDetails.QtyOrdered END < 5 THEN 'Car' ELSE 'VAN' END as NewCol

CTE

或内联作为派生表

;with cte as(
select *,
        CASE 
            WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Run%'
                THEN (CEILING(dbo.WorkOrderDetails.QtyOrdered / 4)) 
            WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Pure' 
                THEN (CEILING(dbo.WorkOrderDetails.QtyOrdered / 4)) 
            WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Team%' 
                THEN (CEILING(dbo.WorkOrderDetails.QtyOrdered / 4)) 
            ELSE dbo.WorkOrderDetails.QtyOrdered 
        END as NumberofItems
from ItemSpecs)

select *,
    case when NumberofItems < 5 then 'car' else 'van' end as NewCol
from cte