SubQuery访问父值

时间:2013-04-29 14:27:47

标签: sql sql-server-2008

大家好,我遇到了这个问题:

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 

我收到错误:

  

无效的objectName'Customer_tool_Lookup'

当我在子查询末尾添加and语句时开始:

AND Customer_ID = Customer_Tool_Lookup.Customer_ID <--
每个人都有一个。

我通常不会问我以前做过子查询的SQL问题,但由于某种原因我在使用父数据时遇到了麻烦。

谢谢!

1 个答案:

答案 0 :(得分:0)

我的建议是将这些相关子查询转换为您加入的单个子查询。如果您使用此子查询,则可以访问TDMonth CASE表达式中的别名:

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