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 

我收到一个错误:

无效的 objectName 'Customer_tool_Lookup'

它从我and在子查询末尾添加语句开始:

AND Customer_ID = Customer_Tool_Lookup.Customer_ID <--

他们每个人。

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

谢谢!

4

1 回答 1

0

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

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
于 2013-04-29T15:06:41.563 回答