Q:

Using the SUM function in SQLITE

I have this basic sqlite3 database. In my store table, I am focused on the numberOfItems category. e.g., in my PurchaseToStore table, 4 different stores buy tacos with the number of them (numberOfItems) of 5, 55, 55, 55.  What I want to do is calculate the sum of those values. So (55 + 55 + 55 + 5) = 170.
CREATE TABLE Store(

storeName VARCHAR(30),
storeid INTEGER,
PRIMARY KEY(storeid)

);

CREATE TABLE item(

itemName VARCHAR(30) ,
itemId INTEGER,
PRIMARY KEY(itemId)

);

CREATE TABLE PurchasedToStore(

storeid INTEGER,
itemId INTEGER,
numberOfItems INTEGER,
expirationMonth Integer,
PRIMARY KEY(storeid, itemID),
FOREIGN KEY(storeid) REFERENCES STORE(storeid),
FOREIGN KEY(itemid) REFERENCES item(itemid)

);

INSERT INTO Store Store('Meijer', 1);
INSERT INTO Store VALUES('Kroger', 2);
INSERT INTO Store VALUES('Walmart', 3);
INSERT INTO Store VALUES('Kmart', 4);

INSERT INTO item VALUES('taco', 556);
INSERT INTO item VALUES('burrito', 557);
INSERT INTO item VALUES('cheese', 600);

INSERT INTO STORE VALUES(1, 556, 5, 12);
INSERT INTO STORE VALUES(2, 556, 55, 11);
INSERT INTO STORE VALUES(3, 556, 55, 10);
INSERT INTO STORE VALUES(4, 556, 55, 2);

Here is what I have to try to do this:
SELECT sum(numberOfItems)
FROM item join purchasetostore
WHERE storeid = itemid;

A:

It's a bit unclear what the end goal is, but a slight change to your where condition will get your desired sum. You'll want to match up itemid from the two tables;
SELECT sum(numberOfItems)
FROM item join PurchasedToStore
WHERE item.itemid = PurchasedToStore.itemid;

A probably slightly more useful query could include the item name of the items, and sum per name;
SELECT item.itemname, sum(numberOfItems)
FROM item join PurchasedToStore
WHERE item.itemid = PurchasedToStore.itemid
GROUP BY item.itemname;

A simple SQLfiddle with both queries.

