2

假设我们有一个这样的表:

**tablename**
ID     Name      Size      Date   AtStore1  AtStore2
 1    Apple    Medium  20120101                  Yes
 2     Pear    Medium  20111231        Yes       Yes
 3    Lemon     Small  20111231        Yes       Yes
 4   Orange     Small  20111231        Yes          
 5   Carrot    Medium  20111231        Yes          
 6   Potato     Small  20111231        Yes          
 7   Celery     Large  20111231                  Yes
 8    Onion    Medium  20111231                     
 9   Tomato    Medium  20111231                     
 10   Apple    Medium  20111231                     
 12    Pear    Medium  20111230        Yes       Yes
 13   Lemon     Small  20111230        Yes       
 14  Orange     Small  20111230                  Yes
 15  Carrot    Medium  20111230                  Yes
 16  Potato     Small  20111230           
 17  Celery     Large  20111229                     
 18   Onion    Medium  20111229                  Yes
 19  Tomato    Medium  20111229                     

我们是否可以构建一个高效的查询来检索每行的所有数据,其中:

  • “是”AtStore1 行,其中当天的 AtStore1 中至少有 4 个“是”,
    或者(含)
  • AtStore2 中有一个“是”

如果只满足第一个参数,即行的正向查找,也是可以接受的。如有必要,我可能可以使用 PHP 编写 AtStore2 部分的脚本。我所有的尝试都惨遭失败;我对如何在 Google 中有效地写出问题的尝试也没有什么成果。

(在 PHP 中将事情分解为不同代码部分的答案也很好,我只想要一些相当有效的东西。)

对于此示例表,这些将是预期的行:

ID     Name      Size      Date   AtStore1  AtStore2
 1    Apple    Medium  20120101                  Yes
 2     Pear    Medium  20111231        Yes       Yes
 3    Lemon     Small  20111231        Yes       Yes
 4   Orange     Small  20111231        Yes          
 5   Carrot    Medium  20111231        Yes          
 6   Potato     Small  20111231        Yes          
 7   Celery     Large  20111231                  Yes
12     Pear    Medium  20111230        Yes       Yes
14   Orange     Small  20111230                  Yes
15   Carrot    Medium  20111230                  Yes
18    Onion    Medium  20111229                  Yes

如你看到的

  • 2012 年 1 月 1 日
    -AtStore1 中没有 Yeses
    -AtStore2 有 Yes 所以返回该行
  • 12 月 31 日
    -5 AtStore1 中的 Yeses,因此返回(ID 2 和 3 中的 AtStore2 Yeses 也足够了)
    -也有 ID 7,因为 AtStore2 中有 Yes
  • 返回的其余行仅是因为 AtStore2 中的 Yeses
4

2 回答 2

1

试试这个:

select t.* 
from tablename t
join (
    select date
    from tablename
    where AtStore1='yes'
    group by date
    having count(*) >= 4
) ta on t.date = ta.date and t.AtStore1 = 'yes'
union
select *
from tablename
where AtStore2 = 'yes'

结果:

ID  Name    Size    Date    AtStore1    AtStore2
1   Apple   Medium  2012-01-01 00:00:00.000 NULL    Yes
2   Pear    Medium  2011-12-31 00:00:00.000 Yes Yes
3   Lemon   Small   2011-12-31 00:00:00.000 Yes Yes
4   Orange  Small   2011-12-31 00:00:00.000 Yes NULL
5   Carrot  Medium  2011-12-31 00:00:00.000 Yes NULL
6   Potato  Small   2011-12-31 00:00:00.000 Yes NULL
7   Celery  Large   2011-12-31 00:00:00.000 NULL    Yes
12  Pear    Medium  2011-12-30 00:00:00.000 Yes Yes
14  Orange  Small   2011-12-30 00:00:00.000 NULL    Yes
15  Carrot  Medium  2011-12-30 00:00:00.000 NULL    Yes
18  Onion   Medium  2011-12-29 00:00:00.000 NULL    Yes

在这种情况下,空值会被聚合有意消除。

