0

这个问题是更改此查询以对行进行分组并过滤掉除具有最小值但最后有额外位的行之外的所有行的延续......

我有以下结果集:

275     72.87368055555555555555555555555555555556   foo    70
275     72.87390046296296296296296296296296296296   foo    90 
113     77.06431712962962962962962962962962962963   foo    80
113     77.07185185185185185185185185185185185185   foo    60 

我从这个查询中得到:

SELECT id, (tbl2.date_modified - tbl1.date_submitted)/86400, some_value
FROM tbl1, tbl2, tbl3
WHERE tbl1.id = tbl2.fid 
AND tbl1.id = tbl3.fid

请注意,有 4 行有 2 个 id。我想过滤行以仅获取第二列中的最小数字。这修复了它:

SELECT id, min((tbl2.date_modified - tbl1.date_submitted)/86400), max(some_value)
FROM tbl1, tbl2, tbl3
WHERE tbl1.id = tbl2.fid 
AND tbl1.id = tbl3.fid
GROUP BY tbl1.id

所以我得到了:

275     72.87368055555555555555555555555555555556   foo    70
113     77.06431712962962962962962962962962962963   foo    80

如何更改它以执行相同但不包括其他行的行some_value=90?IE

113     77.06431712962962962962962962962962962963   foo    80

我想我需要一些嵌套组或嵌套查询?!

非常感谢 :)。

4

3 回答 3

1

您应该能够使用NOT EXISTS

SELECT id, 
  min((tbl2.date_modified - tbl1.date_submitted)/86400), 
  max(some_value)
FROM tbl1, tbl2, tbl3
WHERE tbl1.id = tbl2.fid 
  AND tbl1.id = tbl3.fid
  AND NOT EXISTS (SELECT id 
                  FROM tbl2 
                  WHERE tbl1.id = tbl2.fid
                    AND some_value = 90)
GROUP BY tbl1.id

或者使用 ANSI 连接语法:

SELECT id, 
  min((tbl2.date_modified - tbl1.date_submitted)/86400), 
  max(some_value)
FROM tbl1
INNER JOIN tbl2
   ON tbl1.id = tbl2.fid 
INNER JOIN tbl3
   ON tbl1.id = tbl3.fid
WHERE NOT EXISTS (SELECT id 
                  FROM tbl2 
                  WHERE tbl1.id = tbl2.fid
                    AND some_value = 90)
GROUP BY tbl1.id
于 2012-09-08T15:56:35.737 回答
1

您可以使用where not exists

select a.id, min((b.date_modified - a.date_submitted)/86400), max(some_value)
  from tbl1 a
  join tbl2 b
    on a.id = b.fid 
  join tbl3 c
    on a.id = c.fid
 where not exists ( select 1 
                      from tbl2
                     where fid = a.id 
                       and some_value >= 90 )
 group by a.id

或者not in

select a.id, min((b.date_modified - a.date_submitted)/86400), max(some_value)
  from tbl1 a
  join tbl2 b
    on a.id = b.fid 
  join tbl3 c
    on a.id = c.fid
 where a.id not in ( select fid 
                       from tbl2 
                        and some_value >= 90 )
 group by a.id
于 2012-09-08T15:56:37.497 回答
1

首先,您应该使用标准的 JOIN 语法编写查询。如果您只想排除 some_value = 90 的行,请在 WHERE 子句中执行此操作:

SELECT id, min((tbl2.date_modified - tbl1.date_submitted)/86400), max(some_value)
FROM tbl1 join
     tbl2
     on tbl1.id = tbl2.fid join
     tbl3
     on tbl1.id = tbl3.fid
WHERE somevalue = 90
GROUP BY tbl1.id

我不确定您所说的“不包括具有 some_value = 90 的其他行的行”是什么意思。如果您的意思是在有 90 的情况下排除所有结果行,则使用 HAVING 子句:

SELECT id, min((tbl2.date_modified - tbl1.date_submitted)/86400), max(some_value)
FROM tbl1 join
     tbl2
     on tbl1.id = tbl2.fid join
     tbl3
     on tbl1.id = tbl3.fid
GROUP BY tbl1.id
HAVING sum(case when somevalue = 90 then 1 else 0 end) > 0

另一个提示:始终包含列的别名,这样每个人都知道他们来自哪个表。

于 2012-09-08T15:57:24.730 回答