SQL 某一特定时间点以后的信息提取

我想提取出同一卡号的顾客在【类型】为“脱毛”发生之后消费的所有信息。
如:卡号为10848的顾客,在2016-02-21消费“脱毛”之后,又在2016-09-24和2017-06-11分别消费“脱毛”
我写了如下代码图片说明

如下是我的表格:图片说明 由于营业日期是varchar我用如下代码将他变为date。 alter table dbo.XHD_16 alter column 营业日期 date 求指教 【解释】我想要查看客户消费完脱毛之后还消费了什么项目,条件是在消费脱毛之后的日期。希望这样说大家可以理解我的意思

你想查询类型为‘脱毛’的之后这个客户做了那些项目对吧,

select * from 表 a join (select 客户号,日期 from 表 where type = '脱毛') b on
a.客户号 = b.客户号 and a.日期 >= b.日期 不知道对不对,你描述的不是很清楚

where 后面加上日期不行吗?

营业日期建议改成datetime型

表述的不是很清楚,我理解一下,你是想找到所有用户在“脱毛”之后的所有操作吗?

select * from 表 a join (select 客户号,日期 from 表 where type = '脱毛') b on
a.客户号 = b.客户号 and a.日期 >= b.日期

select * from 表 a join (select 客户号,日期 from 表 where type = '脱毛') b on a.客户号 = b.客户号 and a.日期 >= b.日期

select * from 表 where 日期字段>='开始日期' and 日期字段<='截止日期'
and convert(char(8),日期字段,108)>='开始时间' and convert(char(8),日期字段,108)<='截止时间',
类似的,在你的where后面加上营业日期>=2016-09-24 and 营业日期<=2017-06-11,他的前提条件是营业日期>=2016-02-21and类型=脱毛

select *
from 表 a,
(select 客户卡号,
营业日期
from 表
where 类型 = '脱毛') b
where a.客户卡号 = b.客户卡号
and a.营业日期 >= b.营业日期
order by 客户卡号;

你是不是想查询第一次脱毛以后的记录?你可以这样,把查询出来的结果分组取max()具体操作参照文章:https://blog.csdn.net/mango_love/article/details/80249188
接着重要的来了:这个分组max结果然后inner join关联之前没有分组的结果,然后查询两个结果时间不想等的结果。。这样就可以排除第一次脱毛,就可以得到你想要的客户第一次脱毛后的消费了

select *
from 表 a,
(select top 1 客户卡号,
营业日期
from 表
where 类型 = '脱毛' order by 营业日期) b
where a.客户卡号 = b.客户卡号
and a.营业日期 >= b.营业日期
and a.类型 = '脱毛'
order by 客户卡号,营业日期;

大概是这个意思,没有你的环境,没法调试

select * from dbo.XHD_16 t1
where t1.营业时间 > (
select min(t2.营业时间) from dbo.XHD_16 t2
where t1.客户卡号 = t2.客户卡号 and t2.类型 = '脱毛'
) or t1.营业时间 > (
select min(t3.营业时间) from dbo.XHD_17 t3
where t1.客户卡号 = t3.客户卡号 and t3.类型 = '脱毛'
)

union

select * from dbo.XHD_17 t1
where t1.营业时间 > (
select min(t2.营业时间) from dbo.XHD_16 t2
where t1.客户卡号 = t2.客户卡号 and t2.类型 = '脱毛'
) or t1.营业时间 > (
select min(t3.营业时间) from dbo.XHD_17 t3
where t1.客户卡号 = t3.客户卡号 and t3.类型 = '脱毛'
)