Home > Mobile >  How do i add rows in a table SQLite
How do i add rows in a table SQLite

Time:03-02

I have this table in SQLite3 and I don't know how to add the values of two rows from the same column...

question: Print the sum of Nr_student from Bucuresti and Timisoara. My idea looks like this: SELECT Nr_student(there is the problem Idk what to do in this point) AS Nr_students_TIMISOARA_BUCURESTI FROM Universities WHERE County ='Bucuresti' AND County='Timisoara' ;

CREATE TABLE Universities (Cod_university Number(4), Name VARCHAR2(20), County VARCHAR2(15), Nr_student NUMBER(5), Nr_bursieri NUMBER(5), Nr_specialty NUMBER(2))

INSERT INTO Universities VALUES (100, 'UBB', 'Cluj', 65310, 589, 20) INSERT INTO Universities VALUES (153, 'UPT', 'Timisoara', 59968, 945, 12) INSERT INTO Universities VALUES (106, 'UVT', 'Timisoara', 48962, 1000, 9) INSERT INTO Universities VALUES (231, 'UPET', 'Petrosani', 643, 30, 11) INSERT INTO Universities VALUES (51, 'UAIC', 'Iasi', 8684, 600, 26) INSERT INTO Universities VALUES (51, 'UNIBUC', 'Bucuresti', 812, 624, 32)

CodePudding user response:

You have a few errors in your code: Firstly for the insert, you have 6 separate inserts in your example. If you do this, they should be executed separately with semi-colons (;), but even better, you can just take the lists and separate them with commas with a single insert:

INSERT INTO Universities
VALUES
(100,'UBB','Cluj',65310,589,20),
(153,'UPT','Timisoara',59968,945,12),
(106,'UVT','Timisoara',48962,1000,9),
(231,'UPET','Petrosani',643,30,11),
(51,'UAIC','Iasi',8684,600,26),
(51,'UNIBUC','Bucuresti',812,624,32)

your query is wrong two, as you need to use an aggregate function (SUM) and a group by, and you can't have an AND on county, as each record can only have one county, you must use an OR

select
    county,
    sum(Nr_student)
from
    Universities u
where
    County = "Timisoara"
    or County = "Bucuresti"
group by
    county
  • Related