Sql 访问父值的子查询
大家好,我有一个问题:Sql 访问父值的子查询,sql,sql-server-2008,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'
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
他们每人一个
我通常不会问我以前做过子查询的SQL问题,但由于某些原因,我在使用父数据时遇到了问题
谢谢 我的建议是将这些相关子查询转换为您加入的单个子查询。如果使用此子查询,则可以访问
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
选择ctl.ID,
c、 客户名称,
cd.DCount,
cd.mCount,
cd.yCount
ctl.PricePerClick,
ctl.MinimumPerMonth,
案例
单击每月>cd.mCount时
然后单击每月一次
其他(cd.mCount-ctl.MinimumPerMonth)*价格点击
月底
来自客户工具查找ctl
左连接客户c
在c.ID=ctl.Customer\u ID上
左连接
(
选择客户ID,
计数(大小写)
插入日期时间>='2013-04-29 00:00:00.000'
插入的日期时间='2013-04-01 00:00:00.000'
插入的日期时间='2013-01-01 00:00:00.000'
还有DatetimeInserted,你必须给你的表加上别名,否则就太乱了。查看查询,甚至很难知道你到底想要哪个客户工具查找。。。
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