2012-12-17 67 views
1

我有以下SQL查詢SQL在同一個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 

如果你看一下,我要選擇他們是從柱量生成的最後兩列其中是由它自我生成一個新的列使用選擇案例語句,我是新來的SQL,我想知道如何我可以得到這樣的事情工作,所以基本上它說invalid column name amount

回答

2

列數量不存在,直到查詢是處理,所以我會sugest你一個子查詢,然後進行乘法運算

,如:

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 statments將減緩preformace如果顯著很多數據都procesed所以如果你使用這個在某些服務器-client envirument我會sugest你做在客戶端的計算顯示

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 

可能與子選擇方法suggested by @Jester一樣有效,這可能會證明更易於維護。

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 

而且,你不需要爲空所有這些測試 - 列是否爲空是不會有什麼「之間」,所以我刪除這些。