Home > Mobile >  SQLite query not working when trying to run the same query in postgresql
SQLite query not working when trying to run the same query in postgresql

Time:01-12

I have a database of users purchase/sell of stocks and need to retrieve the data by summing all the shares for that specific user.

Ex: If I choose user_id = 7, it should return two rows, one with TESLA and one with Apple with the sums of the shares.

Database: enter image description here

SQL queries I've tried include:

SELECT name,symbol,name,price,total, SUM(shares) 
FROM symbol 
WHERE user_id=7 
GROUP BY name,symbol,name,price,total

Returns: but it should just be two rows enter image description here

CodePudding user response:

Your issue is your grouping.

Consider the uniqueness of what you are grouping by and that is how many rows you will get returned.

With your where clause and grouping by name,symbol,name,price,total there are three unique rows.

Remove the price and total columns from the grouping and you will get your desired two rows, or include them in your query as sum'd columns.

eg.

SELECT name,symbol,name,SUM(shares) 
FROM symbol 
WHERE user_id=7 
GROUP BY name,symbol,name
  •  Tags:  
  • Related