如何基于查询SQL的结果运行子查询

时间:2015-02-20 15:24:46

标签: sql sql-server sql-server-2008 sql-server-2005

我想要运行2个查询。这里的想法是通过事务“类型”在事务表上运行查询。基于这些结果,我想运行另一个查询,以根据特定类型查看客户上次事务,以查看服务ID是否相同。如果它不相同,我想将其标记为“已升级”

以下是基于事务类型从事务表中提取结果的初始查询:

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

我认为这可能会奏效,但它并不能完全满足我的需要。它返回太多结果,因此查询显然不正确。:

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

3 个答案:

答案 0 :(得分:2)

如果我正确理解了这些要求,您可以使用ROW_NUMBER来确定哪个记录是每个customerID的最新记录。然后,您可以将其重新连接到事务表,以确定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'

答案 1 :(得分:0)

我认为你需要这样的东西。 这里t1为您提供每位客户的最大创建量; 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

答案 2 :(得分:0)

尝试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')