2013-04-29 58 views
0

您好所有我有這樣一個問題:子查詢訪問父值

Select Customer_Tool_Lookup.ID, 
    Customer.CustomerName, 
    (select count(ID) as perDay 
    FROM CustomerData 
    WHERE DatetimeInserted >= '2013-04-29 00:00:00.000' 
     AND DatetimeInserted <= '2013-04-29 11:59:59.599' 
     AND Customer_ID = Customer_Tool_Lookup.Customer_ID) as DCount, 
    (select count(ID) as perMonth 
    FROM CustomerData 
    WHERE DatetimeInserted >= '2013-04-01 00:00:00.000' 
     AND DatetimeInserted <= '2013-04-30 11:59:59.599' 
     AND Customer_ID = Customer_Tool_Lookup.Customer_ID) as mCount, 
    (select count(ID) as perYear 
    FROM CustomerData 
    WHERE DatetimeInserted >= '2013-01-01 00:00:00.000' 
     AND DatetimeInserted <= '2013-04-30 11:59:59.599' 
     AND Customer_ID = Customer_Tool_Lookup.Customer_ID) as yCount, 
    Customer_tool_Lookup.PricePerClick, 
    Customer_Tool_lookup.MinimumPerMonth, 
    case 
     when ClicksPerMonth > (select count(ID) as perMonth 
           FROM CustomerData 
           WHERE DatetimeInserted >= '2013-04-01 00:00:00.000' 
            AND DatetimeInserted <= '2013-04-30 11:59:59.599' 
            AND Customer_ID = Customer_Tool_Lookup.Customer_ID) 
     then ClicksPerMonth 
     else ((select count(ID) as perMonth 
       FROM CustomerData 
       WHERE DatetimeInserted >= '2013-04-01 00:00:00.000' 
        AND DatetimeInserted <= '2013-04-30 11:59:59.599' 
        AND Customer_ID = Customer_Tool_Lookup.Customer_ID) - Customer_tool_lookup.MinimumPerMonth) * PricePerClick END as TDMonth 
FROM Customer_tool_Lookup Left join Customer on Customer.ID = Customer_Tool_Lookup.Customer_ID 

我得到一個錯誤:

Invalid objectName 'Customer_tool_Lookup'

它開始時,我加入了and聲明的結尾子查詢:

AND Customer_ID = Customer_Tool_Lookup.Customer_ID <-- 

其中每一個。

我通常不會問SQL問題之前我做過子查詢,但由於某些原因,我無法使用父數據。

謝謝!

+1

你得給別名到表中,否則這是一個真正的混亂。查看查詢,甚至很難分辨出哪個Customer_Tool_Lookup恰好在那裏。 – sashkello 2013-04-29 14:31:32

回答

0

我的建議是將這些相關的子查詢轉換爲您加入的單個子查詢。如果使用此子查詢,那麼你就可以訪問你的別名CASE TDMonth表達式裏邊的:

Select ctl.ID, 
    c.CustomerName, 
    cd.DCount, 
    cd.mCount, 
    cd.yCount 
    ctl.PricePerClick, 
    ctl.MinimumPerMonth, 
    case 
     when ClicksPerMonth > cd.mCount 
     then ClicksPerMonth 
     else (cd.mCount - ctl.MinimumPerMonth) * PricePerClick 
    END as TDMonth 
from Customer_tool_Lookup ctl 
left join Customer c 
    on c.ID = ctl.Customer_ID 
left join 
(
    select Customer_ID, 
     COUNT(case 
       when DatetimeInserted >= '2013-04-29 00:00:00.000' 
        and DatetimeInserted <= '2013-04-29 11:59:59.599' 
       then ID end) as DCount, 
     COUNT(case 
       when DatetimeInserted >= '2013-04-01 00:00:00.000' 
        and DatetimeInserted <= '2013-04-30 11:59:59.599' 
       then ID end) as mCount, 
     COUNT(case 
       when DatetimeInserted >= '2013-01-01 00:00:00.000' 
        and DatetimeInserted <= '2013-04-30 11:59:59.599' 
       then ID end) as yCount  
    from CustomerData 
    group by Customer_ID 
) cd 
    on ctl.Customer_ID = cd.Customer_ID