创建一个查询,显示从某个日期到 "USA" 的客户最近 3 天没有销售的员工。罗斯文数据库
Create a query that displays the employees with no sale in the last 3 days from a date to customers who are from "USA". northwind database
我想把在 1997-01-25 之前的最后 3 天内没有销售的员工给来自 "USA".
的客户
订单table
OrderID CustomerName EmployeeId Orderdate
10248 C1 1 1997-01-25 00:00:00:000
10249 C2 3 1997-0-24 00:00:00:000
10250 C3 2 1997-01-23 00:00:00:000
10251 c4 5 1997-01-22 00:00:00:000
10251 c5 4 1997-01-23 00:00:00:000
员工
EmployeeID Emp_name
1 E1
2 E2 and so on till e5
客户
CustomerID Country
C1 USA
C2 UK
C3 Brazil
C4 UK
C5 USA
想要的结果
EmployeeID
2
3
5
已尝试查询
select EmployeeID
from Employees e1
where employeeid not in
(
select o.EmployeeID
from orders o
full join customers c on o.CustomerID=c.CustomerID
where c.Country = 'USA'
and o.RequiredDate >= dateadd(day,-3, '1997-01-25')
)
- 共有 e1 到 e5 名员工。
- e1 于 1997 年 1 月 25 日为客户 c1 提供服务。 c1 来自美国
- e4 于1997 年1 月23 日(1997 年1 月25 日前3 天内)为客户c4 提供服务,c4 来自美国。
- 因此从员工总列表中获取不在 e1 和 e4 中的员工列表。
但是我得到的结果是空白,因为没有行。谁能帮忙。 (这只是一个样本):来自northwind 数据库的数据。我刚刚创建了示例数据以防有人没有 northwind 数据库
SELECT DISTINCT Employees.Emp_name
FROM Employees
LEFT JOIN Orders ON Orders.EmployeeID = Employees.EmployeeID AND Orders.Orderdata BETWEEN '1997-01-23' AND '1997-01-25'
LEFT JOIN Customers ON Customers.CustomerID = Orders.CustomerID AND Customers.Country = 'USA'
AND Customers.CustomerID IS NULL
完全基于您 "provided" 的 table 数据,问题是您正在寻找整数的员工 ID,而您的子查询正在返回员工姓名列表。所以如果你这样修改它,你会得到正确的答案:
CREATE TABLE #Orders (OrderID int, CustomerName varchar(20), EmployeeName varchar(20), Orderdate datetime)
INSERT INTO #Orders VALUES
(10248, 'C1', 'E1', '1997-01-25 00:00:00:000'),
(10249, 'C2', 'E3', '1997-01-24 00:00:00:000'),
(10250, 'C3', 'E2', '1997-01-23 00:00:00:000'),
(10251, 'c4', 'e5', '1997-01-22 00:00:00:000'),
(10251, 'c5', 'e4', '1997-01-23 00:00:00:000')
CREATE TABLE #Employees (EmployeeID int, Emp_name varchar(10))
INSERT INTO #Employees VALUES
(1, 'E1'),
(2, 'E2'),
(3, 'E3'),
(4, 'E4'),
(5, 'E5')
CREATE TABLE #Customers (CustomerID varchar(10), Country varchar(20))
INSERT INTO #Customers VALUES
('C1', 'USA'),
('C2', 'UK'),
('C3', 'Brazil'),
('C4', 'UK' ),
('C5', 'USA')
select Emp_name
from #Employees e1
where Emp_name not in
(
select o.EmployeeName
from #orders o
inner join #customers c on o.CustomerName = c.CustomerID
where c.Country = 'USA'
and o.Orderdate >= dateadd(day,-3, '1997-01-25')
)
我想把在 1997-01-25 之前的最后 3 天内没有销售的员工给来自 "USA".
的客户订单table
OrderID CustomerName EmployeeId Orderdate
10248 C1 1 1997-01-25 00:00:00:000
10249 C2 3 1997-0-24 00:00:00:000
10250 C3 2 1997-01-23 00:00:00:000
10251 c4 5 1997-01-22 00:00:00:000
10251 c5 4 1997-01-23 00:00:00:000
员工
EmployeeID Emp_name
1 E1
2 E2 and so on till e5
客户
CustomerID Country
C1 USA
C2 UK
C3 Brazil
C4 UK
C5 USA
想要的结果
EmployeeID
2
3
5
已尝试查询
select EmployeeID
from Employees e1
where employeeid not in
(
select o.EmployeeID
from orders o
full join customers c on o.CustomerID=c.CustomerID
where c.Country = 'USA'
and o.RequiredDate >= dateadd(day,-3, '1997-01-25')
)
- 共有 e1 到 e5 名员工。
- e1 于 1997 年 1 月 25 日为客户 c1 提供服务。 c1 来自美国
- e4 于1997 年1 月23 日(1997 年1 月25 日前3 天内)为客户c4 提供服务,c4 来自美国。
- 因此从员工总列表中获取不在 e1 和 e4 中的员工列表。
但是我得到的结果是空白,因为没有行。谁能帮忙。 (这只是一个样本):来自northwind 数据库的数据。我刚刚创建了示例数据以防有人没有 northwind 数据库
SELECT DISTINCT Employees.Emp_name
FROM Employees
LEFT JOIN Orders ON Orders.EmployeeID = Employees.EmployeeID AND Orders.Orderdata BETWEEN '1997-01-23' AND '1997-01-25'
LEFT JOIN Customers ON Customers.CustomerID = Orders.CustomerID AND Customers.Country = 'USA'
AND Customers.CustomerID IS NULL
完全基于您 "provided" 的 table 数据,问题是您正在寻找整数的员工 ID,而您的子查询正在返回员工姓名列表。所以如果你这样修改它,你会得到正确的答案:
CREATE TABLE #Orders (OrderID int, CustomerName varchar(20), EmployeeName varchar(20), Orderdate datetime)
INSERT INTO #Orders VALUES
(10248, 'C1', 'E1', '1997-01-25 00:00:00:000'),
(10249, 'C2', 'E3', '1997-01-24 00:00:00:000'),
(10250, 'C3', 'E2', '1997-01-23 00:00:00:000'),
(10251, 'c4', 'e5', '1997-01-22 00:00:00:000'),
(10251, 'c5', 'e4', '1997-01-23 00:00:00:000')
CREATE TABLE #Employees (EmployeeID int, Emp_name varchar(10))
INSERT INTO #Employees VALUES
(1, 'E1'),
(2, 'E2'),
(3, 'E3'),
(4, 'E4'),
(5, 'E5')
CREATE TABLE #Customers (CustomerID varchar(10), Country varchar(20))
INSERT INTO #Customers VALUES
('C1', 'USA'),
('C2', 'UK'),
('C3', 'Brazil'),
('C4', 'UK' ),
('C5', 'USA')
select Emp_name
from #Employees e1
where Emp_name not in
(
select o.EmployeeName
from #orders o
inner join #customers c on o.CustomerName = c.CustomerID
where c.Country = 'USA'
and o.Orderdate >= dateadd(day,-3, '1997-01-25')
)