我有这两个表。我想要统计最后一段时间连续注释'1'的con_id的数量。
例如:2表示A1,1表示A3,而0表示A2和B1,因为它们对于下表的最新结果连续不具有“1”。通过查询与内嵌查询组的SQL帮助
t_conmast
- CON_ID [PK]
- off_code
con_id off_code A1 1 A2 1 B1 2 A3 1
t_readbak
- CON_ID [FK]
- 计数器
- 备注
- timestamp [未在表格中显示;自动由系统插入]
con_id counter remark timestamp A1 1 0 A1 3 1 A1 6 1 B1 1 1 B1 2 0 A2 1 0 A2 2 1 A2 3 0 A3 1 1
我尝试和失败(我加了off_code只是为了获得导致单个办公室)
select con_id,
count(con_id)
from t_readbak
where remark=1 and timestamp > (select max(timestamp)
from t_readbak
where remark=0
group by con_id)
and con_id in (select con_id from t_conmast where off_code=1)
预计输出
con_id count(con_id) A1 2 A2 0 A3 1 B1 0
您可以根据问题中的数据发布预期的输出吗? – 2013-04-29 14:37:43
@EdGibbs编辑:) – Sourav 2013-04-29 14:39:10
你可以(a)描述你如何定义一个“期间”(如在*“我想要计算在最后一个期间持续注释'1'的con_id的数目”*), (b)阐明办公室的重要性(大概由'off_code'定义)和(c)将时间戳数据添加到样本数据集中? – 2013-04-29 14:44:36