2013-04-29 80 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