如何计算总和大于x的数据
我对 SQL 非常陌生,正在使用 SQLite 3 对销售数据运行购物篮分析。
相关列是产品 ID、唯一交易 ID(标识购物篮)和产品数量。如果客户购买了多种产品类型,则重复唯一的交易 ID。
我想计算顾客购买了 1 件商品的篮子数量。
到目前为止,我已经尝试 select count(distinct uniqID) from salesdatahaving sum(qty) = 1;
但这引发了一个错误,指出 在 HAVING 之前需要 GROUP BY 子句
。
然后我尝试 select count(distinct uniqID) from salesdata group by uniqID has sum(qty) = 1
SQlite 接受了这一点,但返回了一个只有 1 的列表,这也是不对的!
然后我尝试 select count(uniqID) from salesdata group by qtyhaving sum(qty) = 1
SQlite 也接受了这一点,但什么也没返回。
任何想法将不胜感激!
乙
I am very new to SQL and am using SQLite 3 to run basket analysis on sales data.
The relevant columns are the product ID, a unique transaction ID (which identifies the basket) and the product quantity. Where a customer has bought more than one product type, the unqiue transaction ID is repeated.
I am wanting to count the number of baskets where the customer has bought 1 item.
So far I have tried select count(distinct uniqID) from salesdata having sum(qty) = 1;
But this brought up an error saying a GROUP BY clause is required before HAVING
.
I then tried select count(distinct uniqID) from salesdata group by uniqID having sum(qty) = 1
SQlite accepted this, but returned me a list of just 1s, which isn't right either!
I then tried select count(uniqID) from salesdata group by qty having sum(qty) = 1
SQlite also accepted this but returned nothing at all.
Any ideas would be hugely appreciated!
E
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试这样的操作来检索购物篮中拥有多于或等于一件商品的每个用户
如果您只想让购物篮中有 1 件商品的用户将
>=1
替换为, =1
例如:
如果您想了解购物篮中有 1 件商品的用户数量,您可以得到如下结果:
Try something like this to retrieve every user which has more or equal than one item in his basket
if you want to have only the users which have 1 item in their baskets replace
>=1
with=1
like:
If you want the numbers of users with 1 item in their baskets you get this like this:
选择仅包含一件商品的购物篮数量。这还将过滤掉数量高于一对一单品数量的购物篮。如果您不想这样做,请删除
WHERE qty = 1
部分。Selecting the number of baskets that have only one item. This will also filter out baskets with a quantity higher than one on one single item. If you don't want that, remove the
WHERE qty = 1
part.