SQL逻辑:需要具有最大日期的行

时间:2017-05-23 20:43:22

标签: sql sql-server ssrs-2008

好的,我有以下查询,用于构建一组客户的表,这些客户在一段时间内使用其帐户中的产品。该报告使用该表来查看哪些客户在第二时间段内未使用其帐户中的产品。

我的问题是,对于那些在其帐户中使用过产品的人,可能会在此期间多次这样做。因此,当我从报告中撤回在第一期而非第二期使用产品的成员的数据时,我得到多行,因为他们多次使用产品。

我只需要最新/最大的使用日期行而不是其他行。我尝试使用MAX(SSUSED_1.usedate),但我注意到了我想要的东西。任何帮助将不胜感激!

WITH cte AS (
    SELECT
        SS.memid
    FROM SS
    INNER JOIN SSUSED ON SS.ssid = SSUSED.ssid
    INNER JOIN MEMBERS AS MEMBERS_1 ON SS.memid = MEMBERS_1.memid
    INNER JOIN PRODUCTS ON SS.productid = PRODUCTS.productid
    INNER JOIN PRODUCTCATS ON PRODUCTS.productcatid = PRODUCTCATS.productcatid
    INNER JOIN EMPLOYEES ON SSUSED.employeeid = EMPLOYEES.employeeid
    WHERE
        MEMBERS_1.siteid = @rvSite
        AND SSUSED.usedate BETWEEN @rvStartWeek2 AND @rvEndWeek2
        AND PRODUCTS.productcatid IN (27, 28, 29, 58, 77, 75, 30, 61, 31, 32, 47))

SELECT DISTINCT 
    SS_1.memid,
    MEMBERS_1.scancode,
    MEMBERS_1.lname,
    MEMBERS_1.fname,
    PRODUCTS_1.description,
    EMPLOYEES_1.lname AS TrainLName,
    EMPLOYEES_1.fname AS TrainFName,
    SSUSED_1.usedate,
    SSUSED_1.transactiontype
FROM SS AS SS_1
INNER JOIN SSUSED AS SSUSED_1 ON SS_1.ssid = SSUSED_1.ssid
INNER JOIN MEMBERS AS MEMBERS_1 ON SS_1.memid = MEMBERS_1.memid
INNER JOIN PRODUCTS AS PRODUCTS_1 ON SS_1.productid = PRODUCTS_1.productid
INNER JOIN PRODUCTCATS AS PRODUCTCATS_1 ON PRODUCTS_1.productcatid = PRODUCTCATS_1.productcatid
INNER JOIN EMPLOYEES AS EMPLOYEES_1 ON SSUSED_1.employeeid = EMPLOYEES_1.employeeid
WHERE
    MEMBERS_1.siteid = @rvSite
    AND SSUSED_1.usedate BETWEEN @rvStartWeek1 AND @rvEndWeek1
    AND PRODUCTS_1.productcatid IN (27, 28, 29, 58, 77, 75, 30, 61, 31, 32, 47)
    AND NOT EXISTS
        (SELECT
            1 AS Expr1
        FROM cte AS cte_1
        WHERE SS_1.memid = memid)
ORDER BY TrainLName, MEMBERS_1.scancode, SSUSED_1.usedate DESC

3 个答案:

答案 0 :(得分:0)

以下可能有用吗?

WITH cte AS (SELECT        SS.memid
                             FROM            SS INNER JOIN
                                                      SSUSED ON SS.ssid = SSUSED.ssid INNER JOIN
                                                      MEMBERS AS MEMBERS_1 ON SS.memid = MEMBERS_1.memid INNER JOIN
                                                      PRODUCTS ON SS.productid = PRODUCTS.productid INNER JOIN
                                                      PRODUCTCATS ON PRODUCTS.productcatid = PRODUCTCATS.productcatid INNER JOIN
                                                      EMPLOYEES ON SSUSED.employeeid = EMPLOYEES.employeeid
                             WHERE        (MEMBERS_1.siteid = @rvSite) AND (SSUSED.usedate BETWEEN @rvStartWeek2 AND @rvEndWeek2) AND (PRODUCTS.productcatid IN (27, 28, 29, 
                                                      58, 77, 75, 30, 61, 31, 32, 47)))
    SELECT -- DISTINCT 
                              SS_1.memid, MEMBERS_1.scancode, MEMBERS_1.lname, MEMBERS_1.fname, PRODUCTS_1.description, EMPLOYEES_1.lname AS TrainLName, 
                              EMPLOYEES_1.fname AS TrainFName, SSUSED_1.usedate, SSUSED_1.transactiontype
     FROM            SS AS SS_1 INNER JOIN
                              SSUSED AS SSUSED_1 ON SS_1.ssid = SSUSED_1.ssid INNER JOIN
                              MEMBERS AS MEMBERS_1 ON SS_1.memid = MEMBERS_1.memid INNER JOIN
                              PRODUCTS AS PRODUCTS_1 ON SS_1.productid = PRODUCTS_1.productid INNER JOIN
                              PRODUCTCATS AS PRODUCTCATS_1 ON PRODUCTS_1.productcatid = PRODUCTCATS_1.productcatid INNER JOIN
                              EMPLOYEES AS EMPLOYEES_1 ON SSUSED_1.employeeid = EMPLOYEES_1.employeeid
     WHERE        (MEMBERS_1.siteid = @rvSite) AND (SSUSED_1.usedate BETWEEN @rvStartWeek1 AND @rvEndWeek1) AND (PRODUCTS_1.productcatid IN (27, 28, 29, 58, 77, 
                              75, 30, 61, 31, 32, 47)) AND (NOT EXISTS
                                  (SELECT        1 AS Expr1
                                    FROM            cte AS cte_1
                                    WHERE        (SS_1.memid = memid)))
     -- added below
     GROUP BY SS_1.memid, MEMBERS_1.scancode, MEMBERS_1.lname, MEMBERS_1.fname, PRODUCTS_1.description, EMPLOYEES_1.lname AS TrainLName, 
              EMPLOYEES_1.fname AS TrainFName, SSUSED_1.transactiontype HAVING MAX(SSUSED_1.usedate) = SSUSED_1.usedate
     ORDER BY TrainLName, MEMBERS_1.scancode, SSUSED_1.usedate DESC

