Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 统计每个客户在6个月内出现的特定代码_Sql_Tsql_Sql Server 2012 - Fatal编程技术网

Sql 统计每个客户在6个月内出现的特定代码

Sql 统计每个客户在6个月内出现的特定代码,sql,tsql,sql-server-2012,Sql,Tsql,Sql Server 2012,我有一个表,其中包含以下内容: customerid | date (dmy) | productid John | 1-3-14 | A John | 7-5-14 | Y John | 8-5-14 | Y John | 1-10-15 | B John | 1-11-15 | Y Pete | 1-7-15 |

我有一个表,其中包含以下内容:

customerid | date (dmy) | productid

   John    |    1-3-14    |     A
   John    |    7-5-14    |     Y
   John    |    8-5-14    |     Y
   John    |    1-10-15   |     B
   John    |    1-11-15   |     Y
   Pete    |    1-7-15    |     Y
我需要了解客户X在六个月内购买产品Y的频率

周期的开始时间定义为客户第一次购买a、B、C或Y中的一种产品。周期的结束时间正好是之后的六个月

当客户再次购买产品A、B、C或Y时,下一个周期开始

所以输出应该是

customerid | period-start | period-end | countofY
   John    |    1-3-14    |   8-5-14   |    2
   John    |    1-10-15   |   1-11-15  |    1
   Pete    |    1-7-15    |   1-7-15   |    1

您的示例结果与您的要求不匹配。约翰一共买了3个Y。第一个开始日期是7/5,为什么周期开始日期是1/3?难道周期结束时间不比周期开始时间+6个月短1秒,而下一个周期开始时间不比周期开始时间+6个月短1秒吗?John在2014年3月1日购买了产品A,所以这是第一个6个月周期开始的时间。该期限持续至1-9-14+6个月,该期限内的最后日期为8-5-14。在那个时期,他买了两次产品Y。我的错是DMY不是MDY。我们需要找到产生结果的优化方法。但据我所知,上述问题会给你理想的答案。我们也可以使用游标来获得理想的答案。
;WITH CTE_DateRanges AS (
    SELECT
        customerid,
        productid,
        MIN(purchase_date) AS period_start,
        DATEADD(MM, 6, MIN(purchase_date)) AS period_end
    FROM
        My_Table
    GROUP BY
        customerid,
        productid
)
SELECT
    DR.customerid,
    DR.productid,
    DR.period_start,
    DR.period_end,
    COUNT(*)
FROM
    CTE_DateRanges DR
INNER JOIN My_Table MT ON
    MT.customerid = DR.customerid AND
    MT.productid = DR.productid AND
    MT.purchase_date BETWEEN DR.period_start AND DR.period_end
GROUP BY
    DR.customerid,
    DR.productid,
    DR.period_start,
    DR.period_end,
SELECT c.Customerid, MIN(c.pdate) AS startperiod, c1.endperiod, 
(
    SELECT COUNT(temp.productid) FROM Customer temp 
    WHERE temp.Customerid = c.Customerid
    AND temp.pdate >= MIN(c.pdate)
    AND temp.pdate <= c1.endperiod 
    GROUP BY temp.productid HAVING temp.productid ='Y'
)AS countOfY 
FROM Customer c 
CROSS APPLY
(
    SELECT TOP 1 c1.pdate AS endperiod  
    FROM Customer c1 
    WHERE c1.Customerid = c.Customerid
    AND c1.pdate >= c.pdate
    AND 
    (
        DATEDIFF(MONTH, c.pdate, c1.pdate) < 6  
        OR 
        (
            SELECT TOP 1 t.pdate FROM Customer t
            WHERE t.Customerid = c.Customerid
            AND t.pdate < c1.pdate
        ) IS NULL       
    )   
    ORDER BY c1.pdate DESC
)AS c1 GROUP BY c1.endperiod, c.Customerid