SQL

2012-12-17 2 views
1

당신은 내가 그들이 열 금액에서 생성되는 선택하려는 마지막 2 열 보면 나는 다음과 같은 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 

이있는 그것은 자체 새 컬럼은 select case 문을 사용하여 생성 된, 나는 SQL에 새로운 오전 그리고 내가 일하는 그런 일을 얻을 수있는 궁금해. invalid column name amount

답변

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 문에서도 많이와 쿼리의 종류의 데이터를 많이가 procesed되어 상당히 경우 preformace 속도가 느려집니다 것을 명심 이

1

사용 CROSS APPLY을 표시되기 전에 -client envirument 나는 클라이언트 측에서 계산을 할 수 있습니다 sugest 것이다 :

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 

subselect 메서드 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 

또한 null을 테스트하지 않아도됩니다. 즉, 열이 null 인 경우에는 "사이"가 아니므로 해당 값을 제거했습니다.