【发布时间】:2019-11-22 11:45:54
【问题描述】:
我使用 SqlServer。我有一张有很多列的表格,其中重要的是:
· 用户名
· 分区 - xxxx-xx-xx 格式的日期
· 游戏 - 用作 ID 的字符串
· 学分 - 一个数字
· 下注 - 另一个号码
· 奖品 - 另一个号码
· Num_Spins - 另一个数字
我写了一个查询来选择给定特定日期我感兴趣的那些。
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')
我希望我能把它变成一个视图或其他东西,但不幸的是我没有这样做的特权。因此,我从中做了一个子查询:
我想从这些行中找出其数字符合某个数学结果的行:(Credits+Bet-Prize) > 100000 and num_spins>5
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;
现在,我得到了我需要的所有信息。我运行最后一个查询,以 group_by 日期这些结果,以便我可以分析它们:
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 个用户。如果我将这一行 (Credits+Bet-Prize) > 100000 and num_spins>5 更改为这一行 (Credits+Bet-Prize) > 100000(因此,我完全删除了 num_spins>5 部分),我将获得 16800 个用户。
总而言之,通过对查询进行更多限制,我可以获得更多结果。这对我来说没有意义。有人可以帮忙吗?带我去正确的方向还是什么?
谢谢
【问题讨论】:
-
底层数据变了?我们确实需要复制问题的样本数据。
-
添加更多限制不能增加结果集的大小。要么您对
WHERE子句所做的表述有误,要么您的基础数据正在发生变化。 -
嗯,您可能可以使用更严格的
WHERE获得更多结果如果存在某种聚合并添加了聚合列/删除到WHERE/HAVING。例如,一个客户列表花费了不到 1,000 英镑,然后一个客户列表在某些日期之间花费了不到 1,000 英镑。然而,在日期期间,查询的限制性更强,因为它的限制性更强,更多的客户不会花费了 1,000 英镑,因此会返回更多。不过,这不是你在这里做的, -
基础数据似乎是相同的,因为无论运行查询的时间或顺序如何,我都可以复制相同的结果。 BI 同事让我注意到第一个 group by 是不必要的,因为无论如何我都选择了所有列,所以我将编辑删除它的问题,但同样的情况发生
-
您的逻辑中有一些危险信号。在带有 IN 的子查询中使用 distinct 没有任何用处。在没有聚合的情况下在同一查询中使用 DISTINCT 和 GROUP BY 是您在编写逻辑正确的查询时遇到的另一个问题。游戏的数量被标记为 difmachines - 但是您不能在多个桌子/设备上玩同一个游戏吗?连接 user_name 和它的字节长度很奇怪,并且暗示了另一个 kludge。但是您知道某个特定的星期会给您带来可疑的数字 - 所以针对该星期运行查询以检查原始代码。
标签: sql-server group-by subquery where-clause