每天至少有#个合格行的所有行

时间:2012-01-02 07:23:30

标签: mysql sql count lookup forward

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

**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部分。我的所有尝试都惨遭失败;我在谷歌如何有效地写这个问题的尝试也没有成果。

(在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的情况 -AtStore2有一个是,所以返回行
  • 12月31日 -5在AtStore1中的含义,因此返回(ID2和3中的AtStore2 Yeses也足够了)
    - 因为AtStore2中有“是”,所以也有ID 7
  • 返回的其余行仅仅是因为AtStore2中的Yeses

2 个答案:

答案 0 :(得分: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

在这种情况下,聚合消除了故意的空值。

我不确定and t.AtStore1 = 'yes'中的ta on t.date = ta.date and t.AtStore1 = 'yes'如果您想要特定日期的所有行而不关心它是否在AtStore1列中具有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

答案 1 :(得分: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”, “苹果”, “中等”, “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”; “”; “是”