• MYSQL子查询例题以及答案


    More Subqueries Quizzes

    Above is the ERD for the database again - it might come in handy as you tackle the quizzes below. You should write your solution as a subquery or subqueries, not by finding one solution and copying the output. The importance of this is that it allows your query to be dynamic in answering the question - even if the data changes, you still arrive at the right answer.

        1. Provide the name of the sales_rep in each region with the largest amount of total_amt_usd sales.

          SELECT * FROM
          (SELECT rname rname,MAX(totalsum) totalsum
          FROM
          (SELECT r.name rname,s.name sname,SUM(total_amt_usd) totalsum
          FROM accounts a 
          JOIN orders o
          ON o.account_id = a.id
          JOIN sales_reps s
          ON a.sales_rep_id = s.id
          JOIN region r
          ON r.id = s.region_id
          GROUP BY r.name,s.name
          ORDER BY 1,3 desc ) sub
          GROUP BY rname ) t1
          JOIN
          (SELECT r.name rname,s.name sname,SUM(total_amt_usd) totalsum
          FROM accounts a 
          JOIN orders o
          ON o.account_id = a.id
          JOIN sales_reps s
          ON a.sales_rep_id = s.id
          JOIN region r
          ON r.id = s.region_id
          GROUP BY r.name,s.name
          ORDER BY 1,3 desc ) t2
          ON t1.rname = t2.rname
          WHERE t1.totalsum = t2.totalsum

          METHOD2

          WITH t1 AS (SELECT rname rname,MAX(totalsum) totalsum
          FROM
          (SELECT r.name rname,s.name sname,SUM(total_amt_usd) totalsum
          FROM accounts a
          JOIN orders o
          ON o.account_id = a.id
          JOIN sales_reps s
          ON a.sales_rep_id = s.id
          JOIN region r
          ON r.id = s.region_id
          GROUP BY r.name,s.name
          ORDER BY 1,3 desc ) sub
          GROUP BY rname ),

          t2 AS (SELECT r.name rname,s.name sname,SUM(total_amt_usd) totalsum
          FROM accounts a
          JOIN orders o
          ON o.account_id = a.id
          JOIN sales_reps s
          ON a.sales_rep_id = s.id
          JOIN region r
          ON r.id = s.region_id
          GROUP BY r.name,s.name
          ORDER BY 1,3 desc )

          SELECT *
          FROM t1
          JOIN t2
          ON t1.rname = t2.rname
          WHERE t1.totalsum = t2.totalsum

      1. For the region with the largest (sum) of sales total_amt_usd, how many total (count) orders were placed? 

      2. For the name of the account that purchased the most (in total over their lifetime as a customer) standard_qty paper, how many accounts still had more in total purchases? 

      3. For the customer that spent the most (in total over their lifetime as a customer) total_amt_usd, how many web_events did they have for each channel?

      4. What is the lifetime average amount spent in terms of total_amt_usd for the top 10 total spending accounts?

      5. What is the lifetime average amount spent in terms of total_amt_usd for only the companies that spent more than the average of all orders.

  • 相关阅读:
    网站设计分析:模块化——高效重构
    linux 优化git操作速度
    cool
    跨域解决方案
    vue 登录验证引擎
    scss学习笔记
    配置动态加载模块和js分模块打包,生产环境和开发环境公共常量配置
    降低版本安装flashPlayer
    python 学习日志
    centos atomic host第一次启动
  • 原文地址:https://www.cnblogs.com/kouryoushine/p/9313667.html
Copyright © 2020-2023  润新知