I'm trying to retrieve each entry from the table "Component" which appears to be in more than one "Computer". When I perform :
select Component.Piece, NO_COMPUTER from Component cmp
inner join Computer cpt
on Component.Piece = cpt.Piece
where Piece like 'i7 9210'
I get a result of two lines which is correct (like my 'i7 9210' is actually in two computers)
However now when I try to do that for each entry using :
select Component.Piece, NO_COMPUTER from Component cmp
inner join Computer cpt
on Component.Piece = cpt.Piece
GROUP BY Component.Piece, NO_COMPUTER
HAVING COUNT(DISTINCT cpt.NO_COMPUTER) > 1`
I have nothing in return
I tried to remove the DISTINCT
but I can't figure out what's wrong
1条答案
按热度按时间jk9hmnmh1#
Presumably, this is the query you wanted to write:
This joins the two tables, groups by component (assuming that piece and no_computer can be used as a primary key), and filters on tuples that have more than one match.
However, I find that this kind of filtering logic would be better expressed with a correlated subquery: