lundi 2 mars 2020

counting number of cars that each owner has

I'm trying to learn plsql and got stuck in understanding some basic stuff. Here is a challenge that I'm trying to solve. I have two tables. One holds information about owners and the other is information about cars. I want to to write an anonymous block that joins these two tables and with a for loop based on amount of cars that is registered to each owner prints how many cars each person own. furthermore I want an if statement which distinguishes between 1 Car (singular) and 2, 3 Cars (plural).

the tables are these:

CREATE TABLE owners(
id_nr VARCHAR2(13) PRIMARY KEY,
f_name VARCHAR2(20),
s_name VARCHAR2(20));

CREATE TABLE cars(
reg_nr VARCHAR2(6) PRIMARY KEY,
id_nr REFERENCES owners(pnr),
model VARCHAR2(20),
year NUMBER(4),
date DATE);

The result may look like something like this:

19380321-7799, Hans, Anderson, Owns: 1 car

19490321-7899, Mike, Erikson, Owns: 2 cars

. . . etc.

I tried many different ways but each time i get some errors. I would appreciate any help and hints that helps me understand it.

Thanks!

Aucun commentaire:

Enregistrer un commentaire