答案 1 :(得分:0)

我修改了第4行以在CTE中添加为列

ROW_NUMBER() OVER (PARTITION BY SS.memid ORDER BY SSUSED.usedate) AS RowNumber 

我添加到NOT EXISTS语句

AND cte_1.RowNumber = 1

这导致:

WITH cte AS (
    SELECT
        SS.memid,
        ROW_NUMBER() OVER (PARTITION BY SS.memid ORDER BY SSUSED.usedate) AS RowNumber
    FROM SS
    INNER JOIN SSUSED ON SS.ssid = SSUSED.ssid
    INNER JOIN MEMBERS AS MEMBERS_1 ON SS.memid = MEMBERS_1.memid
    INNER JOIN PRODUCTS ON SS.productid = PRODUCTS.productid
    INNER JOIN PRODUCTCATS ON PRODUCTS.productcatid = PRODUCTCATS.productcatid
    INNER JOIN EMPLOYEES ON SSUSED.employeeid = EMPLOYEES.employeeid
    WHERE
        MEMBERS_1.siteid = @rvSite
        AND SSUSED.usedate BETWEEN @rvStartWeek2 AND @rvEndWeek2
        AND PRODUCTS.productcatid IN (27, 28, 29, 58, 77, 75, 30, 61, 31, 32, 47))

SELECT DISTINCT 
    SS_1.memid,
    MEMBERS_1.scancode,
    MEMBERS_1.lname,
    MEMBERS_1.fname,
    PRODUCTS_1.description,
    EMPLOYEES_1.lname AS TrainLName,
    EMPLOYEES_1.fname AS TrainFName,
    SSUSED_1.usedate,
    SSUSED_1.transactiontype
FROM SS AS SS_1
INNER JOIN SSUSED AS SSUSED_1 ON SS_1.ssid = SSUSED_1.ssid
INNER JOIN MEMBERS AS MEMBERS_1 ON SS_1.memid = MEMBERS_1.memid
INNER JOIN PRODUCTS AS PRODUCTS_1 ON SS_1.productid = PRODUCTS_1.productid
INNER JOIN PRODUCTCATS AS PRODUCTCATS_1 ON PRODUCTS_1.productcatid = PRODUCTCATS_1.productcatid
INNER JOIN EMPLOYEES AS EMPLOYEES_1 ON SSUSED_1.employeeid = EMPLOYEES_1.employeeid
WHERE
    MEMBERS_1.siteid = @rvSite
    AND SSUSED_1.usedate BETWEEN @rvStartWeek1 AND @rvEndWeek1
    AND PRODUCTS_1.productcatid IN (27, 28, 29, 58, 77, 75, 30, 61, 31, 32, 47)
    AND NOT EXISTS
        (SELECT
            1 AS Expr1
        FROM cte AS cte_1
        WHERE SS_1.memid = memid
        AND cte_1.RowNumber = 1)
ORDER BY TrainLName, MEMBERS_1.scancode, SSUSED_1.usedate DESC

答案 2 :(得分:0)

使用窗口函数(trait KDoubles { def v: V[Double] def doIt = takesDoublesOrLists(v.a, v.b, /* ..., */ v.z) } trait KLists { def v: V[List[Double]] def doIt = takesDoublesOrLists(v.a, v.b, /* ..., */ v.z) } class K { // common stuff ... } class K1 extends K with KDoubles { // ... } class K2 extends K with KLists { // ... } COUNT OVER)查找您感兴趣的ROW_NUMBER OVER条记录。

SSUSED