不同的员工姓名购买金额的总和?
我创建了这个查询。它返回超过 35000 条员工及其一年内购买记录。我试图让查询仅返回员工(姓名)的 1 个实例和总购买金额,而不是单独的记录。这可能吗?
SELECT
distinct employee.employee_no,
sum(employee_purchase.purchase_amount) as 'Purchase Amount',
--employee.employee_no,
employee.employee_first_name,
employee.employee_last_name,
employee.home_store_no,
employee_purchase.transaction_date,
employee_purchase.employee_discount_amount
FROM
employee
INNER JOIN
employee_purchase ON employee.employee_no =
employee_purchase.employee_no
where
transaction_date between ('2010-06-30 00:00:00') and ('2011-04-26 00:00:00')
group by employee.employee_no, employee.employee_first_name, employee.employee_last_name,
employee.home_store_no,
employee_purchase.transaction_date,
employee_purchase.employee_discount_amount
order by 1
I have this query I have created. It returns over 35000+ records for employees and their purchases for 1 year. I am trying to have the query return only 1 instance of the employee (name) and the total purchase amount instead of indivdual records. Is this possible?
SELECT
distinct employee.employee_no,
sum(employee_purchase.purchase_amount) as 'Purchase Amount',
--employee.employee_no,
employee.employee_first_name,
employee.employee_last_name,
employee.home_store_no,
employee_purchase.transaction_date,
employee_purchase.employee_discount_amount
FROM
employee
INNER JOIN
employee_purchase ON employee.employee_no =
employee_purchase.employee_no
where
transaction_date between ('2010-06-30 00:00:00') and ('2011-04-26 00:00:00')
group by employee.employee_no, employee.employee_first_name, employee.employee_last_name,
employee.home_store_no,
employee_purchase.transaction_date,
employee_purchase.employee_discount_amount
order by 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
摆脱你的 DISTINCT,并删除交易数据。想想看:如果您想对所有交易进行求和,您就不可能同时获得有关单个交易的信息。
Get rid of your DISTINCT, and remove the transaction data. Think about it: if you want to sum over all transactions, you cannot also have information about a single transaction.