Why stored procedure returns wrong sum for each group?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have this query but SUM(SUM(inv.ServicePrice)) over () as TotalRevenueAllServices
return me the wrong sum. I actually want to get sum of all sums that I have done for each group but it returns wrong value.
Select top(3) s.ServiceName, Count(inv.fk_ServiceID) as TotalServicesCount, Sum(inv.ServicePrice) TotalServicesRevenue,
SUM(SUM(inv.ServicePrice)) over () as TotalRevenueAllServices
from Invoices inv
inner join Services s
on s.ServiceID= inv.fk_ServiceID
group by inv.fk_ServiceID, s.ServiceName
Order By ServiceName asc
sql sql-server tsql stored-procedures
|
show 1 more comment
I have this query but SUM(SUM(inv.ServicePrice)) over () as TotalRevenueAllServices
return me the wrong sum. I actually want to get sum of all sums that I have done for each group but it returns wrong value.
Select top(3) s.ServiceName, Count(inv.fk_ServiceID) as TotalServicesCount, Sum(inv.ServicePrice) TotalServicesRevenue,
SUM(SUM(inv.ServicePrice)) over () as TotalRevenueAllServices
from Invoices inv
inner join Services s
on s.ServiceID= inv.fk_ServiceID
group by inv.fk_ServiceID, s.ServiceName
Order By ServiceName asc
sql sql-server tsql stored-procedures
2
share sample data with expected output
– Zaynul Abadin Tuhin
Nov 22 '18 at 11:19
SUM({expr}) OVER ()
is going to return the sum of every row in the table. Why would you want toSUM
it again?
– Larnu
Nov 22 '18 at 11:22
Welcome to Stack Overflow. Your question doesn't include enough useful detail for us to help you. Check out How To Ask and the importance of a Minimal, Complete, Verifiable Example. After that, Start Here to edit your question as needed.
– Eric Brandt
Nov 22 '18 at 11:40
Do you want the sum of just thetop 3
or of all groups? Without sample data and results, your question is unclear.
– Gordon Linoff
Nov 22 '18 at 11:59
1
Note to self: @ZoharPeled: Don't post an answer based on assumptions.
– Zohar Peled
Nov 22 '18 at 13:43
|
show 1 more comment
I have this query but SUM(SUM(inv.ServicePrice)) over () as TotalRevenueAllServices
return me the wrong sum. I actually want to get sum of all sums that I have done for each group but it returns wrong value.
Select top(3) s.ServiceName, Count(inv.fk_ServiceID) as TotalServicesCount, Sum(inv.ServicePrice) TotalServicesRevenue,
SUM(SUM(inv.ServicePrice)) over () as TotalRevenueAllServices
from Invoices inv
inner join Services s
on s.ServiceID= inv.fk_ServiceID
group by inv.fk_ServiceID, s.ServiceName
Order By ServiceName asc
sql sql-server tsql stored-procedures
I have this query but SUM(SUM(inv.ServicePrice)) over () as TotalRevenueAllServices
return me the wrong sum. I actually want to get sum of all sums that I have done for each group but it returns wrong value.
Select top(3) s.ServiceName, Count(inv.fk_ServiceID) as TotalServicesCount, Sum(inv.ServicePrice) TotalServicesRevenue,
SUM(SUM(inv.ServicePrice)) over () as TotalRevenueAllServices
from Invoices inv
inner join Services s
on s.ServiceID= inv.fk_ServiceID
group by inv.fk_ServiceID, s.ServiceName
Order By ServiceName asc
sql sql-server tsql stored-procedures
sql sql-server tsql stored-procedures
asked Nov 22 '18 at 11:16
azure boyazure boy
1467
1467
2
share sample data with expected output
– Zaynul Abadin Tuhin
Nov 22 '18 at 11:19
SUM({expr}) OVER ()
is going to return the sum of every row in the table. Why would you want toSUM
it again?
– Larnu
Nov 22 '18 at 11:22
Welcome to Stack Overflow. Your question doesn't include enough useful detail for us to help you. Check out How To Ask and the importance of a Minimal, Complete, Verifiable Example. After that, Start Here to edit your question as needed.
– Eric Brandt
Nov 22 '18 at 11:40
Do you want the sum of just thetop 3
or of all groups? Without sample data and results, your question is unclear.
– Gordon Linoff
Nov 22 '18 at 11:59
1
Note to self: @ZoharPeled: Don't post an answer based on assumptions.
– Zohar Peled
Nov 22 '18 at 13:43
|
show 1 more comment
2
share sample data with expected output
– Zaynul Abadin Tuhin
Nov 22 '18 at 11:19
SUM({expr}) OVER ()
is going to return the sum of every row in the table. Why would you want toSUM
it again?
– Larnu
Nov 22 '18 at 11:22
Welcome to Stack Overflow. Your question doesn't include enough useful detail for us to help you. Check out How To Ask and the importance of a Minimal, Complete, Verifiable Example. After that, Start Here to edit your question as needed.
– Eric Brandt
Nov 22 '18 at 11:40
Do you want the sum of just thetop 3
or of all groups? Without sample data and results, your question is unclear.
– Gordon Linoff
Nov 22 '18 at 11:59
1
Note to self: @ZoharPeled: Don't post an answer based on assumptions.
– Zohar Peled
Nov 22 '18 at 13:43
2
2
share sample data with expected output
– Zaynul Abadin Tuhin
Nov 22 '18 at 11:19
share sample data with expected output
– Zaynul Abadin Tuhin
Nov 22 '18 at 11:19
SUM({expr}) OVER ()
is going to return the sum of every row in the table. Why would you want to SUM
it again?– Larnu
Nov 22 '18 at 11:22
SUM({expr}) OVER ()
is going to return the sum of every row in the table. Why would you want to SUM
it again?– Larnu
Nov 22 '18 at 11:22
Welcome to Stack Overflow. Your question doesn't include enough useful detail for us to help you. Check out How To Ask and the importance of a Minimal, Complete, Verifiable Example. After that, Start Here to edit your question as needed.
– Eric Brandt
Nov 22 '18 at 11:40
Welcome to Stack Overflow. Your question doesn't include enough useful detail for us to help you. Check out How To Ask and the importance of a Minimal, Complete, Verifiable Example. After that, Start Here to edit your question as needed.
– Eric Brandt
Nov 22 '18 at 11:40
Do you want the sum of just the
top 3
or of all groups? Without sample data and results, your question is unclear.– Gordon Linoff
Nov 22 '18 at 11:59
Do you want the sum of just the
top 3
or of all groups? Without sample data and results, your question is unclear.– Gordon Linoff
Nov 22 '18 at 11:59
1
1
Note to self: @ZoharPeled: Don't post an answer based on assumptions.
– Zohar Peled
Nov 22 '18 at 13:43
Note to self: @ZoharPeled: Don't post an answer based on assumptions.
– Zohar Peled
Nov 22 '18 at 13:43
|
show 1 more comment
1 Answer
1
active
oldest
votes
declare @FromDate date= '22/Nov/2017',
@ToDate date= '24/Nov/2018'
Set @ToDate= ISNULL(@ToDate, getdate());
with MainTable
as
(
Select top(3) s.ServiceName, Count(inv.fk_ServiceID) as TotalServicesCount, Sum(inv.ServicePrice) TotalServicesRevenue
from Invoices inv
inner join Services s
on s.ServiceID= inv.fk_ServiceID
--where CAST(inv.EntryDateTime as DATE) between @FromDate and @ToDate
group by inv.fk_ServiceID, s.ServiceName
Order By ServiceName asc
)
select * , (select sum(totalservicesrevenue) from MainTable) 'AllServicesRevenue'
, (select sum(TotalServicesCount) from MainTable) 'OverallServices'
from MainTable
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%2f53429776%2fwhy-stored-procedure-returns-wrong-sum-for-each-group%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
declare @FromDate date= '22/Nov/2017',
@ToDate date= '24/Nov/2018'
Set @ToDate= ISNULL(@ToDate, getdate());
with MainTable
as
(
Select top(3) s.ServiceName, Count(inv.fk_ServiceID) as TotalServicesCount, Sum(inv.ServicePrice) TotalServicesRevenue
from Invoices inv
inner join Services s
on s.ServiceID= inv.fk_ServiceID
--where CAST(inv.EntryDateTime as DATE) between @FromDate and @ToDate
group by inv.fk_ServiceID, s.ServiceName
Order By ServiceName asc
)
select * , (select sum(totalservicesrevenue) from MainTable) 'AllServicesRevenue'
, (select sum(TotalServicesCount) from MainTable) 'OverallServices'
from MainTable
add a comment |
declare @FromDate date= '22/Nov/2017',
@ToDate date= '24/Nov/2018'
Set @ToDate= ISNULL(@ToDate, getdate());
with MainTable
as
(
Select top(3) s.ServiceName, Count(inv.fk_ServiceID) as TotalServicesCount, Sum(inv.ServicePrice) TotalServicesRevenue
from Invoices inv
inner join Services s
on s.ServiceID= inv.fk_ServiceID
--where CAST(inv.EntryDateTime as DATE) between @FromDate and @ToDate
group by inv.fk_ServiceID, s.ServiceName
Order By ServiceName asc
)
select * , (select sum(totalservicesrevenue) from MainTable) 'AllServicesRevenue'
, (select sum(TotalServicesCount) from MainTable) 'OverallServices'
from MainTable
add a comment |
declare @FromDate date= '22/Nov/2017',
@ToDate date= '24/Nov/2018'
Set @ToDate= ISNULL(@ToDate, getdate());
with MainTable
as
(
Select top(3) s.ServiceName, Count(inv.fk_ServiceID) as TotalServicesCount, Sum(inv.ServicePrice) TotalServicesRevenue
from Invoices inv
inner join Services s
on s.ServiceID= inv.fk_ServiceID
--where CAST(inv.EntryDateTime as DATE) between @FromDate and @ToDate
group by inv.fk_ServiceID, s.ServiceName
Order By ServiceName asc
)
select * , (select sum(totalservicesrevenue) from MainTable) 'AllServicesRevenue'
, (select sum(TotalServicesCount) from MainTable) 'OverallServices'
from MainTable
declare @FromDate date= '22/Nov/2017',
@ToDate date= '24/Nov/2018'
Set @ToDate= ISNULL(@ToDate, getdate());
with MainTable
as
(
Select top(3) s.ServiceName, Count(inv.fk_ServiceID) as TotalServicesCount, Sum(inv.ServicePrice) TotalServicesRevenue
from Invoices inv
inner join Services s
on s.ServiceID= inv.fk_ServiceID
--where CAST(inv.EntryDateTime as DATE) between @FromDate and @ToDate
group by inv.fk_ServiceID, s.ServiceName
Order By ServiceName asc
)
select * , (select sum(totalservicesrevenue) from MainTable) 'AllServicesRevenue'
, (select sum(TotalServicesCount) from MainTable) 'OverallServices'
from MainTable
answered Nov 23 '18 at 9:51
azure boyazure boy
1467
1467
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%2f53429776%2fwhy-stored-procedure-returns-wrong-sum-for-each-group%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
2
share sample data with expected output
– Zaynul Abadin Tuhin
Nov 22 '18 at 11:19
SUM({expr}) OVER ()
is going to return the sum of every row in the table. Why would you want toSUM
it again?– Larnu
Nov 22 '18 at 11:22
Welcome to Stack Overflow. Your question doesn't include enough useful detail for us to help you. Check out How To Ask and the importance of a Minimal, Complete, Verifiable Example. After that, Start Here to edit your question as needed.
– Eric Brandt
Nov 22 '18 at 11:40
Do you want the sum of just the
top 3
or of all groups? Without sample data and results, your question is unclear.– Gordon Linoff
Nov 22 '18 at 11:59
1
Note to self: @ZoharPeled: Don't post an answer based on assumptions.
– Zohar Peled
Nov 22 '18 at 13:43