我有三张桌子
emp(id, name)
product(id, productname)
sales(id,emp_id,product_id,saleprice)
- 列出所有具有总销售额的员工。
- 获取销售额最高的员工。
注意:我不想使用加入和子查询,请建议我更好的方法。
我有三张桌子
emp(id, name)
product(id, productname)
sales(id,emp_id,product_id,saleprice)
注意:我不想使用加入和子查询,请建议我更好的方法。
我认为你可以这样做。但更好的方法是使用连接语句。但根据您的要求,您可以这样做。我认为这是最简单的方法和非常简单的查询。
我提供了创建表和示例数据插入查询。
这是您要求的答案
-- 1.List all the employee with total sales.
select distinct E.Id,E.Name,P.Productname,S.saleprice from Sales S,Emp E,Product P
where E.Id=S.Emp_id and P.Id=S.Product_id
-- 2.Fetch the employee with highest sales.
select S.Emp_id,E.Name,SUM(S.saleprice) AS TotalSalePrice from Sales S,Emp E
where E.Id=S.Emp_id
Group By S.Emp_id,E.Name
这是根据您的要求创建的表和示例数据插入查询
create table Emp
(
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] [nvarchar](100)
);
create table Product(
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Productname] [nvarchar](100)
);
create table Sales(
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Emp_id] [int] FOREIGN KEY REFERENCES Emp([Id]) ,
[Product_id] [int] FOREIGN KEY REFERENCES Product([Id]) ,
[saleprice] [int] NOT NULL
);
INSERT INTO [dbo].[Emp] ([Name]) VALUES('Jone Doe')
INSERT INTO [dbo].[Emp] ([Name]) VALUES('Micheal Oshea')
INSERT INTO [dbo].[Emp] ([Name]) VALUES('Ish Thalagala')
INSERT INTO [dbo].[Emp] ([Name]) VALUES('Mark Poull')
INSERT INTO [dbo].[Emp] ([Name]) VALUES('Janne Marker')
INSERT INTO [dbo].[Product]([Productname]) VALUES ('Coca Cola')
INSERT INTO [dbo].[Product]([Productname]) VALUES ('Pepsi')
INSERT INTO [dbo].[Product]([Productname]) VALUES ('Tooth Brush')
INSERT INTO [dbo].[Product]([Productname]) VALUES ('Water Filter')
INSERT INTO [dbo].[Product]([Productname]) VALUES ('Playstation 4 pro')
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(1,1,10)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(1,4,500)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(2,2,10)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(2,5,600)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(2,4,500)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(3,1,10)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(3,2,10)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(3,3,30)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(3,4,500)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(3,5,600)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(4,1,10)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(5,4,500)
INSERT INTO [dbo].[Sales]([Emp_id],[Product_id],[saleprice])VALUES(5,5,600)
如果您只能参考 emp_id 则根本不需要使用JOIN:
SELECT emp_id, SUM(salesprice) AS total_sales
FROM sales
GROUP BY emp_id
ORDER BY total_sales DESC -- comment 2 lines to get answer for 1 question
LIMIT 1; -- MySQL version for SQL Server use `SELECT TOP 1`
请注意,如果特定员工没有任何销售,那么它将被跳过。对于不存在的数据,您不会获得 0/NULL 值。
编辑:
如果您决定使用JOIN然后:
SELECT e.id, e.name, SUM(s.salesprice) AS total_sales
FROM emp e
LEFT JOIN sales s
ON e.id = s.emp_id
GROUP BY e.id, e.name
ORDER BY BY total_sales DESC LIMIT 1;