美文网首页
利用sql进行商业分析

利用sql进行商业分析

作者: 鲸鱼酱375 | 来源:发表于2019-07-17 06:26 被阅读0次

课件来自datacamp

profit=revenue-cost

1.Revenue per week

Get the revenue per week for each week in June and check whether there's any consistent growth in revenue.

SELECT DATE_TRUNC('week', order_date) :: DATE AS delivr_week,
       # Calculate revenue
       SUM(meal_price * order_quantity) AS revenue
  FROM meals
  JOIN orders ON meals.meal_id = orders.meal_id
# Keep only the records in June 2018
WHERE DATE_TRUNC('month', order_date) = '2018-06-01'
GROUP BY delivr_week
ORDER BY delivr_week ASC;

DATE_TRUNC在postgreSQL才有的用法

2.Profit per month

After prioritizing and making deals with eateries by their overall profits, Alice wants to track Delivr profits per month to see how well it's doing. You're here to help.

You're provided with two CTEs. The first stores revenue and the second stores cost. To access revenue and cost in one query, the two CTEs are joined in the last query. From there, you can apply the formula for profit Profit = Revenue - Cost to calculate profit per month.

Remember that revenue is the sum of each meal's price times its order quantity, and that cost is the sum of each meal's cost times its stocked quantity.

# Set up the revenue CTE
WITH revenue AS ( 
    SELECT
        DATE_TRUNC('month', order_date) :: DATE AS delivr_month,
        sum(meal_price*order_quantity) AS revenue
    FROM meals
    JOIN orders ON meals.meal_id = orders.meal_id
    GROUP BY delivr_month),
#Set up the cost CTE
  cost AS (
    SELECT
        DATE_TRUNC('month', stocking_date) :: DATE AS delivr_month,
        sum(meal_cost*stocked_quantity) AS cost
    FROM meals
    JOIN stock ON meals.meal_id = stock.meal_id
    GROUP BY delivr_month)
#Calculate profit by joining the CTEs
SELECT
    revenue.delivr_month,
    (revenue-cost) as profit
FROM revenue
JOIN cost ON revenue.delivr_month = cost.delivr_month
ORDER BY revenue.delivr_month ASC;

相关文章

  • 利用sql进行商业分析

    课件来自datacamp profit=revenue-cost 1.Revenue per week Get t...

  • 数据分析学习计划

    1.python:Python入门实践、利用Python进行数据分析、Pandas+Numpy 2.SQL:SQL...

  • BD第7课:分析罩杯销售

    本文会利用 SQL 语句和 Pandas 对前面抓取和整理的的胸罩数据进行分析,本文主要分析了胸罩销售的比例,以及...

  • Business Analytics with SQL in a

    写这个系列的初衷非常简单——写一个通俗、易懂、易上手的,如何使用SQL进行商业分析(business analyt...

  • Adventure Works数据库自行车业务分析报告

    本文记录了整个项目需求分析与实现的过程,主要任务是使用SQL完成数据提取过程,接着利用Python进行数据分析,并...

  • Nginx日志安全分析脚本

    功能 统计Top 20 地址 SQL注入分析 SQL from查询统计 常见扫描器、黑客工具分析 漏洞利用检测 敏...

  • 如何将SQL语句进行自动翻译

    如何将SQL语句进行自动翻译 这里我们利用SQL-to-Text Generation with Graph-to...

  • 如何成为商业分析师?

    什么是商业分析师? 商业分析师负责利用数据分析将IT技术和商业联系起来,通过数据分析帮助企业优化生产流程,产品,服...

  • Spark SQL概述

    一、SQL on Hadoop 使用SQL基于Hadoop进行业务处理和分析,常用的框架: H...

  • 工作碎片总结

    一、BI智能商业 智能商业利用技术(数据仓库、联机分析处理OLAP、数据挖掘分析),用于整合企业数据、提供报表(分...

网友评论

      本文标题:利用sql进行商业分析

      本文链接:https://www.haomeiwen.com/subject/lxlakctx.html