【问题标题】:sql generating a new column from a new column in the same SQL querysql 从同一 SQL 查询中的新列生成新列
【发布时间】:2012-12-17 08:00:44
【问题描述】:

我有以下 sql 查询

SELECT
i.catalogid, i.itemname,
CASE WHEN o.oshippeddate is not null 
          AND  o.oshippeddate between @Date1 AND @Date2
    THEN ISNULL(i.F2,0)
    ELSE 0 END +
CASE WHEN o.oshippeddate2 is not null 
          AND o.oshippeddate2 between @Date1 AND @Date2
    THEN ISNULL(i.F3,0) 
    ELSE 0 END +
CASE WHEN o.oshippeddate3 is not null 
          AND  o.oshippeddate3 between @Date1 AND @Date2
     THEN ISNULL(i.F4,0) 
     ELSE 0 END AS amount, 
    amount*i.ekprice EK,
    amount * (i.unitprice
              - ((i.unitprice/((o.otax/100)+1))
              - o.odiscount-o.oshipcost-o.coupondiscount) VK
FROM orders o 
INNER JOIN oitems i 
ON i.orderid = o.orderid 

如果您查看我要选择的最后 2 列,它们是从列数量生成的,它本身是使用 select case 语句生成的新列,我是 sql 新手,我想知道如何获得这样的工作,所以基本上它说invalid column name amount

【问题讨论】:

    标签: sql select sql-server-2008-r2 case


    【解决方案1】:

    在处理查询之前,列数量不存在,所以我建议您使用子查询,然后进行乘法

    喜欢:

    SELECT AA.*,(amount*ekprice)  as EK,
            (amount*(unitprice-((unitprice/((otax/100)+1))-odiscount-oshipcost-coupondiscount)) as VK
    FROM (
        SELECT
            i.catalogid,i.itemname,i.ekprice,i.unitprice,o.otax,o.odiscount,o.oshipcost,o.coupondiscount
            CASE WHEN o.oshippeddate is not null AND  o.oshippeddate  between @Date1 AND @Date2
                 THEN ISNULL(i.F2,0)
                 ELSE 0 END +
            CASE WHEN o.oshippeddate2 is not null AND o.oshippeddate2 between @Date1 AND @Date2
                 THEN ISNULL(i.F3,0) 
                 ELSE 0 END +
            CASE WHEN o.oshippeddate3 is not null AND  o.oshippeddate3 between @Date1 AND @Date2
                 THEN ISNULL(i.F4,0) 
                 ELSE 0 END AS amount   
        FROM 
            orders o 
            INNER JOIN oitems i ON i.orderid = o.orderid 
    )AS AA
    

    编辑:

    请记住,如果处理大量数据,这种带有大量 CASE 语句的查询会显着减慢执行速度,因此如果您在某些 SERVER-CLIENT 环境中使用它,我建议您在客户端上进行计算显示前的一面

    【讨论】:

      【解决方案2】:

      使用CROSS APPLY:

      SELECT
          i.catalogid, i.itemname, x.amount,
          x.amount * i.ekprice EK,
          x.amount * (i.unitprice
                    - ((i.unitprice/((o.otax/100)+1))
                    - o.odiscount-o.oshipcost-o.coupondiscount) VK
      FROM orders o 
      INNER JOIN oitems i 
      ON i.orderid = o.orderid 
      CROSS APPLY (
        SELECT
          CASE WHEN o.oshippeddate is not null 
                    AND  o.oshippeddate between @Date1 AND @Date2
              THEN ISNULL(i.F2,0)
              ELSE 0 END +
          CASE WHEN o.oshippeddate2 is not null 
                    AND o.oshippeddate2 between @Date1 AND @Date2
              THEN ISNULL(i.F3,0) 
              ELSE 0 END +
          CASE WHEN o.oshippeddate3 is not null 
                    AND  o.oshippeddate3 between @Date1 AND @Date2
               THEN ISNULL(i.F4,0) 
               ELSE 0 END AS amount
      ) x
      

      可能与子选择方法suggested by @Jester 一样高效,这可能会更易于维护。

      【讨论】:

        【解决方案3】:

        使用内部查询,选择您需要的所有列。

        SELECT catalogid, itemname,
            amount*ekprice EK,
            amount*(unitprice-((unitprice/((otax/100)+1))-odiscount-oshipcost-coupondiscount) VK
        FROM (
        SELECT
        i.catalogid,
        i.itemname,
        i.ekprice,
        i.unitprice,
        o.otax,
        o.odiscount,
        o.oshipcost,
        o.coupondiscount,
        CASE WHEN o.oshippeddate between @Date1 AND @Date2
             THEN ISNULL(i.F2,0)
             ELSE 0 END +
        CASE WHEN o.oshippeddate2 between @Date1 AND @Date2
             THEN ISNULL(i.F3,0) 
             ELSE 0 END +
        CASE WHEN o.oshippeddate3 between @Date1 AND @Date2
             THEN ISNULL(i.F4,0) 
             ELSE 0 END AS amount
        FROM orders o INNER JOIN oitems i 
        ON i.orderid = o.orderid 
        ) x
        

        此外,您不需要所有这些 null 测试 - 如果列为 null 将不会在任何“之间”,所以我删除了这些。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-06-15
          • 2014-02-01
          • 1970-01-01
          • 2020-03-08
          • 2014-12-02
          • 2022-09-22
          相关资源
          最近更新 更多