mysql - 需要查找购买的每种产品的销售地点数量吗?

标签 mysql sql hadoop hive hiveql

我有 2 个表(客户、交易)。

customer
id  name location
1    samuel AZ
2    mithun  CA
3    Robert  NY
etc..


transaction
id   customer_id  product_id
1     1            12000
2     1             12222
3     3             15000
etc.

有不同的产品和不同的位置

通过这些列,我需要找到购买某一特定产品的地点数量?

我尝试使用联接。

我的代码是

select c.id, c.location, t.product_id
from customer c join transaction t
on (c.id = t.cid);

我能够连接两个表,但我需要按 tid 计算事务数。

最佳答案

尝试对它们进行分组 - 这提供了购买特定产品的地点数量。这将包括位置的重复条目,但会回答您的交易数量位

select t.product_id, count(c.location)
from customer c join transaction t
on (c.id = t.cid)
group by 1;

关于mysql - 需要查找购买的每种产品的销售地点数量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46344730/

相关文章:

hadoop - 如何修改此配置单元查询以增加分配的 reducer 数量?

mysql - 使用 --skip-slave-start 启动 mysql

php - 如何在 php 的 sql 数据库中发生新插入时通知?

php - 这个数据库调用有什么问题?

mysql - 如何在 mysql 中的 varchar 中执行 SQL 插入

sql - mysql JOIN 中的 ON 和 WHERE 有什么区别?

hadoop - 当节点暂时与网络断开连接时,YARN中的应用程序/容器会发生什么情况

PHP 准备语句插入查询

java - hibernate 异常 : Missing Column (column exists)

hadoop - 发出同时运行两个Oozie协调器应用程序的问题