stucklucky
stucklucky

Reputation: 77

How to combine all rows with same data in column?

Let's say I have a table name receipts. Then the table has a receipt_num, product, and price.

Let's say I have a table that looks like this.

receipt_num | item | price
    --------+------+------
    6       | a    | 10
    6       | b    | 15
    1       | c    | 7

What SQL query can I use to get something like this.

receipt_num | price
    --------+------+------
    6       | 25
    1       | 7

Essentially I am combining all the rows with the same receipt_num, excluding the item column and adding up the total price.

I have tried using SELECT receipt_num, price FROM receipts. I am very new at this so I am not sure what I am doing wrong.

Upvotes: 0

Views: 60

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269503

This is an aggregation query -- a very basic SQL operation:

select receipt_num, sum(price)
from receipts
group by receipt_num;

I strongly encourage you to find tutorials, books, lessons or something so you can learn the fundamental ideas in SQL.

Upvotes: 2

Related Questions