I have a database with the following tables:
CREATE TABLE PAR (
ID varchar(10) not null,
Name varchar(20) not null,
State varchar(20) not null,
primary key (ID)
);
INSERT INTO PAR (ID, Name, State)
VALUES
('A000','Susan','New York'),
('B123','Bob','Texas'),
('C456','Tony','Washington'),
('D789','Adam','California');
CREATE TABLE GROUP (
GroupID varchar(25) not null,
Person1 varchar(15) not null,
Person2 varchar(15) not null,
Person3 varchar(15),
Person4 varchar(15),
primary key (GroupID)
);
INSERT INTO GROUP (GroupID, Person1, Person2, Person3, Person4)
VALUES
('G234','A000','B666','C777'),
('G456','B123','D898','E632'),
('G789','C456', null, null),
('G444','D789','O123', null);
CREATE TABLE SOLO (
Classification varchar(15) not null,
ID varchar(10) not null,
Fruits varchar(10) not null,
primary key (Classification, ID)
foreign key (ID) references PAR(ID)
);
INSERT INTO SOLO(Classification, ID, Fruits)
VALUES
('A Group','A000','Apple'),
('A Group','B123','Banana'),
('B Group','C456','Orange');
CREATE TABLE GROUPINGS (
Classification varchar(20) not null,
Group varchar(25) not null,
Fruits varchar(15) not null,
primary key (Classification, Group)
foreign key (Group) references GROUP(GroupID)
);
INSERT INTO GROUPINGS(Classification, Group, Fruits)
VALUES
('A Group','G234','Apple'),
('B Group','G456','Grape'),
('A Group','G789','Banana'),
('D Group','G444','Orange');
I am trying to get the following result from my query:
State| Fruits
------+-------
New York| 2
Texas | 2
Washington| 2
California| 1
I created a query that looks like this but it does not work: I know that I need to sum up all the different fruits from the tables and I have use common keys in order to this but I am not sure how as I am quite new to SQL queries
SELECT P.State, COUNT(S.ID) + COUNT(z.Group) as gSum
FROM PARTICIPANT AS P, SOLO AS S,
GROUPINGS AS G, GROUP as Z
WHERE z.GroupID = G.Group
GROUP BY P.STATE
I need to figure out how I can link the ID key in PAR table with each person column in GROUP table