不同的员工姓名购买金额的总和?

发布于 2024-11-03 10:24:31 字数 808 浏览 0 评论 0原文

我创建了这个查询。它返回超过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏雨凉 2024-11-10 10:24:31

摆脱你的 DISTINCT,并删除交易数据。想想看:如果您想对所有交易进行求和,您就不可能同时获得有关单个交易的信息。

SELECT
  employee.employee_no,
  sum(employee_purchase.purchase_amount) as 'Purchase Amount',
  employee.employee_first_name,
  employee.employee_last_name,
  employee.home_store_no
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
order by 1 

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.

SELECT
  employee.employee_no,
  sum(employee_purchase.purchase_amount) as 'Purchase Amount',
  employee.employee_first_name,
  employee.employee_last_name,
  employee.home_store_no
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
order by 1 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文