SQL从不同的表中选择行

时间:2017-06-25 20:56:45

标签: sql

我有以下三个表,其中包含SQL数据库中列出的列。

Person: PersonID, FirstName, LastName ,state,city

Email: EmailID, Email, PersonID

Order: OrderID,EmailID,Order_amt, order_date

我想要一个查询,选择50个已下订单> $ 50的顶级人员,来自CA或OR或WA,并在当前月份内下订单最少5次,按order_amt排序

2 个答案:

答案 0 :(得分:0)

应该像这样工作。我只是在线使用我的手机,所以我还没有能够测试它。

 SELECT TOP 50 Person.*, orders.*
  FROM Person
 INNER JOIN Email
 ON Person.PersonID = Email.PersonID
 INNER JOIN
  (
    SELECT EmailID, SUM (Order_amt) as TotalAmt
        FROM Order 
  WHERE Order_amt > 50
    AND order_date >= DATEADD(MONTG, -1, GETDATE())
   GROUP BY EmailID
    HAVING COUNT (*) >= 5 
) orders
ON orders.EmailID = Email.EmailID
 WHERE Person.State IN ('CA','OR','WA')
 ORDER BY orders.TotalAmt DESC

答案 1 :(得分:0)

这与Esteban的答案类似,但我添加了一些样本数据(因此减少了计数限制,如图所示)。目前尚不清楚50限额是单次购买,上个月或终身购买的总和,所以我给了两个不同的版本。埃斯特班和我都假设了#34;当月和#34;正在滚动。如果没有,那么您需要更改日期限制。 BTW这适用于SQL Server。请注意,所有dbs都略有不同,因此请在您的问题的未来状态中使用您正在使用的数据库。如果你自己做一些尝试,人们也会喜欢它!

declare @person table
(
personid int,
firstname varchar(20),
lastname varchar(20),
[state] char(2),
city varchar(20)
);
declare @email table
(
emailid int,
email varchar(30),
personid int
);
declare @order table
(
orderid int,
emailid int,
order_amt decimal,
order_date datetime
);

INSERT INTO @person VALUES(1, 'Mickey','Mouse','CA','Los Angeles');
INSERT INTO @person VALUES(2, 'Donald','Duck','CA','San Francisco');
INSERT INTO @person VALUES(3, 'Roger','Rabbit','WA','Seattle');
INSERT INTO @person VALUES(4, 'Loadsa','Money','NY','New York');

INSERT INTO @email VALUES(1, 'm.mouse@disney.com', 1);
INSERT INTO @email VALUES(2, 'd.duck@disney.com', 2);
INSERT INTO @email VALUES(3, 'r.rabbit@buenavista.com', 3);
INSERT INTO @email VALUES(4, 'v.rich@wallstreet.com', 4);

INSERT INTO @order VALUES (1, 1, 34.20, '2017-04-15');
INSERT INTO @order VALUES (2, 1, 22.25, '2017-05-18');
INSERT INTO @order VALUES (3, 1, 62.30, '2017-06-18');
INSERT INTO @order VALUES (4, 2, 17.20, '2017-06-02');
INSERT INTO @order VALUES (5, 2, 28.40, '2017-06-20');
INSERT INTO @order VALUES (6, 2, 85.60, '2017-06-22');
INSERT INTO @order VALUES (7, 3, 10.30, '2017-06-03');
INSERT INTO @order VALUES (8, 3, 10.70, '2017-06-24');
INSERT INTO @order VALUES (9, 3, 42.60, '2017-05-25');
INSERT INTO @order VALUES (10, 4, 890.79, '2017-06-03');
INSERT INTO @order VALUES (11, 4, 1800.22, '2017-06-24');
INSERT INTO @order VALUES (12, 4, 2000.00, '2017-06-25');
INSERT INTO @order VALUES (13, 3, 20.30, '2017-06-03');

--Version 1 Lifetime 50 order_amt limit
SELECT TOP 2 Count(o.orderid) as LastMonthBuys, 
SUM(o.order_amt) AS LastMonthSum, o.emailid, p.personid 
FROM @order o INNER JOIN @email e 
ON o.emailid = e.emailid 
inner join @person p on e.personid = p.personid
INNER JOIN 
(SELECT p2.personid FROM @person p2 
 INNER JOIN @email e2 on p2.personid = e2.emailid 
 inner join @order o2 on e2.emailid = o2.emailid 
 GROUP BY p2.personid HAVING Sum(o2.order_amt) > 50 ) 
 bigbuyers on bigbuyers.personid = p.personid
 WHERE o.order_date > DATEADD(mm, -1, GETDATE()) 
 AND p.state IN ('CA', 'WA', 'OR') 
 GROUP BY o.emailid, p.personid
 HAVING Count(o.orderid) > 2
 ORDER BY LastMonthSum DESC

--Version 2 50 order_amt limit for last month
 SELECT TOP 2 Count(o.orderid) as LastMonthBuys, 
SUM(o.order_amt) AS LastMonthSum, o.emailid, p.personid 
FROM @order o INNER JOIN @email e 
ON o.emailid = e.emailid 
inner join @person p on e.personid = p.personid
WHERE o.order_date > DATEADD(mm, -1, GETDATE()) 
 AND p.state IN ('CA', 'WA', 'OR') 
 GROUP BY p.personid, o.emailid
 HAVING Count(o.orderid) > 2 AND SUM(o.order_amt) > 50
 ORDER BY LastMonthSum DESC