Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL计算一段时间内的收益、损失和净收益_Mysql - Fatal编程技术网

MySQL计算一段时间内的收益、损失和净收益

MySQL计算一段时间内的收益、损失和净收益,mysql,Mysql,我有一张这样的桌子: 我想计算每个月的“赢得”客户、每个月的“失去”客户以及每个月的净收益,即使是在单独的表/视图中完成 我该怎么做 编辑 好的,让我来演示一下我到目前为止所做的工作 为了选择任何月份获得的客户,我尝试从Bookings表中选择不存在以下内容的客户: select Customer from Bookings where not exists (select Customer from Bookings where (Bookings.date BETW

我有一张这样的桌子:

我想计算每个月的“赢得”客户、每个月的“失去”客户以及每个月的净收益,即使是在单独的表/视图中完成

我该怎么做

编辑

好的,让我来演示一下我到目前为止所做的工作

为了选择任何月份获得的客户,我尝试从Bookings表中选择不存在以下内容的客户:

select Customer
from Bookings
where not exists
  (select Customer
   from Bookings
   where
     (Bookings.date BETWEEN
          DATE_FORMAT(DATE_SUB(Bookings.date, INTERVAL 1 MONTH), '%Y-%m-01 00:00:00')
          AND DATE_FORMAT(Bookings.date, '%Y-%m-01 00:00:00'
     )
) AND Bookings.date >= STR_TO_DATE('2010-11-01 00:00:00', '%Y-%m-%d 00:00:00'))
select Customer
from Booking
where not exists
  (select Customer
   from Bookings
   where
     (Bookings.date BETWEEN
          DATE_FORMAT(Bookings.date, '%Y-%m-01 00:00:00')
          AND Bookings.date
     )
     AND Bookings.date >= STR_TO_DATE('2010-11-01 00:00:00', '%Y-%m-%d 00:00:00'
  )
)
这可能会获取“选定”月份中存在但上一个月中不存在的客户。“2010-11-01”是开始预订的日期+1个月

为了选择任何月份的流失客户,我尝试从Bookings表中选择不存在以下内容的客户:

select Customer
from Bookings
where not exists
  (select Customer
   from Bookings
   where
     (Bookings.date BETWEEN
          DATE_FORMAT(DATE_SUB(Bookings.date, INTERVAL 1 MONTH), '%Y-%m-01 00:00:00')
          AND DATE_FORMAT(Bookings.date, '%Y-%m-01 00:00:00'
     )
) AND Bookings.date >= STR_TO_DATE('2010-11-01 00:00:00', '%Y-%m-%d 00:00:00'))
select Customer
from Booking
where not exists
  (select Customer
   from Bookings
   where
     (Bookings.date BETWEEN
          DATE_FORMAT(Bookings.date, '%Y-%m-01 00:00:00')
          AND Bookings.date
     )
     AND Bookings.date >= STR_TO_DATE('2010-11-01 00:00:00', '%Y-%m-%d 00:00:00'
  )
)
这可能会得到上个月存在的客户,但不是“选定”的客户


对于“丢失”SQL查询,我得到了空结果!对于“增益”,我得到了数千行,但不确定这是否准确。

您可以使用COUNT DISTINCT来计算您的客户,其中年份(日期)=[YEAR]和月份(日期)=[MONTH]来获取月份

2013年9月的客户总数:

SELECT COUNT(DISTINCT Customer) AS MonthTotalCustomers FROM table
WHERE YEAR(date) = 2013 AND MONTH(date) = 9
SELECT COUNT(DISTINCT Customer) AS MonthGainedCustomers FROM table
WHERE YEAR(date) = 2013 AND MONTH(date) = 9
AND Customer NOT IN 
    (SELECT Customer FROM table 
    WHERE date < '2013-09-01')
2013年9月获得的客户:

SELECT COUNT(DISTINCT Customer) AS MonthTotalCustomers FROM table
WHERE YEAR(date) = 2013 AND MONTH(date) = 9
SELECT COUNT(DISTINCT Customer) AS MonthGainedCustomers FROM table
WHERE YEAR(date) = 2013 AND MONTH(date) = 9
AND Customer NOT IN 
    (SELECT Customer FROM table 
    WHERE date < '2013-09-01')

我希望你能从这些例子中推断出你在寻找什么。

那么客户什么时候失去或得到了?标准是什么?请定义获得和失去这不会给你每个月的
“获得”客户
@MostyMostacho…而你这么说是因为…?因为你只有一个给定的月份才有
where
子句(?)我看到你的提问,我看到你甚至解释说,这只会在2013年9月起作用<代码>2013年9月!=每月
:/