选择用户每年购买的地方

时间:2014-08-18 13:42:14

标签: sql sql-server-2008

我有一个sales表,我在其中存储每个销售。此表包含year_ordereduserIdorderId等列。

我希望编写一个SQL查询来选择行,用户每年从2008年开始订购。 所以我只想要那些从2008年到2014年一直忠诚和订购的人。

我已尝试使用此查询,但它为我提供了year_ordered大于2007的任何内容 -

select COUNT(*) as sales_count, ss.userID, ss.year_ordered 
from subscriber_sub ss
where ss.date_deleted is null
and ss.year_ordered > 2007
group by ss.year_ordered, ss.userID
having  COUNT(*) > 1 
order by ss.year_ordered

5 个答案:

答案 0 :(得分:4)

你所追求的是关系分裂。基本上有两种方法可以实现这一目标:

select COUNT(distinct ss.year_ordered) as sales_count, ss.userID 
from subscriber_sub ss
where ss.date_deleted is null
  and ss.year_ordered > 2007
group by ss.userID
having  COUNT(distinct ss.year_ordered) >= ( select 2014 - 2008 )  

另一种方法是重写FORALL x:p(x)< =>不存在x:不是p(x),即一年不存在的用户,因此当年没有销售。我将其留作练习: - )

答案 1 :(得分:2)

请尝试使用HAVING子句:

HAVING (SELECT COUNT(DISTINCT ss.year_ordered)) = 7

答案 2 :(得分:1)

这应该列出从2007年开始购买多件物品的用户的订户记录......

select s2.* 
from subscriber_sub s2
where s2.year_ordered > 2007
and s2.userID in
(
    select ss.userID
    from subscriber_sub ss
    where ss.date_deleted is null
    and ss.year_ordered > 2007
    group by ss.userID
    having  COUNT(*) > 1 
)

答案 3 :(得分:1)

这应该是动态的,即保持计算用户必须根据当前日期时间进行订购的年数:

DECLARE @subscriber_sub TABLE (
    userId INT,
    year_ordered INT,
    date_deleted DATE);
INSERT INTO @subscriber_sub VALUES (1, 2007, NULL);
INSERT INTO @subscriber_sub VALUES (1, 2008, NULL);
INSERT INTO @subscriber_sub VALUES (1, 2009, NULL);
INSERT INTO @subscriber_sub VALUES (1, 2010, NULL);
INSERT INTO @subscriber_sub VALUES (1, 2011, NULL);
INSERT INTO @subscriber_sub VALUES (1, 2012, NULL);
INSERT INTO @subscriber_sub VALUES (1, 2013, NULL);
INSERT INTO @subscriber_sub VALUES (1, 2014, NULL);
INSERT INTO @subscriber_sub VALUES (2, 2007, NULL);
INSERT INTO @subscriber_sub VALUES (2, 2008, NULL);
INSERT INTO @subscriber_sub VALUES (2, 2009, NULL);
INSERT INTO @subscriber_sub VALUES (2, 2010, NULL);
INSERT INTO @subscriber_sub VALUES (2, 2011, NULL);
INSERT INTO @subscriber_sub VALUES (2, 2012, NULL);
INSERT INTO @subscriber_sub VALUES (2, 2013, NULL);

WITH YearsOrdered AS (
    SELECT
        userId,
        COUNT(DISTINCT year_ordered) AS years
    FROM
        @subscriber_sub
    WHERE
        year_ordered > 2007
        AND date_deleted IS NULL
    GROUP BY
        userId)
SELECT 
    ss.userID,
    ss.year_ordered,
    COUNT(*) AS sales_count
FROM 
    @subscriber_sub ss
    LEFT JOIN YearsOrdered yo ON yo.userId = ss.userId
WHERE 
    ss.date_deleted IS NULL
    AND ss.year_ordered > 2007
    AND yo.years = DATEDIFF(YEAR, '20070101', GETDATE())
GROUP BY 
    ss.year_ordered,
    ss.userID
ORDER BY 
    ss.year_ordered,
    ss.userId;

当我测试时,用户#1被报告但用户#2不是他们在2014年没有订购的。

答案 4 :(得分:1)

如果您只需要了解用户,那么您需要选择userId,其中year_ordered个值= 7(从2008年到2014年,包括):

select COUNT(*) as sales_count, ss.userID
from subscriber_sub ss
where ss.date_deleted is null
and ss.year_ordered > 2007
group by ss.userID
having  COUNT( DISTINCT ss.year_ordered) = 7