Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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
SQL-条件Where语句可能吗?_Sql_Sql Server - Fatal编程技术网

SQL-条件Where语句可能吗?

SQL-条件Where语句可能吗?,sql,sql-server,Sql,Sql Server,我一直在尝试实现一个有条件的Where和having问题。我想做的是只选择那些在过去一年里去过美容院理发和修指甲的顾客。治疗不必在同一天进行。e、 g.如果Susan在过去12个月内修过指甲,请选择Susan在同一时间段内所有的理发时间。该表是结构化的,因此每次访问都是一行。e、 g Customer Treatment Treatment_Date Susan Manicure 12 Jan 2013 Susan Make-up

我一直在尝试实现一个有条件的Where和having问题。我想做的是只选择那些在过去一年里去过美容院理发和修指甲的顾客。治疗不必在同一天进行。e、 g.如果Susan在过去12个月内修过指甲,请选择Susan在同一时间段内所有的理发时间。该表是结构化的,因此每次访问都是一行。e、 g

Customer     Treatment     Treatment_Date
Susan        Manicure      12 Jan 2013
Susan        Make-up       3 Feb 2013
Susan        Haircut       14 Feb 2013
Susan        Blow Dry      3 Mar 2013
Susan        Haircut       21 Apr 2013
Betty        Manicure      4 Jun 2013
Betty        Haircut       7 July 2013
Tara         Haircut       10 Aug 2013
所以理想情况下,我会选择苏珊和贝蒂。这可以在一个查询中完成,还是必须进行分解,比如首先选择所有理发的客户,然后从中选择那些修过指甲的客户

提前谢谢你的建议


这是集合内集合子查询的示例。我喜欢分组解决这些问题,并拥有:

having子句中的每个条件统计每种类型的处理次数。您的条件指定每个选项至少显示一次,因此>0

其他方法:您可以使用between子句。也可以使用自联接。
在其他新闻中,我不会称之为条件Where语句。如果Where子句包含Case When Then结尾短语,我会称之为一个

另一种方法

SELECT DISTINCT Customer
  FROM
(
  SELECT Customer,
         MAX(CASE WHEN Treatment = 'Manicure' THEN 1 ELSE 0 END) 
             OVER (PARTITION BY Customer) had_manicure,
         MAX(CASE WHEN Treatment = 'Haircut' THEN 1 ELSE 0 END) 
             OVER (PARTITION BY Customer) had_haircut
    FROM table1 t
   WHERE Treatment_Date >= DATEADD(month, -12, GETDATE())
) q
 WHERE had_manicure + had_haircut = 2
输出:

| CUSTOMER | |----------| | Betty |
这里是演示

您可以这样做

挑选 顾客

 count(case when treatment = 'manicure' then 1 else 0 end) as Manicure,
 count(case when treatment = 'haircut' then 1 else 0 end) as haircut

其中,治疗日期>=DATEADDyear,-1,GetDate

group by Treatment

have count(Manicure) > 1 and count(haircut) > 1

你能发布你正在使用的SQL吗?看一看,所以理想情况下我想选择Susan和Betty。苏珊在过去的12个月里没有修过指甲,不过。。。
 count(case when treatment = 'manicure' then 1 else 0 end) as Manicure,
 count(case when treatment = 'haircut' then 1 else 0 end) as haircut
customerTreatment
group by Treatment

have count(Manicure) > 1 and count(haircut) > 1