如何 运行 基于查询结果的子查询 SQL
How to run a subquery based on results of a query SQL
我有 2 个问题想 运行。这里的思路是通过事务"type"运行查询事务table。基于这些结果,我想 运行 另一个查询以查看基于特定类型的客户上次交易以查看服务 ID 是否相同。如果不一样,我想将其标记为 "upgraded"
这是根据交易类型从交易 table 中提取结果的初始查询:
Select customerid, serviceid
from Transactions
where (dtcreated > @startdate and dtcreated < @enddate) and (transactiontype = 'Cust Save')
这个输出是:
Customerid ServiceID
1 11
2 21
3 21
4 11
5 12
6 11
接下来我想做的是 运行 这个查询,匹配 customerID 以查看客户最后一次收费是什么:
Select serviceID, MAx(dtcreated) as MostRecent
From Transactions
Where (transactiontype = 'Cust Purchase')
Group By serviceID
我结合这两个查询的最终输出是:
Customerid ServiceID Last Purchase Upgraded?
1 11 11 No
2 21 11 Yes
3 21 12 Yes
4 11 10 Yes
5 12 12 No
6 11 11 No
我认为这可能有用,但它并不能完全满足我的需求。 returns结果太多,所以查询显然不正确。:
Select serviceID, MAx(dtcreated) as MostRecent
From Transactions
WHERE Where (transactiontype = 'Cust Purchase') AND EXISTS
(Select customerid, serviceid
from Transactions
where (dtcreated > @startdate and dtcreated < @enddate) and (transactiontype = 'Cust Save'))
GROUP BY serviceid
我想你需要这样的东西。
这里 t1 为您提供了每个客户创建的最大 dt;
t2 包含给定日期范围内的所有交易;
t3 为您提供每个客户的最后一次购买。
select
t1.customerid,
t3.serviceid as Last_Purchase_ServiceID,
t1.dtcreated as Last_Purchase_DateCreated,
t2.ServiceID as Current_Purchase_ServiceID,
t2.dtcreated as Current_Purchase_DateCreated
from
(
select customerid, max(dtcreated) as dtcreated
from
Transactions
group by customerid
)
t1
join
(
select customerid, serviceid, dtcreated
from Transactions
where (dtcreated > @startdate and dtcreated < @enddate) and (transactiontype = 'Cust Save')
) t2 on t1.customerid = t2.customerid
join
Transactions t3 on t1.customerid = t3.customerid and t1.dtcreated = t3.dtcreated
如果我正确理解这些要求,您可以使用 ROW_NUMBER
来确定每个客户 ID 的最新记录。然后您可以将其加入到事务 table 以确定 ServiceID 中是否存在匹配项:
SELECT r.CustomerID,
t.ServiceID,
t.dtCreated,
Upgraded = CASE WHEN t.ServiceID = cp.ServiceID THEN 0 ELSE 1 END
FROM Transactions AS t
INNER JOIN
( SELECT CustomerID,
ServiceID,
dtCreated,
RowNumber = ROW_NUMBER() OVER(PARTITION BY CustomerID ORDER BY dtCreated DESC)
FROM Transactions
WHERE transactiontype = 'Cust Purchase'
) AS cp
ON cp.CustomerID = t.CustomerID
AND cp.RowNumber = 1
WHERE t.dtcreated > @startdate
AND t.dtcreated < @enddate
AND t.transactiontype = 'Cust Save'
尝试 OUTER APPLY
:
Select t.customerid, t.serviceid, o.MostRecent
from Transactions t
OUTER APPLY (
Select MAx(dtcreated) as MostRecent
From Transactions
Where transactiontype = 'Cust Purchase' and Customerid = t.Customerid --And ServiceID = t.ServiceID
) o
where (t.dtcreated > @startdate and t.dtcreated < @enddate) and (t.transactiontype = 'Cust Save')
我有 2 个问题想 运行。这里的思路是通过事务"type"运行查询事务table。基于这些结果,我想 运行 另一个查询以查看基于特定类型的客户上次交易以查看服务 ID 是否相同。如果不一样,我想将其标记为 "upgraded"
这是根据交易类型从交易 table 中提取结果的初始查询:
Select customerid, serviceid
from Transactions
where (dtcreated > @startdate and dtcreated < @enddate) and (transactiontype = 'Cust Save')
这个输出是:
Customerid ServiceID
1 11
2 21
3 21
4 11
5 12
6 11
接下来我想做的是 运行 这个查询,匹配 customerID 以查看客户最后一次收费是什么:
Select serviceID, MAx(dtcreated) as MostRecent
From Transactions
Where (transactiontype = 'Cust Purchase')
Group By serviceID
我结合这两个查询的最终输出是:
Customerid ServiceID Last Purchase Upgraded?
1 11 11 No
2 21 11 Yes
3 21 12 Yes
4 11 10 Yes
5 12 12 No
6 11 11 No
我认为这可能有用,但它并不能完全满足我的需求。 returns结果太多,所以查询显然不正确。:
Select serviceID, MAx(dtcreated) as MostRecent
From Transactions
WHERE Where (transactiontype = 'Cust Purchase') AND EXISTS
(Select customerid, serviceid
from Transactions
where (dtcreated > @startdate and dtcreated < @enddate) and (transactiontype = 'Cust Save'))
GROUP BY serviceid
我想你需要这样的东西。 这里 t1 为您提供了每个客户创建的最大 dt; t2 包含给定日期范围内的所有交易; t3 为您提供每个客户的最后一次购买。
select
t1.customerid,
t3.serviceid as Last_Purchase_ServiceID,
t1.dtcreated as Last_Purchase_DateCreated,
t2.ServiceID as Current_Purchase_ServiceID,
t2.dtcreated as Current_Purchase_DateCreated
from
(
select customerid, max(dtcreated) as dtcreated
from
Transactions
group by customerid
)
t1
join
(
select customerid, serviceid, dtcreated
from Transactions
where (dtcreated > @startdate and dtcreated < @enddate) and (transactiontype = 'Cust Save')
) t2 on t1.customerid = t2.customerid
join
Transactions t3 on t1.customerid = t3.customerid and t1.dtcreated = t3.dtcreated
如果我正确理解这些要求,您可以使用 ROW_NUMBER
来确定每个客户 ID 的最新记录。然后您可以将其加入到事务 table 以确定 ServiceID 中是否存在匹配项:
SELECT r.CustomerID,
t.ServiceID,
t.dtCreated,
Upgraded = CASE WHEN t.ServiceID = cp.ServiceID THEN 0 ELSE 1 END
FROM Transactions AS t
INNER JOIN
( SELECT CustomerID,
ServiceID,
dtCreated,
RowNumber = ROW_NUMBER() OVER(PARTITION BY CustomerID ORDER BY dtCreated DESC)
FROM Transactions
WHERE transactiontype = 'Cust Purchase'
) AS cp
ON cp.CustomerID = t.CustomerID
AND cp.RowNumber = 1
WHERE t.dtcreated > @startdate
AND t.dtcreated < @enddate
AND t.transactiontype = 'Cust Save'
尝试 OUTER APPLY
:
Select t.customerid, t.serviceid, o.MostRecent
from Transactions t
OUTER APPLY (
Select MAx(dtcreated) as MostRecent
From Transactions
Where transactiontype = 'Cust Purchase' and Customerid = t.Customerid --And ServiceID = t.ServiceID
) o
where (t.dtcreated > @startdate and t.dtcreated < @enddate) and (t.transactiontype = 'Cust Save')