1

我有以下 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

4

3 回答 3

2

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

喜欢:

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 环境中使用它,我建议您在客户端进行计算之前被展示

于 2012-12-17T08:11:10.887 回答
1

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

可能与@Jester 建议的 subselect 方法一样有效,这可能会证明更易于维护。

于 2012-12-17T08:42:29.137 回答
1

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

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 将不会在任何“之间”,所以我删除了这些。

于 2012-12-17T09:25:55.407 回答