Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Group by and having clause  RSS feed

 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the below product table :

I want to find out the makers who produce only one product type and more than one model.
The answer is

So far, I have written this query:

It gives the below result:

My question is how to write condition to get result for 1 type only ? as writing having count(type)=1 removes maker D which is required.
 
Stephan van Hulst
Saloon Keeper
Posts: 7190
118
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's take a step back and first create a result table that shows how many types each maker has. That means you should group by just the maker, and not by the maker and the type.

This is the result I want, build a query to achieve it:

makernumber of types
A3
B2
C1
D1
E2
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be like :

select maker from <table-name> group by maker having count(maker) = 1
 
Stephan van Hulst
Saloon Keeper
Posts: 7190
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you guessing, Tushar, or are you testing your query against an actual DBMS? You'll find that your query does not return the result I asked Pramod to give me.

Your query likely gives me the following result:

A
B
C
D
E
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!