How to sum,multiply and divide in 1 Mysql
This has been bugging me for some time
table : stock
| customerID | Item | Quantity | Price |
| cus1 | A | 3 | 4 |
| cus1 | B | 2 | 3 |
| cus1 | C | 3 | 3 |
| cus2 | A | 3 | 3.50 |
| cus2 | C | 3 | 2 |
| cus3 | D | 6 | 4 |
how to make my mysql query generate view like this below
view : stock_v
| cusID | Item |Quan |Price |Tot_Qua | Tot_pri | sumtot_pri/tot_qua|
| cus1 | A | 3 | 4 | 8 | 12 | 3.375 |
| cus1 | B | 2 | 3 | 8 | 6 | 3.375 |
| cus1 | C | 3 | 3 | 8 | 9 | 3.375 |
| cus2 | A | 3 | 3.50 | 6 | 10.05 | 2.675 |
| cus2 | C | 3 | 2 | 6 | 6 | 2.675 |
| cus3 | D | 6 | 4 | 6 | 24 | 4.00 |
Example for cus1.
Cus1 have 3 item which is A,B and C.
so I want some formula for
Tot_Qua = 3+2+3 = 8
Tot_pri = price x quan
and Tot_pri must sum for this cus1 record,
sumtot_pri = (quan x price) + (quan x price) + (quan x price)
sumtot_pri = (12) + (6) + (9)
because cus1 have 3 item, and last one
sumtot_pri / Tot_qua = 27 / 8 = 3.375
Need group by I think because I want to see their item. I don't care if column tot_qua
and column sumtot_pri/tot_qua
will duplicate the same data for each Cus.
mysql sql
add a comment |
This has been bugging me for some time
table : stock
| customerID | Item | Quantity | Price |
| cus1 | A | 3 | 4 |
| cus1 | B | 2 | 3 |
| cus1 | C | 3 | 3 |
| cus2 | A | 3 | 3.50 |
| cus2 | C | 3 | 2 |
| cus3 | D | 6 | 4 |
how to make my mysql query generate view like this below
view : stock_v
| cusID | Item |Quan |Price |Tot_Qua | Tot_pri | sumtot_pri/tot_qua|
| cus1 | A | 3 | 4 | 8 | 12 | 3.375 |
| cus1 | B | 2 | 3 | 8 | 6 | 3.375 |
| cus1 | C | 3 | 3 | 8 | 9 | 3.375 |
| cus2 | A | 3 | 3.50 | 6 | 10.05 | 2.675 |
| cus2 | C | 3 | 2 | 6 | 6 | 2.675 |
| cus3 | D | 6 | 4 | 6 | 24 | 4.00 |
Example for cus1.
Cus1 have 3 item which is A,B and C.
so I want some formula for
Tot_Qua = 3+2+3 = 8
Tot_pri = price x quan
and Tot_pri must sum for this cus1 record,
sumtot_pri = (quan x price) + (quan x price) + (quan x price)
sumtot_pri = (12) + (6) + (9)
because cus1 have 3 item, and last one
sumtot_pri / Tot_qua = 27 / 8 = 3.375
Need group by I think because I want to see their item. I don't care if column tot_qua
and column sumtot_pri/tot_qua
will duplicate the same data for each Cus.
mysql sql
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 26 '18 at 21:59
add a comment |
This has been bugging me for some time
table : stock
| customerID | Item | Quantity | Price |
| cus1 | A | 3 | 4 |
| cus1 | B | 2 | 3 |
| cus1 | C | 3 | 3 |
| cus2 | A | 3 | 3.50 |
| cus2 | C | 3 | 2 |
| cus3 | D | 6 | 4 |
how to make my mysql query generate view like this below
view : stock_v
| cusID | Item |Quan |Price |Tot_Qua | Tot_pri | sumtot_pri/tot_qua|
| cus1 | A | 3 | 4 | 8 | 12 | 3.375 |
| cus1 | B | 2 | 3 | 8 | 6 | 3.375 |
| cus1 | C | 3 | 3 | 8 | 9 | 3.375 |
| cus2 | A | 3 | 3.50 | 6 | 10.05 | 2.675 |
| cus2 | C | 3 | 2 | 6 | 6 | 2.675 |
| cus3 | D | 6 | 4 | 6 | 24 | 4.00 |
Example for cus1.
Cus1 have 3 item which is A,B and C.
so I want some formula for
Tot_Qua = 3+2+3 = 8
Tot_pri = price x quan
and Tot_pri must sum for this cus1 record,
sumtot_pri = (quan x price) + (quan x price) + (quan x price)
sumtot_pri = (12) + (6) + (9)
because cus1 have 3 item, and last one
sumtot_pri / Tot_qua = 27 / 8 = 3.375
Need group by I think because I want to see their item. I don't care if column tot_qua
and column sumtot_pri/tot_qua
will duplicate the same data for each Cus.
mysql sql
This has been bugging me for some time
table : stock
| customerID | Item | Quantity | Price |
| cus1 | A | 3 | 4 |
| cus1 | B | 2 | 3 |
| cus1 | C | 3 | 3 |
| cus2 | A | 3 | 3.50 |
| cus2 | C | 3 | 2 |
| cus3 | D | 6 | 4 |
how to make my mysql query generate view like this below
view : stock_v
| cusID | Item |Quan |Price |Tot_Qua | Tot_pri | sumtot_pri/tot_qua|
| cus1 | A | 3 | 4 | 8 | 12 | 3.375 |
| cus1 | B | 2 | 3 | 8 | 6 | 3.375 |
| cus1 | C | 3 | 3 | 8 | 9 | 3.375 |
| cus2 | A | 3 | 3.50 | 6 | 10.05 | 2.675 |
| cus2 | C | 3 | 2 | 6 | 6 | 2.675 |
| cus3 | D | 6 | 4 | 6 | 24 | 4.00 |
Example for cus1.
Cus1 have 3 item which is A,B and C.
so I want some formula for
Tot_Qua = 3+2+3 = 8
Tot_pri = price x quan
and Tot_pri must sum for this cus1 record,
sumtot_pri = (quan x price) + (quan x price) + (quan x price)
sumtot_pri = (12) + (6) + (9)
because cus1 have 3 item, and last one
sumtot_pri / Tot_qua = 27 / 8 = 3.375
Need group by I think because I want to see their item. I don't care if column tot_qua
and column sumtot_pri/tot_qua
will duplicate the same data for each Cus.
mysql sql
mysql sql
edited Nov 23 '18 at 3:47
Gordon Linoff
788k35312417
788k35312417
asked Nov 23 '18 at 3:35
user2431391user2431391
84
84
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 26 '18 at 21:59
add a comment |
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 26 '18 at 21:59
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 26 '18 at 21:59
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 26 '18 at 21:59
add a comment |
3 Answers
3
active
oldest
votes
In MySQL 8+ you simply use window functions:
select s.*,
sum(quan) over (partition by cusid) as tot_quan,
(quan * price) as tot_price,
sum(quan * price) over (partition by cusid) / sum(quan) over (partition by cusid) as ratio
from stock s;
In earlier versions, you would use subqueries or a similar mechanism. Probably join
and group by
is simplest:
select s.*, s2.tot_quan, (s.quan * s.price) as tot_price,
tot_tot_price / tot_quan as ratio
from stock s join
(select cusid, sum(quan) as tot_quan,
sum(quan * price) as tot_tot_price
from stock s2
group by cusid
) s2
using (cusid);
add a comment |
This query will give you the results you want. It performs all the aggregation you need in a subquery then JOIN
s that to the main table to give the desired output:
SELECT s.customerID
, Item
, Quantity
, Price
, Tot_Qua
, Quantity * Price AS Tot_pri
, Avg_Pri
FROM stock s
JOIN (SELECT customerID
, SUM(Quantity) AS Tot_Qua
, SUM(Quantity * Price) / SUM(Quantity) AS Avg_Pri
FROM stock
GROUP BY customerID) s1
ON s1.customerID = s.customerID
Output:
customerID Item Quantity Price Tot_Qua Tot_pri Avg_Pri
cus1 A 3 4 8 12 3.375
cus1 B 2 3 8 6 3.375
cus1 C 3 3 8 9 3.375
cus2 A 3 3.5 6 10.5 2.75
cus2 C 3 2 6 6 2.75
cus3 D 6 4 6 24 4
Demo on dbfiddle
add a comment |
select customerID, item, quantity, price,
(select sum(quantity) from stock s2 where stock.customerID = s2.customerID ) as tot_qua,
price * quantity as tot_pri,
(select sum(quantity * price) / sum(quantity) from stock s2 where stock.customerID = s2.customerID) as `sumtotpri/tot_qua`
from stock
order by stock.customerID, stock.item;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53440374%2fhow-to-sum-multiply-and-divide-in-1-mysql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In MySQL 8+ you simply use window functions:
select s.*,
sum(quan) over (partition by cusid) as tot_quan,
(quan * price) as tot_price,
sum(quan * price) over (partition by cusid) / sum(quan) over (partition by cusid) as ratio
from stock s;
In earlier versions, you would use subqueries or a similar mechanism. Probably join
and group by
is simplest:
select s.*, s2.tot_quan, (s.quan * s.price) as tot_price,
tot_tot_price / tot_quan as ratio
from stock s join
(select cusid, sum(quan) as tot_quan,
sum(quan * price) as tot_tot_price
from stock s2
group by cusid
) s2
using (cusid);
add a comment |
In MySQL 8+ you simply use window functions:
select s.*,
sum(quan) over (partition by cusid) as tot_quan,
(quan * price) as tot_price,
sum(quan * price) over (partition by cusid) / sum(quan) over (partition by cusid) as ratio
from stock s;
In earlier versions, you would use subqueries or a similar mechanism. Probably join
and group by
is simplest:
select s.*, s2.tot_quan, (s.quan * s.price) as tot_price,
tot_tot_price / tot_quan as ratio
from stock s join
(select cusid, sum(quan) as tot_quan,
sum(quan * price) as tot_tot_price
from stock s2
group by cusid
) s2
using (cusid);
add a comment |
In MySQL 8+ you simply use window functions:
select s.*,
sum(quan) over (partition by cusid) as tot_quan,
(quan * price) as tot_price,
sum(quan * price) over (partition by cusid) / sum(quan) over (partition by cusid) as ratio
from stock s;
In earlier versions, you would use subqueries or a similar mechanism. Probably join
and group by
is simplest:
select s.*, s2.tot_quan, (s.quan * s.price) as tot_price,
tot_tot_price / tot_quan as ratio
from stock s join
(select cusid, sum(quan) as tot_quan,
sum(quan * price) as tot_tot_price
from stock s2
group by cusid
) s2
using (cusid);
In MySQL 8+ you simply use window functions:
select s.*,
sum(quan) over (partition by cusid) as tot_quan,
(quan * price) as tot_price,
sum(quan * price) over (partition by cusid) / sum(quan) over (partition by cusid) as ratio
from stock s;
In earlier versions, you would use subqueries or a similar mechanism. Probably join
and group by
is simplest:
select s.*, s2.tot_quan, (s.quan * s.price) as tot_price,
tot_tot_price / tot_quan as ratio
from stock s join
(select cusid, sum(quan) as tot_quan,
sum(quan * price) as tot_tot_price
from stock s2
group by cusid
) s2
using (cusid);
answered Nov 23 '18 at 3:44
Gordon LinoffGordon Linoff
788k35312417
788k35312417
add a comment |
add a comment |
This query will give you the results you want. It performs all the aggregation you need in a subquery then JOIN
s that to the main table to give the desired output:
SELECT s.customerID
, Item
, Quantity
, Price
, Tot_Qua
, Quantity * Price AS Tot_pri
, Avg_Pri
FROM stock s
JOIN (SELECT customerID
, SUM(Quantity) AS Tot_Qua
, SUM(Quantity * Price) / SUM(Quantity) AS Avg_Pri
FROM stock
GROUP BY customerID) s1
ON s1.customerID = s.customerID
Output:
customerID Item Quantity Price Tot_Qua Tot_pri Avg_Pri
cus1 A 3 4 8 12 3.375
cus1 B 2 3 8 6 3.375
cus1 C 3 3 8 9 3.375
cus2 A 3 3.5 6 10.5 2.75
cus2 C 3 2 6 6 2.75
cus3 D 6 4 6 24 4
Demo on dbfiddle
add a comment |
This query will give you the results you want. It performs all the aggregation you need in a subquery then JOIN
s that to the main table to give the desired output:
SELECT s.customerID
, Item
, Quantity
, Price
, Tot_Qua
, Quantity * Price AS Tot_pri
, Avg_Pri
FROM stock s
JOIN (SELECT customerID
, SUM(Quantity) AS Tot_Qua
, SUM(Quantity * Price) / SUM(Quantity) AS Avg_Pri
FROM stock
GROUP BY customerID) s1
ON s1.customerID = s.customerID
Output:
customerID Item Quantity Price Tot_Qua Tot_pri Avg_Pri
cus1 A 3 4 8 12 3.375
cus1 B 2 3 8 6 3.375
cus1 C 3 3 8 9 3.375
cus2 A 3 3.5 6 10.5 2.75
cus2 C 3 2 6 6 2.75
cus3 D 6 4 6 24 4
Demo on dbfiddle
add a comment |
This query will give you the results you want. It performs all the aggregation you need in a subquery then JOIN
s that to the main table to give the desired output:
SELECT s.customerID
, Item
, Quantity
, Price
, Tot_Qua
, Quantity * Price AS Tot_pri
, Avg_Pri
FROM stock s
JOIN (SELECT customerID
, SUM(Quantity) AS Tot_Qua
, SUM(Quantity * Price) / SUM(Quantity) AS Avg_Pri
FROM stock
GROUP BY customerID) s1
ON s1.customerID = s.customerID
Output:
customerID Item Quantity Price Tot_Qua Tot_pri Avg_Pri
cus1 A 3 4 8 12 3.375
cus1 B 2 3 8 6 3.375
cus1 C 3 3 8 9 3.375
cus2 A 3 3.5 6 10.5 2.75
cus2 C 3 2 6 6 2.75
cus3 D 6 4 6 24 4
Demo on dbfiddle
This query will give you the results you want. It performs all the aggregation you need in a subquery then JOIN
s that to the main table to give the desired output:
SELECT s.customerID
, Item
, Quantity
, Price
, Tot_Qua
, Quantity * Price AS Tot_pri
, Avg_Pri
FROM stock s
JOIN (SELECT customerID
, SUM(Quantity) AS Tot_Qua
, SUM(Quantity * Price) / SUM(Quantity) AS Avg_Pri
FROM stock
GROUP BY customerID) s1
ON s1.customerID = s.customerID
Output:
customerID Item Quantity Price Tot_Qua Tot_pri Avg_Pri
cus1 A 3 4 8 12 3.375
cus1 B 2 3 8 6 3.375
cus1 C 3 3 8 9 3.375
cus2 A 3 3.5 6 10.5 2.75
cus2 C 3 2 6 6 2.75
cus3 D 6 4 6 24 4
Demo on dbfiddle
answered Nov 23 '18 at 3:49
NickNick
35.7k132243
35.7k132243
add a comment |
add a comment |
select customerID, item, quantity, price,
(select sum(quantity) from stock s2 where stock.customerID = s2.customerID ) as tot_qua,
price * quantity as tot_pri,
(select sum(quantity * price) / sum(quantity) from stock s2 where stock.customerID = s2.customerID) as `sumtotpri/tot_qua`
from stock
order by stock.customerID, stock.item;
add a comment |
select customerID, item, quantity, price,
(select sum(quantity) from stock s2 where stock.customerID = s2.customerID ) as tot_qua,
price * quantity as tot_pri,
(select sum(quantity * price) / sum(quantity) from stock s2 where stock.customerID = s2.customerID) as `sumtotpri/tot_qua`
from stock
order by stock.customerID, stock.item;
add a comment |
select customerID, item, quantity, price,
(select sum(quantity) from stock s2 where stock.customerID = s2.customerID ) as tot_qua,
price * quantity as tot_pri,
(select sum(quantity * price) / sum(quantity) from stock s2 where stock.customerID = s2.customerID) as `sumtotpri/tot_qua`
from stock
order by stock.customerID, stock.item;
select customerID, item, quantity, price,
(select sum(quantity) from stock s2 where stock.customerID = s2.customerID ) as tot_qua,
price * quantity as tot_pri,
(select sum(quantity * price) / sum(quantity) from stock s2 where stock.customerID = s2.customerID) as `sumtotpri/tot_qua`
from stock
order by stock.customerID, stock.item;
edited Nov 23 '18 at 3:51
answered Nov 23 '18 at 3:43
DanBDanB
1,7541315
1,7541315
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53440374%2fhow-to-sum-multiply-and-divide-in-1-mysql%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
Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. See stackoverflow.com/help/someone-answers
– Nick
Nov 26 '18 at 21:59