Skip to content
Advertisement

How to create a view using data from two tables

I have two tables

ITEM

  • id (int),
  • model (varchar),
  • weight (float),
  • fk_type (int)

TYPE

  • id (int),
  • name (varchar)

And I want to create a view with

  • TypeName (for every type name) –> varchar
  • NumberOfItems (total number of ITEMS for TypeName) —> int
  • NumberOfModels (total number of MODELS for TypeName) —> int
  • TotWeight (again sum of all the ITEMS weight per TypeName) —>float

For example: I have three pencils, two of them have the same model, and one rubber. I’m expecting to get something like this.

TypeName NumberOfItems NumberOfModels TotWeight
pencil 3 2 50
rubber 1 1 25

I tried something like this but I can’t get my head around it

Can someone help?

Advertisement

Answer

This is basically an aggregation query with a join:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement