简体   繁体   English

如何在查询中显示以下数据?

[英]How do I display the following data in a query?

I have been trying to do the following exercise, which is to create a query using SQL, "Find number of salesmen who have more than 3 clients in a week. Display the staff ID, staff name, client ID, client name and number of clients who the salesman met with." 我一直在尝试执行以下练习,即使用SQL创建查询:“查找一周内拥有3个以上客户的销售人员数量。显示人员ID,人员名称,客户ID,客户名称和推销员遇到的客户。”

The entities and relevant fields are: 实体和相关字段是:

Staff:   [Staff_ID (PK), Staff_Name, ...]
Clients: [Client_ID (PK), Client_Name, Staff_ID (FK), ...]
Sales:   [Sale_ID (PK), Client_ID (FK), Staff_ID (FK), Date_of_Sale, ...]
(All data are strings except Date_of_Sale, which is in DATE format)

I've tried to accomplish this, with the following: 我尝试通过以下方法实现此目的:

SELECT t.Staff_ID, t.Staff_Name, COUNT(s.Client_ID), 
COUNT(c.Client_Name), DATEPART(wk, s.Date_of_Sale) as Week
FROM Clients c, Staff t, Sales s
GROUP BY DATEPART(wk, s.Date_of_Sale), t.Staff_ID, t.Staff_Name
HAVING COUNT(DATEPART(wk, s.Date_of_Sale)) > 3

I know it's incorrect, but I don't know how to fix it, as I am very unskilled in sql. 我知道它是不正确的,但是我不知道如何解决它,因为我对SQL非常不熟练。 I want to understand how to do this and I could (to a limited extent) by reverse engineering it. 我想了解如何执行此操作,并且可以(在一定程度上)对它进行反向工程。 Even better, if someone were to explain it for me, I would much appreciate it, thanks in advance. 更好的是,如果有人为我解释它,我将不胜感激,在此先感谢。

Here we go 开始了

WITH CTE AS (
    SELECT *, DATEPART(wk, s.Date_of_Sale) as SALES_WEEK
    FROM Clients c
    INNER JOIN Sales s ON c.Client_ID = s.CLIENT_ID
    INNER JOIN Staff t ON t.STAFF_ID = s.STAFF_ID
)

SELECT SALES_WEEK, Staff_ID, Staff_Name, COUNT(1) AS NO_OF_SALES
FROM CTE
GROUP BY SALES_WEEK, Staff_ID, Staff_Name
HAVING COUNT(1) > 3

Hope this works as expected 希望这能按预期工作

There's a big difference between number of clients who the salesman met with and the number of sales the salesman made . 推销员遇到的客户 数量与推销员进行的销售数量之间存在很大差异。 Mainly, whether or not to count repeat visits to the same client. 主要是,是否计算对同一客户的重复访问。 The former says no, the latter says yes, because the number of clients hasn't changed but the number of sales has. 前者说不,后者说是,因为客户数量没有变化,但是销售数量却有变化。

-- Get list of clients with sales greater than 3 in a week
WITH SalesCount AS (
    SELECT Client_ID AS 'Client', DATEPART(wk, Date_of_Sale) AS 'week', 
        COUNT(Staff_ID) AS 'Sales_Count'
    FROM Sales
    GROUP BY DATEPART(wk, Date_of_Sale), Client_ID
    HAVING COUNT(Staff_ID) > 3  ),
-- Shorten the list to unique entries and add the StaffID
ClientList AS (
    SELECT DISTINCT sc.Client, c.Staff_ID
    FROM SalesCount AS sc
    INNER JOIN Clients AS c ON sc.Client = c.Client_ID) ,
-- Create a count of clients visited
ClientCount AS (
    SELECT COUNT(l.Client) AS visitors, l.Staff_ID
    FROM ClientList AS l
    GROUP BY l.Staff_ID)

SELECT cc.visitors, cc.Staff_ID, l.Client, t.Staff_Name
FROM ClientCount AS cc
INNER JOIN ClientList AS l ON cc.Staff_ID = l.Staff_ID
INNER JOIN Staff AS t ON t.Staff_ID = l.Staff_ID 
INNER JOIN Clients AS c ON l.Client = c.Client_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM