SQL statement exercise Example 4: Find the employee with the highest sales volume in the promotion

Source: Internet
Author: User

Copy codeThe Code is as follows:
--- Locate the employees with the highest sales volume during the promotion
--- You have just found a job in a clothing sales company. At this time, the manager asks you to obtain the salesperson with the highest sales volume based on the two tables in the database.
--- 1. A promotion table
--- 2. One is the sales customer list
Create table Promotions
(
Activity nvarchar (30 ),
Sdate datetime,
Edate datetime
)
Insert Promotions
Select '51 promotions ', '2017-5-1', '2017-5-7'
Union
Select '11 promotions ', '2017-10-1', '2017-10-7'
Union
Select 'oa session activity', '2017-6-1 ', '2017-6-7'
Go
Create table sales
(
Id int not null,
Name nvarchar (20 ),
Saledate datetime,
Price money
)
Go
Insert sales
Select 1, 'wang 5', '2017-5-1 ', 2011 union
Select 1, 'wang 5', '2017-5-2 ', 2011 union
Select 1, 'wang 5', '2014-5-3 ', 2011 union
Select 1, 'wang 5', '2014-5-4 ', 2011 union
Select 1, 'zhang san', '2017-5-1 ', 2011 union
Select 1, 'zhang san', '2014-5-3 ', 2011 union
Select 1, 'zhang san', '2014-5-4 ', 2011 union
Select 1, 'lily', '2014-5-6 ', 2011 union
Select 1, 'zhao liu', '2017-5-5 ', 2011 union
Select 1, 'money 7', '2014-5-8 ', 2011 union

Select 1, 'Sun 5', '2017-6-1 ', 2011 union
Select 1, 'Sun 5', '2017-6-2 ', 2011 union
Select 1, 'wang 5', '2017-6-3 ', 2011 union
Select 1, 'Sun 5', '2017-6-4 ', 2011 union
Select 1, 'zhang san', '2017-6-1 ', 2011 union
Select 1, 'zhang san', '2017-6-3 ', 2011 union
Select 1, 'zhang san', '2017-6-4 ', 2011 union
Select 1, 'lily', '2017-6-6 ', 2011 union
Select 1, 'zhao liu', '2017-6-5 ', 2011 union
Select 1, 'money 7', '2017-6-8 ', 2011 union

Select 1, 'Sun 5', '2017-10-1 ', 2011 union
Select 1, 'Sun 5', '2017-10-2 ', 2011 union
Select 1, 'wang 5', '2017-10-3 ', 2011 union
Select 1, 'Sun 5', '2017-10-4 ', 2011 union
Select 1, 'zhang san', '2017-10-1 ', 2011 union
Select 1, 'zhang san', '2017-10-3 ', 2011 union
Select 1, 'zhang san', '2017-10-4 ', 2011 union
Select 1, 'lily', '2017-10-6 ', 2011 union
Select 1, 'zhao liu', '2017-10-5 ', 2011 union
Select 1, 'money 7', '2017-10-8 ', 2011
Go
----- We need to find that the total sales volume is greater than or equal
--- All sales of other employees and promotions.
--- Note: predicate a2.name <> a. name will exclude other employees from the total subquery.
--------- The subquery in the predicate Between ensures that we use the correct promotion date

-- Method 1:
Select a. name, B. activity, SUM (a. price) as totalprice
From sales a, Promotions as B
Where a. saledate between B. sdate and B. edate
Group by a. name, B. activity
Having SUM (price)> = all (select SUM (price) from sales a2
Where a2.name <> a. name and a2.saledate
(
Select sdate from Promotions as b2 where b2.activity = B. activity
)
And (select edate from Promotions b3
Where b3.activity = B. activity)
Group by a2.name)
-----------------

--- Method 2:
--- Note: If the promotion time does not overlap, the promotions table has only one primary key column.
-- The usage in the clause (activity, sdate, edate) will not change. However, it enables the having clause to use sdate and edate.
Select a. name, B. activity, SUM (a. price) as totalprice
From sales a, Promotions as B
Where a. saledate between B. sdate and B. edate
Group by B. activity, B. sdate, B. edate, a. name
Having SUM (price)> = all (select SUM (price) from sales a2
Where a2.name <> a. name and a2.saledate
B. sdate
And B. edate
Group by a2.name)

Go

-- Method 3:
--- Use cte (SQL 2005 and later versions)
With clearksTotal (name, activity, totalprice)
(
Select a. name, B. activity, SUM (price)
From sales a, Promotions B
Where a. saledate between B. sdate and B. edate
Group by a. name, B. activity
)

Select c1.name, c1.activity, c1.totalprice
From clearksTotal c1
Where totalprice = (select MAX (c2.totalprice) from clearksTotal c2
Where c1.activity = c2.activity)
Go
Drop table Promotions
Go
Drop table sales

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.