How to combine monthly results into a single row?
up vote
0
down vote
favorite
If have a sales table with data like:
SALES
-----
seller_id month amount
1 1 10
1 2 15
I would like to retrieve all seller's sales in a single row. How can I combine the results from the table into a result like this?
seller_id jan_amount feb_amount
1 10 15
EDIT:
I can retrieve the monthly sales with this query:
select seller_id, month, sum(amount)
from sales
group by sellerd_id, month
But it gives me results like:
1,1,10
1,2,15
I'm hoping to transform this into:
1, 10 as JAN_AMOUNT, 15 as FEB_AMONT
sql oracle pivot
|
show 3 more comments
up vote
0
down vote
favorite
If have a sales table with data like:
SALES
-----
seller_id month amount
1 1 10
1 2 15
I would like to retrieve all seller's sales in a single row. How can I combine the results from the table into a result like this?
seller_id jan_amount feb_amount
1 10 15
EDIT:
I can retrieve the monthly sales with this query:
select seller_id, month, sum(amount)
from sales
group by sellerd_id, month
But it gives me results like:
1,1,10
1,2,15
I'm hoping to transform this into:
1, 10 as JAN_AMOUNT, 15 as FEB_AMONT
sql oracle pivot
What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
– Brad
Nov 9 at 19:23
Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
– Bast
Nov 9 at 19:25
@Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
– ab11
Nov 9 at 19:28
1
You need to look into PIVOT
– Brad
Nov 9 at 19:32
1
Have you ever heard ofPIVOT
?
– Eric
Nov 9 at 19:36
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
If have a sales table with data like:
SALES
-----
seller_id month amount
1 1 10
1 2 15
I would like to retrieve all seller's sales in a single row. How can I combine the results from the table into a result like this?
seller_id jan_amount feb_amount
1 10 15
EDIT:
I can retrieve the monthly sales with this query:
select seller_id, month, sum(amount)
from sales
group by sellerd_id, month
But it gives me results like:
1,1,10
1,2,15
I'm hoping to transform this into:
1, 10 as JAN_AMOUNT, 15 as FEB_AMONT
sql oracle pivot
If have a sales table with data like:
SALES
-----
seller_id month amount
1 1 10
1 2 15
I would like to retrieve all seller's sales in a single row. How can I combine the results from the table into a result like this?
seller_id jan_amount feb_amount
1 10 15
EDIT:
I can retrieve the monthly sales with this query:
select seller_id, month, sum(amount)
from sales
group by sellerd_id, month
But it gives me results like:
1,1,10
1,2,15
I'm hoping to transform this into:
1, 10 as JAN_AMOUNT, 15 as FEB_AMONT
sql oracle pivot
sql oracle pivot
edited Nov 9 at 20:00
Barbaros Özhan
11k71430
11k71430
asked Nov 9 at 19:20
ab11
9,0583286171
9,0583286171
What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
– Brad
Nov 9 at 19:23
Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
– Bast
Nov 9 at 19:25
@Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
– ab11
Nov 9 at 19:28
1
You need to look into PIVOT
– Brad
Nov 9 at 19:32
1
Have you ever heard ofPIVOT
?
– Eric
Nov 9 at 19:36
|
show 3 more comments
What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
– Brad
Nov 9 at 19:23
Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
– Bast
Nov 9 at 19:25
@Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
– ab11
Nov 9 at 19:28
1
You need to look into PIVOT
– Brad
Nov 9 at 19:32
1
Have you ever heard ofPIVOT
?
– Eric
Nov 9 at 19:36
What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
– Brad
Nov 9 at 19:23
What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
– Brad
Nov 9 at 19:23
Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
– Bast
Nov 9 at 19:25
Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
– Bast
Nov 9 at 19:25
@Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
– ab11
Nov 9 at 19:28
@Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
– ab11
Nov 9 at 19:28
1
1
You need to look into PIVOT
– Brad
Nov 9 at 19:32
You need to look into PIVOT
– Brad
Nov 9 at 19:32
1
1
Have you ever heard of
PIVOT
?– Eric
Nov 9 at 19:36
Have you ever heard of
PIVOT
?– Eric
Nov 9 at 19:36
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You may use conditional aggregation to pivot your data with the following SQL statement as :
select seller_id,
max(case when month = 1 then amount end ) as jan_amount,
max(case when month = 2 then amount end ) as feb_amount
from sales
where seller_id = 1
group by seller_id;
SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15
As an Oracle user, you may use the following SQL statement with pivot
keyword as :
select *
from sales
pivot(
sum(amount)
for(month)
in (1 as jan_amount ,2 as feb_amount )
);
SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15
Rextester Demo
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You may use conditional aggregation to pivot your data with the following SQL statement as :
select seller_id,
max(case when month = 1 then amount end ) as jan_amount,
max(case when month = 2 then amount end ) as feb_amount
from sales
where seller_id = 1
group by seller_id;
SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15
As an Oracle user, you may use the following SQL statement with pivot
keyword as :
select *
from sales
pivot(
sum(amount)
for(month)
in (1 as jan_amount ,2 as feb_amount )
);
SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15
Rextester Demo
add a comment |
up vote
2
down vote
accepted
You may use conditional aggregation to pivot your data with the following SQL statement as :
select seller_id,
max(case when month = 1 then amount end ) as jan_amount,
max(case when month = 2 then amount end ) as feb_amount
from sales
where seller_id = 1
group by seller_id;
SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15
As an Oracle user, you may use the following SQL statement with pivot
keyword as :
select *
from sales
pivot(
sum(amount)
for(month)
in (1 as jan_amount ,2 as feb_amount )
);
SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15
Rextester Demo
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You may use conditional aggregation to pivot your data with the following SQL statement as :
select seller_id,
max(case when month = 1 then amount end ) as jan_amount,
max(case when month = 2 then amount end ) as feb_amount
from sales
where seller_id = 1
group by seller_id;
SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15
As an Oracle user, you may use the following SQL statement with pivot
keyword as :
select *
from sales
pivot(
sum(amount)
for(month)
in (1 as jan_amount ,2 as feb_amount )
);
SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15
Rextester Demo
You may use conditional aggregation to pivot your data with the following SQL statement as :
select seller_id,
max(case when month = 1 then amount end ) as jan_amount,
max(case when month = 2 then amount end ) as feb_amount
from sales
where seller_id = 1
group by seller_id;
SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15
As an Oracle user, you may use the following SQL statement with pivot
keyword as :
select *
from sales
pivot(
sum(amount)
for(month)
in (1 as jan_amount ,2 as feb_amount )
);
SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15
Rextester Demo
edited Nov 10 at 8:57
answered Nov 9 at 19:27
Barbaros Özhan
11k71430
11k71430
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232055%2fhow-to-combine-monthly-results-into-a-single-row%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
– Brad
Nov 9 at 19:23
Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
– Bast
Nov 9 at 19:25
@Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
– ab11
Nov 9 at 19:28
1
You need to look into PIVOT
– Brad
Nov 9 at 19:32
1
Have you ever heard of
PIVOT
?– Eric
Nov 9 at 19:36