The first question is relatively simple, check out the sales table, with sales greater than the average recorded in the region, with a sql statement to get.
Sales Table
OrderID |
Region |
Total |
1 |
A |
100.00 |
2 |
C |
80.00 |
3 |
A |
130.00 |
4 |
B |
90.00 |
5 |
B |
100.00 |
6 |
C |
120.00 |
7 |
A |
90.00 |
8 |
C |
90.00 |
9 |
B |
80.00 |
Sql语句:select * from sales as s inner join (select avg(total) as avge,region from sales group by region) avgtable on s.region = avgtable.region where total > avgtable.avge
The second question is more trouble, advertising on their company website is the carousel, a certain number of ads each day up advertising carousel is limited, such as A advertising, only three ads a day carousel , but the sales staff in the sales advertising does not consider this limit, requests for access to the contract table, more than the number of carousel advertising contract.
Contract table Orders
OrderID |
Positioncode |
Startdate |
Enddate |
1 |
A |
2006-11-01 |
2006-11-03 |
2 |
C |
2006-11-02 |
2006-11-03 |
3 |
B |
2006-11-01 |
2006-11-04 |
4 |
A |
2006-11-03 |
2006-11-04 |
5 |
C |
2006-11-01 |
2006-11-02 |
6 |
B |
2006-11-02 |
2006-11-05 |
7 |
A |
2006-11-02 |
2006-11-03 |
8 |
A |
2006-11-04 |
2006-11-05 |
9 |
C |
2006-11-03 |
2006-11-04 |
10 |
C |
2006-11-02 |
2006-11-04 |
Advertising table Product
Positioncode |
Showcount |
A |
2 |
B |
1 |
C |
3 |
Note: For advertising A is concerned, the following table carousel
OrderID |
2006-11-01 |
2006-11-02 |
2006-11-03 |
2006-11-04 |
2006-11-05 |
1 |
|||||
4 |
|||||
7 |
|||||
8 |
Slot A can rotate up to day 2 ad, but the contract table in 2006-11-03 three ads (day 1 , 4 , 7), for the ad slot A, the ultimate need is 1,4,7 results . As necessary, the use of temporary tables, stored procedures, and the like.
At that time it might be a little nervous, get this question when the interview more than two hours, still not resolved, eventually had to give up. But still did not give up, go home and then carefully study a little, and finally to solve, using stored procedures, but do not know there is no better way, over the next process.
proc overcontract Create
AS
DECLARE @mindate smalldatetime
DECLARE @days int
DECLARE @temptable Table (OrderID int)
set @mindate = (select min(startdate) from orders)
set @days = (select datediff(d,min(startdate),max(enddate)) from orders)
while (@days>-1)
begin
declare @curdate smalldatetime
set @curdate = dateadd(d,@days,@mindate)
insert into @temptable select o.orderid from product as p inner join
(select count(positioncode) as total,positioncode from orders where @curdate between startdate and enddate group by positioncode ) dt on dt.positioncode = p.positioncode left join orders o on o.positioncode = p.positioncode
where total>p.showcount and @curdate between startdate and enddate
set @days = @days-1
end
select distinct(orderid) from @temptable
go