基于重叠限制排除行(SQL)

时间:2012-02-08 19:36:58

标签: mysql sql database

我在看this link,但仍然无法创建我的陈述。我有以下查询

SELECT SUM(AMT) AS TOTAL, NAME, YEAR 
FROM TABLE WHERE IND='ISVALID' GROUP BY NAME, YEAR

我会得到以下结果

TOTAL | NAME | YEAR 
====================
10000 | JOHN | 2010  
20500 | PETE | 2009 
12500 | MIKE | 2008 
50500 | MARY | 2008 
50500 | MARY | 2007 
40000 | JANE | 2007 

我想排除50500 |玛丽| 2008年,它的IND列也带有'ISVALID'。我该怎么做?

1 个答案:

答案 0 :(得分:3)

SELECT SUM(AMT) AS TOTAL, NAME, YEAR 
FROM TABLE 
WHERE IND='ISVALID' AND (NAME <> 'MARY' OR YEAR <> 2008)
GROUP BY NAME, YEAR