How can I have Sub Sum column in sql query? - sql

I have the following table in Sqlite
Person1 Person2 Amount
A X 1000
A Y 2000
A Z 5000
B X 3000
B Y 4000
how can I write a query to add an extra column that its value is the sum of Amount for each Person in Column Person1.
I need the following result:
Person1 Person2 Amount SumAmountPerson1
A X 1000 8000
A Y 2000 8000
A Z 5000 8000
B X 3000 5000
B Y 2000 5000
my query is:
select *, Sum(Amount) from MyTable
GROUP by Person1
But it returns the following result set in Sqlite:
Person1 Person2 Amount SumAmountPerson1
A X 1000 8000
B X 3000 5000
But I need all rows

SQLite does not support window functions, so one option to get your result would be to use a subquery to aggregate amounts by the first person, and then join this back to your original table.
SELECT t1.Person1,
t1.Person2,
t1.Amount,
t2.SumAmountPerson1
FROM yourTable t1
INNER JOIN
(
SELECT Person1, SUM(Amount) AS SumAmountPerson1
FROM yourTable
GROUP BY Person1
) t2
ON t1.Person1 = t2.Person1

Related

How to query for column to primary key relation? [duplicate]

I am stuck here. I have row in Postgres like this:
id amount
a 5000
a 1500
a 500
b 2000
b 1000
c 4000
How is the sql syntax to get result like this?
id amount
a 7000
b 3000
c 4000
SELECT id, SUM(amount) AS amount
FROM yourtable
GROUP BY id

SQL: Efficient way to get group by results including all table columns

Let's consider a simple table below.
id
code
marks
grade
1
X
100
A
2
Y
120
B
3
Z
130
A
4
X
120
C
5
Y
100
A
6
Z
110
B
7
X
150
A
8
X
140
C
Goal: Get maximum marks for each grade, return all the columns.
id
code
marks
grade
7
X
150
A
2
Y
120
B
8
X
140
C
This is very simple if I don't want id and code column
select grade, max(marks)
from table
group by grade;
What could be the most efficient query to get id and code column in the above query?
I tried something like this which didn't work
select * from table t
inner join
(select grade, max(marks)
from table
group by grade) a
on a.grade=t.grade;
In Postgres the most efficient way for this kind of query is to use (the proprietary) distinct on ()
select distinct on (grade) *
from the_table t
order by grade, marks desc;
Are you looking for a correlated subquery?
select t.*
from t
where t.marks = (select max(t2.marks) from t t2 where t2.grade = t.grade);

how to do group by on certain fields only in sql

I have the following query
select a,b,sum(c) from(
select a,b,c from table 1
union
select a,b,c from table 2
)
group by a,b
result of above sql is as follows:
A B C
Apple Red 100
Apple null 100
Pear Green 200
Pear null 300
instead of above result how can i get the following result:
A B C
Apple Red 200
Pear Green 500
Given your desired results, you want to GROUP BY your first column (A) and have a single value for your second column (B). You can use another aggregate such as max on the second column and remove it from the GROUP BY:
select a,max(b),sum(c) from(
select a,b,c from table1
union
select a,b,c from table2
) t
group by a
SQL Fiddle Demo
Note, if you have multiple distinct non-null values per group, this will sum them all to a single max group.

SQL query for selecting sum of multiple column based on other column

i have in database like this
say a table X with columns A and B
X
--
A B
1 300
1 400
1 100
2 200
2 400
and i want to write sql query to get result in format
P Q
1 800
2 600
Where Q is sum of all having same value of A.
I thought of selecting the data in same format and then using the logic display it
is there a way to do with SQL query?
SELECT A as P, SUM(B) AS Q
FROM X
GROUP BY A
select A as P
, sum(B) as Q
from X
group by
A
SELECT A AS P, SUM(B) AS Q
FROM TableA
GROUP BY A

how to group by and return sum row in Postgres

I am stuck here. I have row in Postgres like this:
id amount
a 5000
a 1500
a 500
b 2000
b 1000
c 4000
How is the sql syntax to get result like this?
id amount
a 7000
b 3000
c 4000
SELECT id, SUM(amount) AS amount
FROM yourtable
GROUP BY id