SQLite嵌套查询的最大值

时间:2019-01-30 03:53:10

标签: sqlite

我正在尝试使用SQL Server的DB Browser来构造一个嵌套查询,以确定前10名消费最多的SECOND价格最高的商品。我必须挑选出前10名支出者的查询是:

SELECT user_id, max(item_total), SUM (item_total + shipping_cost -
discounts_applied) AS total_spent 
FROM orders AS o
WHERE payment_reject = "FALSE" 
GROUP BY user_id 
ORDER BY total_spent DESC 
LIMIT 10

这会提供user_id,他们购买的最昂贵的商品(不计算运费或折扣)以及他们在网站上花费的总金额。

我试图使用嵌套查询来生成他们购买的第二贵商品的列表,但始终会出错。我尝试过

SELECT user_id, MAX(item_total) AS second_highest
  FROM orders
 WHERE item_total < (SELECT user_id, SUM (item_total + shipping_cost -
discounts_applied) AS total_spent 
FROM orders 
WHERE payment_reject = "FALSE" 
GROUP BY user_id 
ORDER BY total_spent DESC 
LIMIT 10)
group by user_id

我一直收到行值滥用错误。是否有人在此嵌套查询上有指针,或者是否知道从第一个查询中找到的组中找到购买价格第二高的商品的另一种方法? 谢谢!

1 个答案:

答案 0 :(得分:1)

(注意:以下假设您使用的是Sqlite 3.25或更高版本,因为它使用的是窗口函数)。

这将为每个item_total返回第二大的user_id,而不会重复:

WITH ranked AS
 (SELECT DISTINCT user_id, item_total
       , dense_rank() OVER (PARTITION BY user_id ORDER BY item_total DESC) AS ranking
  FROM orders)
SELECT user_id, item_total FROM ranked WHERE ranking = 2;

您可以将其与原始查询结合使用,例如:

WITH ranked AS
 (SELECT DISTINCT user_id, item_total
       , dense_rank() OVER (PARTITION BY user_id ORDER BY item_total DESC) AS ranking
  FROM orders),
 totals AS
 (SELECT user_id
       , sum (item_total + shipping_cost - discounts_applied) AS total_spent 
  FROM orders
  WHERE payment_reject = 0 
  GROUP BY user_id)
SELECT t.user_id, r.item_total, t.total_spent
FROM totals AS t
JOIN ranked AS r ON t.user_id = r.user_id
WHERE r.ranking = 2
ORDER BY t.total_spent DESC, t.user_id
LIMIT 10;

好的,在修复了表定义以更好地反映存储在表中的值和所述问题之后,修复了数据并将其添加到表中,以便您实际上可以得到结果,以及如下所示的可选但有用的索引:

CREATE TABLE orders (order_id INTEGER PRIMARY KEY
                   , user_id INTEGER
                   , item_total REAL
                   , shipping_cost NUMERIC
                   , discounts_applied NUMERIC
                   , payment_reject INTEGER);
INSERT INTO orders(user_id, item_total, shipping_cost, discounts_applied
                 , payment_reject) VALUES (9852,60.69,10,0,FALSE),
 (2784,123.91,15,0,FALSE), (1619,119.75,15,0,FALSE), (9725,151.92,15,0,FALSE),
 (8892,153.27,15,0,FALSE), (7105,156.86,25,0,FALSE), (4345,136.09,15,0,FALSE),
 (7779,134.93,15,0,FALSE), (3874,157.27,15,0,FALSE), (5102,108.3,10,0,FALSE),
 (3098,59.97,10,0,FALSE),  (6584,124.92,15,0,FALSE), (5136,111.06,10,0,FALSE),
 (1869,113.44,20,0,FALSE), (3830,129.63,15,0,FALSE), (9852,70.69,10,0,FALSE),
 (2784,134.91,15,0,FALSE), (1619,129.75,15,0,FALSE), (9725,161.92,15,0,FALSE),
 (8892,163.27,15,0,FALSE), (7105,166.86,25,0,FALSE), (4345,146.09,15,0,FALSE),
 (7779,144.93,15,0,FALSE), (3874,167.27,15,0,FALSE), (5102,118.3,10,0,FALSE),
 (3098,69.97,10,0,FALSE),  (6584,134.92,15,0,FALSE), (5136,121.06,10,0,FALSE), 
 (1869,123.44,20,0,FALSE), (3830,139.63,15,0,FALSE);
CREATE INDEX orders_idx_1 ON orders(user_id, item_total DESC);

上面的查询将给出:

user_id     item_total  total_spent
----------  ----------  -----------
7105        156.86      373.72     
3874        157.27      354.54     
8892        153.27      346.54     
9725        151.92      343.84     
4345        136.09      312.18     
7779        134.93      309.86     
3830        129.63      299.26     
6584        124.92      289.84     
2784        123.91      288.82     
1619        119.75      279.5      

(如果现在从查询中收到语法错误,那是因为您使用的是不支持窗口功能的旧版本的sqlite。)