- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定一个表格
custid | date | action
1 | 2011-04-01 | activate
1 | 2011-04-10 | deactivate
1 | 2011-05-02 | activate
2 | 2011-04-01 | activate
3 | 2011-03-01 | activate
3 | 2011-04-01 | deactivate
数据库是PostgreSQL。
我想要一个 SQL 查询来显示在 5 月的任何阶段活跃的客户。
因此,在上面,这将是 1 和 2。
我只是无法理解如何处理这个问题。有什么指点吗?
更新
客户 2 在 5 月活跃,因为他在 5 月之前被激活,并且自激活后没有被停用。比如,我这个月还活着,但不是这个月出生的,也没有死。
select distinct custid
from MyTable
where action = 'active' and date >= '20110501' and date < '20110601'
这种方法行不通,因为它只显示五月期间的激活,而不是“事件”。
最佳答案
注意:这将是一个起点,仅适用于 2011 年。
忽略任何挥之不去的错误,此代码(针对每个客户)查看 1) 客户在 5 月之前的最新状态更新以及 2) 客户是否在 5 月期间变得活跃?
SELECT
Distinct CustId
FROM
MyTable -- Start with the Main table
-- So, was this customer active at the start of may?
LEFT JOIN -- Find this customer's latest entry before May of This Year
(select
max(Date)
from
MyTable
where
Date < '2011-05-01') as CustMaxDate_PreMay on CustMaxDate_PreMay.CustID = MyTable.CustID
-- Return a record "1" here if the Customer was Active on this Date
LEFT JOIN
(select
1 as Bool,
date
from
MyTable
) as CustPreMay_Activated on CustPreMay_Activated.Date = CustMaxDate_PreMay.Date and CustPreMay_Activated.CustID = MyTable.CustID and CustPreMay_Activated = 'activated'
-- Fallback plan: If the user wasn't already active at the start of may, did they turn active during may? If so, return a record here "1"
LEFT JOIN
(select
1 as Bool
from
MyTable
where
Date <= '2011-05-01' and Date < '2011-06-01' and action = 'activated') as TurnedActiveInMay on TurnedActiveInMay .CustID = MyTable.CustID
-- The Magic: If CustPreMay_Activated is Null, then they were not active before May
-- If TurnedActiveInMay is also Null, they did not turn active in May either
WHERE
ISNULL(CustPreMay_Activated.Bool, ISNULL(TurnedActiveInMay.Bool, 0)) = 1
注意:
您可能需要将“FROM MyTable”替换为
From (Select distinct CustID from MyTable) as Customers
仅查看这段代码,我不清楚它是否会 A) 太慢或 B) 由于启动 FROM 子句 @MYTable 而导致重复或问题,每个客户可能包含许多记录。 DISTINCT 子句可能会解决这个问题,但我想我会提到这个解决方法。
最后,我将留给您让这项工作跨越不同的年份。
关于SQL根据激活/停用记录回答: which customers were active in a given month,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6222147/
我正在处理一些遗留代码,其中 java.util.Calendar 用于与日期相关的计算(基本上是添加月份)。现在我想在代码中用 java.time.LocalDate 替换 java.util.Ca
我刚刚在我正在学习的类(class)中开始使用 R,到目前为止我所做的是制作一个加载文件的脚本。 如果重要的话,我正在使用 Rstudio 1.0.44 使用 Ubuntu 14.04 运行 R。 我
我正在按月查询在 Laravel 中创建的用户,这是我的代码 $devlist = DB::table("users") ->select("id" ,DB::ra
C++20 计时类型/值之间有什么区别 month{7}和 months{7} ?有两个如此相似的名字是不是很困惑? 最佳答案 是的,同时拥有 month 可能会令人困惑。和 months第一次遇到这
我有一个数据库,其中包含我的用户完成的每个操作,并带有时间戳,如下所示: 用户名 ; Action ; 创建日期 我想评估一段时间内活跃用户的数量。我可以看到每周活跃的用户数 (count (dist
我在我的 Python 脚本中使用了 parsedatetime 库,似乎“上个月”有效,“下个月”但“本月”无效。 我错过了什么?有没有办法获取当月的第一天? 编辑:抱歉,这是一些代码: impor
我发现的大部分信息都不在 python>pandas>dataframe 中,因此是这个问题。 我想将 1 到 12 之间的整数转换为缩写的月份名称。 我有一个 df,它看起来像: client
在用户单击“下个月”或“上个月”后,我需要使用自定义 CSS 重新填充日期选择器。我正在点击 onChangeMonthYear 事件,但不知道用户要去哪条路(下个月或上个月)。有什么简单的内置东西可
我正在尝试计算一个月内产品的总销售额,但我希望它包括任何“空”月份(没有销售额)并且只选择最近的 12 个月。 到目前为止,这是我的代码。 declare @ProductNo int set @Pr
我有一个独特的问题,我根本无法解决。 所以我在 SQL Server 2005 中,我得到了以下要处理的数据: FISCAL_YEAR_START_MONTH INT(财政年度的第一个月) COUNT
我正在进行的一个项目需要获取过去 12 个月中每个月的平均发票总额。我可以通过执行 12 个查询(每月 1 个)轻松获得我需要的内容,但它非常丑陋,我最终得到 12 个唯一变量(id 而不是有一个循环
我希望用更好的不太详细的过程替换以下函数,用于从字符串确定数字月份值。有谁知道是否有可用于此目的的日期/日期时间函数?我没有注意到任何一个可以满足我的需求。 代码中的文本字符串正是我接收它们的方式。
我正在尝试获取过去 12 个月(包括当月)按月分组的记录数。不是从今年年初开始。 我有一个表 EVENTS 和一个字段 WODATE。我能做到 aFDQuery.Connection := Tra
我正在使用moment.js在 angularjs 应用程序中进行日期转换。我要打印Month number作为Month name 。我已经尝试过如下, {{item.ReviewMonth | d
我想通过 JPA 从 Oracle 数据库中选择实体,方法如下: Calendar cal = Calendar.getInstance(); cal.setTime(anOtherDate); Qu
我希望用一个更好的更简单的过程来替换以下函数,以便从字符串中确定数字月份值。有谁知道是否有可用于此的 Date/DateTime 函数?我没有注意到可以满足我的需求的。 代码中的文本字符串正是我接收它
我正在尝试获取过去 12 个月(包括当月)按月分组的记录数。不是从今年年初开始。 我有一个表 EVENTS 和一个字段 WODATE。我能做到 aFDQuery.Connection := Tra
我读到“将日期字符串转换为mysql日期时间字段”:但这对我没有帮助 所以我想做的是将dat文件读入mysql中的空表。 table CLIMBED(TRIP_ID int,PEAK VARCHAR(
我有这样的查询: SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS apcptot FROM t_synop_data2 d
一些需要更改 df header 的 Django 格式问题。 测试数据: Test_Data = [ ('Year_Month', ['Done_RFQ','Not_
我是一名优秀的程序员,十分优秀!