MS Access SQL:不包括在计数中
MS Access SQL: Don't Include in Count
上下文:这是针对一家假设的汽车租赁公司。有两个 table 关注这个问题。它们被称为 'Staff Table' 和 'Sale Staff Member Table'。第一个 table 包括有关工作人员的信息,例如 DOB 等等。第二个table包括以下信息:
Staff_ID = 工作人员的唯一代码
Sale_ID = 特定销售的唯一代码
SaleStaffMember_AssociatedWithPurchase = 工作人员是否参与了最初的销售或只是 return 汽车。
两个table有相同的Staff_ID。
我写了以下SQL:
SELECT [staff table].staff_id as [Staff ID], Count([sale staff member table].staff_id) AS [Number of Rentals]
FROM [staff table]
LEFT JOIN [sale staff member table] ON [sale staff member table].staff_id = [staff table].staff_id
WHERE [sale staff member table].staff_id IS NULL or [sale staff member table].staff_id IS NOT NULL
GROUP BY [staff table].staff_id
它的作用是列出每个员工(有些员工没有进行任何销售,因此不在销售人员 table 中),并在他们旁边列出他们的销售额制作:
我的问题是,这里指的工作人员在技术上没有进行任何销售,但是当他应该销售 0 时,他被列为销售 1。我理解为什么,因为我没有写任何关于与SQL 中的购买字段。我试过这样做,但我能得到的最大结果是从查询结果中完全删除该成员。但是,我想做的基本上是说:
'If the staff member is not associated with the purchase, do not include this specific entry in the count'
但我不知道该怎么做。我尝试了一些荒谬的解决方案并进行了查找,但最终找不到解决方案。我敢肯定它相对简单,但是我这几天刚学SQL所以我还不知道。
编辑:刚刚意识到我的 where 语句实际上什么也没做哈哈,但这并没有解决任何问题,它只是让代码更整洁:
我修改后的代码:
SELECT [staff table].staff_id as [Staff ID], Count([sale staff member table].staff_id) AS [Number of Rentals]
FROM [staff table]
LEFT JOIN [sale staff member table] ON [sale staff member table].staff_id = [staff table].staff_id
GROUP BY [staff table].staff_id
谢谢!
编辑:最终答案
SELECT [Staff Table].Staff_ID AS [Staff ID], SSM.[Number of Rentals] AS [Number of Rentals]
FROM [staff table]
LEFT JOIN (SELECT Staff_ID, nz(COUNT(Staff_ID),0) as [Number of Rentals]
FROM [Sale Staff Member Table]
WHERE SaleStaffMember_AssociatedWithPurchase = 1
GROUP BY Staff_ID) AS SSM
ON [Staff Table].Staff_ID = SSM.Staff_ID
据我了解你的问题,如果你只是在寻找相关采购的员工,那么你可以简单地执行以下查询:
SELECT s.staff_id AS [Staff Id], COALESCE(ssm.NoOfRentals,0) AS [Number of Rentals]
FROM [staff table] AS s
LEFT JOIN (SELECT staff_id, COUNT(staff_id) as NoOfRentals
FROM [sale staff member table]
WHERE SaleStaffMember_AssociatedWithPurchase = 1
GROUP BY staff_id) AS ssm
ON s.staff_id = ssm.staff_id
如有错误请指正
向您的初始查询添加一个字段,例如
Sales:sum (iif(SaleStaffMember_AssociatedWithPurchase = 1,1,0))
这将在结果中保留所有员工,并汇总他们相关的销售数量。
上下文:这是针对一家假设的汽车租赁公司。有两个 table 关注这个问题。它们被称为 'Staff Table' 和 'Sale Staff Member Table'。第一个 table 包括有关工作人员的信息,例如 DOB 等等。第二个table包括以下信息:
Staff_ID = 工作人员的唯一代码 Sale_ID = 特定销售的唯一代码 SaleStaffMember_AssociatedWithPurchase = 工作人员是否参与了最初的销售或只是 return 汽车。
两个table有相同的Staff_ID。
我写了以下SQL:
SELECT [staff table].staff_id as [Staff ID], Count([sale staff member table].staff_id) AS [Number of Rentals]
FROM [staff table]
LEFT JOIN [sale staff member table] ON [sale staff member table].staff_id = [staff table].staff_id
WHERE [sale staff member table].staff_id IS NULL or [sale staff member table].staff_id IS NOT NULL
GROUP BY [staff table].staff_id
它的作用是列出每个员工(有些员工没有进行任何销售,因此不在销售人员 table 中),并在他们旁边列出他们的销售额制作:
我的问题是,这里指的工作人员在技术上没有进行任何销售,但是当他应该销售 0 时,他被列为销售 1。我理解为什么,因为我没有写任何关于与SQL 中的购买字段。我试过这样做,但我能得到的最大结果是从查询结果中完全删除该成员。但是,我想做的基本上是说:
'If the staff member is not associated with the purchase, do not include this specific entry in the count'
但我不知道该怎么做。我尝试了一些荒谬的解决方案并进行了查找,但最终找不到解决方案。我敢肯定它相对简单,但是我这几天刚学SQL所以我还不知道。
编辑:刚刚意识到我的 where 语句实际上什么也没做哈哈,但这并没有解决任何问题,它只是让代码更整洁:
我修改后的代码:
SELECT [staff table].staff_id as [Staff ID], Count([sale staff member table].staff_id) AS [Number of Rentals]
FROM [staff table]
LEFT JOIN [sale staff member table] ON [sale staff member table].staff_id = [staff table].staff_id
GROUP BY [staff table].staff_id
谢谢!
编辑:最终答案
SELECT [Staff Table].Staff_ID AS [Staff ID], SSM.[Number of Rentals] AS [Number of Rentals]
FROM [staff table]
LEFT JOIN (SELECT Staff_ID, nz(COUNT(Staff_ID),0) as [Number of Rentals]
FROM [Sale Staff Member Table]
WHERE SaleStaffMember_AssociatedWithPurchase = 1
GROUP BY Staff_ID) AS SSM
ON [Staff Table].Staff_ID = SSM.Staff_ID
据我了解你的问题,如果你只是在寻找相关采购的员工,那么你可以简单地执行以下查询:
SELECT s.staff_id AS [Staff Id], COALESCE(ssm.NoOfRentals,0) AS [Number of Rentals]
FROM [staff table] AS s
LEFT JOIN (SELECT staff_id, COUNT(staff_id) as NoOfRentals
FROM [sale staff member table]
WHERE SaleStaffMember_AssociatedWithPurchase = 1
GROUP BY staff_id) AS ssm
ON s.staff_id = ssm.staff_id
如有错误请指正
向您的初始查询添加一个字段,例如
Sales:sum (iif(SaleStaffMember_AssociatedWithPurchase = 1,1,0))
这将在结果中保留所有员工,并汇总他们相关的销售数量。