Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Group by and having clause

 
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 13493
305
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13493
305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic