1

I have a SQL problem that I don't have the vocabulary to explain very well.

Here is a simplified table. My goal is to identify groups where the Tax_IDs are not equal. In this case, the query should return groups 1 and 3.

Group   Line_ID     Tax_ID
1   1001        11
1   1002        13
2   1003        17
2   1004        17
3   1005        23
3   1006        29

I can easily perform comparisons across rows, however I do not know how to perform comparisons "down" a table (here is really where my vocabulary fails me). I.e. what is the syntax that will cause SQL to compare Tax_ID values within groups?

Any help appreciated,

  • OB
4

1 回答 1

2

The simplest way is to use group by with a having clause:

select "group"
from t
group by "group"
having min(tax_id) <> max(tax_id);

You can also phrase the having clause as:

having count(distinct tax_id) > 1;

However, count(distinct) is more expensive than just a min() or max()operation.

于 2013-07-19T20:33:31.647 回答