如果您想要特定日期的所有行而不关心它是否在 AtStore1 列中具有值,and t.AtStore1 = 'yes'我不确定是否删除它。ta on t.date = ta.date and t.AtStore1 = 'yes'yes

补充

回答评论中的问题,但这是一种快速且可能很肮脏的方法(没有时间):

select t3.*, t4.qty from tablename t3
join (
    select id, max(qty) as qty from (
        select t.id, ta.qty
        from tablename t
        join (
            select date, count(*) as qty
            from tablename
            where AtStore1='yes'
            group by date
            having count(*) >= 4
        ) ta on t.date = ta.date and t.AtStore1 = 'yes'
        union
        select id, 0 as src
        from tablename
        where AtStore2 = 'yes'
    ) t2
    group by t2.id
) t4 on t3.id = t4.id

结果:

ID  Name    Size    Date    AtStore1    AtStore2    qty
1   Apple   Medium  2012-01-01 00:00:00.000 NULL    Yes 0
2   Pear    Medium  2011-12-31 00:00:00.000 Yes Yes 5
3   Lemon   Small   2011-12-31 00:00:00.000 Yes Yes 5
4   Orange  Small   2011-12-31 00:00:00.000 Yes NULL    5
5   Carrot  Medium  2011-12-31 00:00:00.000 Yes NULL    5
6   Potato  Small   2011-12-31 00:00:00.000 Yes NULL    5
7   Celery  Large   2011-12-31 00:00:00.000 NULL    Yes 0
12  Pear    Medium  2011-12-30 00:00:00.000 Yes Yes 0
14  Orange  Small   2011-12-30 00:00:00.000 NULL    Yes 0
15  Carrot  Medium  2011-12-30 00:00:00.000 NULL    Yes 0
18  Onion   Medium  2011-12-29 00:00:00.000 NULL    Yes 0
于 2012-01-02T08:08:11.657 回答
0

它应该是这样的:

SELECT * FROM t1 WHERE ID IN (
    SELECT Id FROM t1 WHERE AtStore2 != 'Yes' AND Date IN (
        SELECT Date FROM t1 WHERE AtStore1 = 'Yes' GROUP BY Date
        HAVING COUNT(Date) >= 4
))
UNION ALL
SELECT * FROM t1 WHERE AtStore2 = 'Yes'

"ID";"名称";"大小";"日期";"Atstore1";"Atstore2" "1";"Apple";"Medium";"20120101";"";"是""2";"梨”;“中”;“20111231”;“是”;“是”“3”;“柠檬”;“小”;“20111231”;“是”;“是”“4”;“橙色”;”小”;“20111231”;“是”;“”“5”;“胡萝卜”;“中”;“20111231”;“是”;“”“6”;“土豆”;“小”;“20111231” ;"是";"" "7";"芹菜";"大";"20111231";"";"是" "8";"洋葱";"中号;"20111231";"";"""9";"番茄";"中号";"20111231";"";"""10";"苹果";"中号";"20111231";" “;”“12”;“梨”;“中”;“20111230”;“是”;“是”“14”;“橙色”;“小”;“20111230”;“”;“是” 15";"胡萝卜";"中等";"20111230";"";"是" "18";"洋葱";"中等";"20111229";"";"是"“中”;“20111231”;“”;“”“12”;“梨”;“中”;“20111230”;“是”;“是”“14”;“橙色”;“小”;“20111230” “;”“;“是”“15”;“胡萝卜”;“中等”;“20111230”;“”;“是”“18”;“洋葱”;“中等”;“20111229”;“”;”是的”“中”;“20111231”;“”;“”“12”;“梨”;“中”;“20111230”;“是”;“是”“14”;“橙色”;“小”;“20111230” “;”“;“是”“15”;“胡萝卜”;“中等”;“20111230”;“”;“是”“18”;“洋葱”;“中等”;“20111229”;“”;”是的”18";"洋葱";"中等";"20111229";"";"是"18";"洋葱";"中等";"20111229";"";"是"

于 2012-01-02T08:11:47.500 回答