Sql server 在WHERE子句中添加条件会得到更多结果

Sql server 在WHERE子句中添加条件会得到更多结果,sql-server,group-by,subquery,where-clause,Sql Server,Group By,Subquery,Where Clause,我使用SqlServer。我有一个包含许多列的表,其中重要的是: ·用户名 ·分区-xxxx xx格式的日期 ·游戏-用作ID的字符串 ·学分-一个数字 ·打赌-另一个数字 ·奖品-另一个号码 ·Num_旋转-另一个数字 我写了一个查询,在给定日期的情况下选择我感兴趣的那些 Select distinct CONCAT(User_Name, DATALENGTH(User_Name)) as User_name, Partition, Game, Bet, Num_spins, Credits,

我使用SqlServer。我有一个包含许多列的表,其中重要的是:

·用户名

·分区-xxxx xx格式的日期

·游戏-用作ID的字符串

·学分-一个数字

·打赌-另一个数字

·奖品-另一个号码

·Num_旋转-另一个数字

我写了一个查询,在给定日期的情况下选择我感兴趣的那些

Select distinct CONCAT(User_Name, DATALENGTH(User_Name)) as User_name, Partition, Game, Bet, Num_spins, Credits, Prize
            from ***
            where Partition>='2019-09-01' and Partition<'2019-11-17' and Bet>0 and credits is not null
            and User_Name IN (Select distinct userName from *** where GeoIpCountryCode='ES')
现在,我得到了我需要的所有信息。我运行最后一个查询,按日期对这些结果进行分组,以便分析它们:

Select  datepart(week,Partition) as Week, count (distinct user_name) as Users
    from (
        Select user_name, partition, count(Game) as difMachines
        FROM
        (
            Select distinct CONCAT(User_Name, DATALENGTH(User_Name)) as User_name, Partition, Game, Bet, Num_spins, Credits, Prize
            from ***
            where Partition>='2019-09-01' and Partition<'2019-11-17' and Bet>0 and credits is not null
            and User_Name IN (Select distinct userName from *** where GeoIpCountryCode='ES')
        ) as A
        where 
        (Credits+Bet-Prize) > 100000 and num_spins>5
        group by User_Name, Partition
    ) as B
    Where difMachines=1
    group by datepart(week,Partition)
    order by Week asc;
我知道查询可以优化,但这不是困扰我的问题。问题是,在运行此查询时,我在第36周获得了17050个用户。如果我更改这一行的积分+赌注>100000,并且这一行的积分+赌注>100000的num_spins>5,那么我只删除num_spins>5部分,取而代之的是16800个用户

总而言之,我通过在查询中更严格地限制来获得更多的结果。这对我来说毫无意义。有人能帮忙吗?带我去正确的方向还是别的什么


谢谢

您正在尝试使用此筛选器diffmachine=1获取结果集的计数,不是吗?。但是如果你移除过滤器num_spins>5,那么当diffmachine大于1时,计数将增加。这里我举一个类似你的例子

Declare  @t table
(
    [user_name] varchar(5), [partition] date, Game varchar(10),num_spins int
)

insert into @t
select 'a','01nov19','g1',1
union all
select 'a','01nov19','g1',2
union all
select 'a','01nov19','g1',3
union all
select 'a','01nov19','g1',4
union all
select 'a','01nov19','g1',5
union all
select 'a','01nov19','g1',6
union all
select 'b','01nov19','g1',7

select * from
(
    select [user_name],[partition],count(game) cnt
    from  @t
    where num_spins>5
    group by [user_name],[partition]
)a
where cnt=1

基础数据已更改?我们确实需要复制问题的样本数据。添加更多限制不能增加结果集的大小。可能是您对WHERE子句的表述错误,或者您的基础数据正在更改。嗯,如果存在某种聚合,并且该聚合列被添加/删除到WHERE/HAVING,您可能会通过更严格的WHERE得到更多结果。例如,列出花费低于1000英镑的客户,然后列出在特定日期之间花费低于1000英镑的客户。然而,在日期期间,查询更具限制性,因为它更具限制性,更多的客户不会花费1000英镑,因此会返回更多。但这并不是您在这里要做的,底层数据似乎是相同的,因为无论运行查询的时间或顺序如何,我都可以复制相同的结果。一位BI同事让我注意到,第一个group by是不必要的,因为我选择了所有列,所以我将编辑问题并删除它,但同样的情况也会出现在您的逻辑中的一些危险信号。在带有in的子查询中使用distinct没有任何用处。在同一查询中使用DISTINCT和GROUP BY而不使用aggregrate是编写逻辑正确的查询时需要考虑的另一个问题。游戏计数被标记为difmachines,但您不能在多个桌子/设备上玩同一个游戏吗?将用户名和它的长度(以字节为单位)连在一起很奇怪,这意味着另一个难题。但是你知道一个特定的星期会给你可疑的数字,所以对那个星期运行查询来检查原始代码。就是这样,我用一个不同的difMachines编号检查了它,从一个子集中删除的编号被放到了另一个子集中。非常感谢。
Declare  @t table
(
    [user_name] varchar(5), [partition] date, Game varchar(10),num_spins int
)

insert into @t
select 'a','01nov19','g1',1
union all
select 'a','01nov19','g1',2
union all
select 'a','01nov19','g1',3
union all
select 'a','01nov19','g1',4
union all
select 'a','01nov19','g1',5
union all
select 'a','01nov19','g1',6
union all
select 'b','01nov19','g1',7

select * from
(
    select [user_name],[partition],count(game) cnt
    from  @t
    where num_spins>5
    group by [user_name],[partition]
)a
where cnt=1