Using different group by statements in DB2











up vote
0
down vote

favorite












I have a successful query which is returning some data for a user and category (the query is grouped by category and user)
which is basically showing projections for each category per user



select 
user,
category,
sum(CatProjection) as CatProjection,
sum(CatProjection)/52 as WeekCatProjection
from projections
where user = 123
group by user, category


And this returns what I want/expect:



USER  |   CATEGORY      |   CatProjection |  WeekCatProjection   
----------------------------------------------------------------
123 | CategoryA | 755000 | 14519
123 | CategoryB | 390000 | 7500
123 | CategoryC | 281250 | 5408


The issue is, those results are grouped by user and category, and now I want to add two columns which are dependent on the sum of CatProjection for that user, but not grouped by category.



So for the new value TotalCatProjection, I want to add all CatProjection values for that User. I then want to user CatPercentage to divide the WeekCatProjection by TotalCatProjection.



So now I would expect these results:



USER  |   CATEGORY      |   CatProjection |  WeekCatProjection   |  TotalCatProjection |  CatPercentage 
--------------------------------------------------------------------------------------------------------
123 | CategoryA | 755000 | 14519 | 27427 | 52.94
123 | CategoryB | 390000 | 7500 | 27427 | 27.35
123 | CategoryC | 281250 | 5408 | 27427 | 19.72


How can I get these 2 new columns which are based only on the USER when my main query is grouped by user AND category?










share|improve this question
























  • Do you always get it for just one user? I mean do you always have the where user = XXX condition there?
    – Radim Bača
    Nov 9 at 20:07










  • It's in a php script where it says where user = {$user} so no there's not always one user but I'm testing these queries off of one user. It will have the where clause though
    – Tom N.
    Nov 9 at 20:09










  • I suppose it could just be grouped by user but I'm testing on one right now
    – Tom N.
    Nov 9 at 20:09










  • which version of DB2 do you have?
    – Radim Bača
    Nov 9 at 20:21












  • I'm currently running 7.2
    – Tom N.
    Nov 9 at 20:23















up vote
0
down vote

favorite












I have a successful query which is returning some data for a user and category (the query is grouped by category and user)
which is basically showing projections for each category per user



select 
user,
category,
sum(CatProjection) as CatProjection,
sum(CatProjection)/52 as WeekCatProjection
from projections
where user = 123
group by user, category


And this returns what I want/expect:



USER  |   CATEGORY      |   CatProjection |  WeekCatProjection   
----------------------------------------------------------------
123 | CategoryA | 755000 | 14519
123 | CategoryB | 390000 | 7500
123 | CategoryC | 281250 | 5408


The issue is, those results are grouped by user and category, and now I want to add two columns which are dependent on the sum of CatProjection for that user, but not grouped by category.



So for the new value TotalCatProjection, I want to add all CatProjection values for that User. I then want to user CatPercentage to divide the WeekCatProjection by TotalCatProjection.



So now I would expect these results:



USER  |   CATEGORY      |   CatProjection |  WeekCatProjection   |  TotalCatProjection |  CatPercentage 
--------------------------------------------------------------------------------------------------------
123 | CategoryA | 755000 | 14519 | 27427 | 52.94
123 | CategoryB | 390000 | 7500 | 27427 | 27.35
123 | CategoryC | 281250 | 5408 | 27427 | 19.72


How can I get these 2 new columns which are based only on the USER when my main query is grouped by user AND category?










share|improve this question
























  • Do you always get it for just one user? I mean do you always have the where user = XXX condition there?
    – Radim Bača
    Nov 9 at 20:07










  • It's in a php script where it says where user = {$user} so no there's not always one user but I'm testing these queries off of one user. It will have the where clause though
    – Tom N.
    Nov 9 at 20:09










  • I suppose it could just be grouped by user but I'm testing on one right now
    – Tom N.
    Nov 9 at 20:09










  • which version of DB2 do you have?
    – Radim Bača
    Nov 9 at 20:21












  • I'm currently running 7.2
    – Tom N.
    Nov 9 at 20:23













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a successful query which is returning some data for a user and category (the query is grouped by category and user)
which is basically showing projections for each category per user



select 
user,
category,
sum(CatProjection) as CatProjection,
sum(CatProjection)/52 as WeekCatProjection
from projections
where user = 123
group by user, category


And this returns what I want/expect:



USER  |   CATEGORY      |   CatProjection |  WeekCatProjection   
----------------------------------------------------------------
123 | CategoryA | 755000 | 14519
123 | CategoryB | 390000 | 7500
123 | CategoryC | 281250 | 5408


The issue is, those results are grouped by user and category, and now I want to add two columns which are dependent on the sum of CatProjection for that user, but not grouped by category.



So for the new value TotalCatProjection, I want to add all CatProjection values for that User. I then want to user CatPercentage to divide the WeekCatProjection by TotalCatProjection.



So now I would expect these results:



USER  |   CATEGORY      |   CatProjection |  WeekCatProjection   |  TotalCatProjection |  CatPercentage 
--------------------------------------------------------------------------------------------------------
123 | CategoryA | 755000 | 14519 | 27427 | 52.94
123 | CategoryB | 390000 | 7500 | 27427 | 27.35
123 | CategoryC | 281250 | 5408 | 27427 | 19.72


How can I get these 2 new columns which are based only on the USER when my main query is grouped by user AND category?










share|improve this question















I have a successful query which is returning some data for a user and category (the query is grouped by category and user)
which is basically showing projections for each category per user



select 
user,
category,
sum(CatProjection) as CatProjection,
sum(CatProjection)/52 as WeekCatProjection
from projections
where user = 123
group by user, category


And this returns what I want/expect:



USER  |   CATEGORY      |   CatProjection |  WeekCatProjection   
----------------------------------------------------------------
123 | CategoryA | 755000 | 14519
123 | CategoryB | 390000 | 7500
123 | CategoryC | 281250 | 5408


The issue is, those results are grouped by user and category, and now I want to add two columns which are dependent on the sum of CatProjection for that user, but not grouped by category.



So for the new value TotalCatProjection, I want to add all CatProjection values for that User. I then want to user CatPercentage to divide the WeekCatProjection by TotalCatProjection.



So now I would expect these results:



USER  |   CATEGORY      |   CatProjection |  WeekCatProjection   |  TotalCatProjection |  CatPercentage 
--------------------------------------------------------------------------------------------------------
123 | CategoryA | 755000 | 14519 | 27427 | 52.94
123 | CategoryB | 390000 | 7500 | 27427 | 27.35
123 | CategoryC | 281250 | 5408 | 27427 | 19.72


How can I get these 2 new columns which are based only on the USER when my main query is grouped by user AND category?







sql db2 db2-400






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 20:37









Lennart

4,98111123




4,98111123










asked Nov 9 at 19:33









Tom N.

1,177414




1,177414












  • Do you always get it for just one user? I mean do you always have the where user = XXX condition there?
    – Radim Bača
    Nov 9 at 20:07










  • It's in a php script where it says where user = {$user} so no there's not always one user but I'm testing these queries off of one user. It will have the where clause though
    – Tom N.
    Nov 9 at 20:09










  • I suppose it could just be grouped by user but I'm testing on one right now
    – Tom N.
    Nov 9 at 20:09










  • which version of DB2 do you have?
    – Radim Bača
    Nov 9 at 20:21












  • I'm currently running 7.2
    – Tom N.
    Nov 9 at 20:23


















  • Do you always get it for just one user? I mean do you always have the where user = XXX condition there?
    – Radim Bača
    Nov 9 at 20:07










  • It's in a php script where it says where user = {$user} so no there's not always one user but I'm testing these queries off of one user. It will have the where clause though
    – Tom N.
    Nov 9 at 20:09










  • I suppose it could just be grouped by user but I'm testing on one right now
    – Tom N.
    Nov 9 at 20:09










  • which version of DB2 do you have?
    – Radim Bača
    Nov 9 at 20:21












  • I'm currently running 7.2
    – Tom N.
    Nov 9 at 20:23
















Do you always get it for just one user? I mean do you always have the where user = XXX condition there?
– Radim Bača
Nov 9 at 20:07




Do you always get it for just one user? I mean do you always have the where user = XXX condition there?
– Radim Bača
Nov 9 at 20:07












It's in a php script where it says where user = {$user} so no there's not always one user but I'm testing these queries off of one user. It will have the where clause though
– Tom N.
Nov 9 at 20:09




It's in a php script where it says where user = {$user} so no there's not always one user but I'm testing these queries off of one user. It will have the where clause though
– Tom N.
Nov 9 at 20:09












I suppose it could just be grouped by user but I'm testing on one right now
– Tom N.
Nov 9 at 20:09




I suppose it could just be grouped by user but I'm testing on one right now
– Tom N.
Nov 9 at 20:09












which version of DB2 do you have?
– Radim Bača
Nov 9 at 20:21






which version of DB2 do you have?
– Radim Bača
Nov 9 at 20:21














I'm currently running 7.2
– Tom N.
Nov 9 at 20:23




I'm currently running 7.2
– Tom N.
Nov 9 at 20:23












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Since the window functions are introduced in DB2 7.3 you should use a subquery solution:



select user, 
category,
CatProjection,
WeekCatProjection,
TotalCatProjection,
100 * WeekCatProjection / TotalCatProjection
from
(
select
user,
category,
sum(CatProjection) as CatProjection,
sum(CatProjection)/52 as WeekCatProjection,
(
select sum(CatProjection)
from projections p2
where p1.user = p2.user
) as TotalCatProjection
from projections p1
group by user, category
) t


I avoid the where user = XYZ condition because then the GROUP BY user does not make a sense, however, you can insert it there if you want to.






share|improve this answer























  • Hmm, it says Token ( was not valid.
    – Tom N.
    Nov 9 at 20:20










  • @TomN. ok, and what about the subquery solution?
    – Radim Bača
    Nov 9 at 20:29










  • It says more than one row returned. However, I got it to work using: (SELECT SUM(catProjection) from projections where user = 309) as TotalCatProjection
    – Tom N.
    Nov 9 at 20:30










  • But now I'm wondering how I can divide CatProjection by TotalCatProjection
    – Tom N.
    Nov 9 at 20:30










  • Wait your update does work! Thank you, I missed the update you made somehow
    – Tom N.
    Nov 9 at 20:33











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232212%2fusing-different-group-by-statements-in-db2%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








up vote
1
down vote



accepted










Since the window functions are introduced in DB2 7.3 you should use a subquery solution:



select user, 
category,
CatProjection,
WeekCatProjection,
TotalCatProjection,
100 * WeekCatProjection / TotalCatProjection
from
(
select
user,
category,
sum(CatProjection) as CatProjection,
sum(CatProjection)/52 as WeekCatProjection,
(
select sum(CatProjection)
from projections p2
where p1.user = p2.user
) as TotalCatProjection
from projections p1
group by user, category
) t


I avoid the where user = XYZ condition because then the GROUP BY user does not make a sense, however, you can insert it there if you want to.






share|improve this answer























  • Hmm, it says Token ( was not valid.
    – Tom N.
    Nov 9 at 20:20










  • @TomN. ok, and what about the subquery solution?
    – Radim Bača
    Nov 9 at 20:29










  • It says more than one row returned. However, I got it to work using: (SELECT SUM(catProjection) from projections where user = 309) as TotalCatProjection
    – Tom N.
    Nov 9 at 20:30










  • But now I'm wondering how I can divide CatProjection by TotalCatProjection
    – Tom N.
    Nov 9 at 20:30










  • Wait your update does work! Thank you, I missed the update you made somehow
    – Tom N.
    Nov 9 at 20:33















up vote
1
down vote



accepted










Since the window functions are introduced in DB2 7.3 you should use a subquery solution:



select user, 
category,
CatProjection,
WeekCatProjection,
TotalCatProjection,
100 * WeekCatProjection / TotalCatProjection
from
(
select
user,
category,
sum(CatProjection) as CatProjection,
sum(CatProjection)/52 as WeekCatProjection,
(
select sum(CatProjection)
from projections p2
where p1.user = p2.user
) as TotalCatProjection
from projections p1
group by user, category
) t


I avoid the where user = XYZ condition because then the GROUP BY user does not make a sense, however, you can insert it there if you want to.






share|improve this answer























  • Hmm, it says Token ( was not valid.
    – Tom N.
    Nov 9 at 20:20










  • @TomN. ok, and what about the subquery solution?
    – Radim Bača
    Nov 9 at 20:29










  • It says more than one row returned. However, I got it to work using: (SELECT SUM(catProjection) from projections where user = 309) as TotalCatProjection
    – Tom N.
    Nov 9 at 20:30










  • But now I'm wondering how I can divide CatProjection by TotalCatProjection
    – Tom N.
    Nov 9 at 20:30










  • Wait your update does work! Thank you, I missed the update you made somehow
    – Tom N.
    Nov 9 at 20:33













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Since the window functions are introduced in DB2 7.3 you should use a subquery solution:



select user, 
category,
CatProjection,
WeekCatProjection,
TotalCatProjection,
100 * WeekCatProjection / TotalCatProjection
from
(
select
user,
category,
sum(CatProjection) as CatProjection,
sum(CatProjection)/52 as WeekCatProjection,
(
select sum(CatProjection)
from projections p2
where p1.user = p2.user
) as TotalCatProjection
from projections p1
group by user, category
) t


I avoid the where user = XYZ condition because then the GROUP BY user does not make a sense, however, you can insert it there if you want to.






share|improve this answer














Since the window functions are introduced in DB2 7.3 you should use a subquery solution:



select user, 
category,
CatProjection,
WeekCatProjection,
TotalCatProjection,
100 * WeekCatProjection / TotalCatProjection
from
(
select
user,
category,
sum(CatProjection) as CatProjection,
sum(CatProjection)/52 as WeekCatProjection,
(
select sum(CatProjection)
from projections p2
where p1.user = p2.user
) as TotalCatProjection
from projections p1
group by user, category
) t


I avoid the where user = XYZ condition because then the GROUP BY user does not make a sense, however, you can insert it there if you want to.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 20:37

























answered Nov 9 at 20:15









Radim Bača

8,21911025




8,21911025












  • Hmm, it says Token ( was not valid.
    – Tom N.
    Nov 9 at 20:20










  • @TomN. ok, and what about the subquery solution?
    – Radim Bača
    Nov 9 at 20:29










  • It says more than one row returned. However, I got it to work using: (SELECT SUM(catProjection) from projections where user = 309) as TotalCatProjection
    – Tom N.
    Nov 9 at 20:30










  • But now I'm wondering how I can divide CatProjection by TotalCatProjection
    – Tom N.
    Nov 9 at 20:30










  • Wait your update does work! Thank you, I missed the update you made somehow
    – Tom N.
    Nov 9 at 20:33


















  • Hmm, it says Token ( was not valid.
    – Tom N.
    Nov 9 at 20:20










  • @TomN. ok, and what about the subquery solution?
    – Radim Bača
    Nov 9 at 20:29










  • It says more than one row returned. However, I got it to work using: (SELECT SUM(catProjection) from projections where user = 309) as TotalCatProjection
    – Tom N.
    Nov 9 at 20:30










  • But now I'm wondering how I can divide CatProjection by TotalCatProjection
    – Tom N.
    Nov 9 at 20:30










  • Wait your update does work! Thank you, I missed the update you made somehow
    – Tom N.
    Nov 9 at 20:33
















Hmm, it says Token ( was not valid.
– Tom N.
Nov 9 at 20:20




Hmm, it says Token ( was not valid.
– Tom N.
Nov 9 at 20:20












@TomN. ok, and what about the subquery solution?
– Radim Bača
Nov 9 at 20:29




@TomN. ok, and what about the subquery solution?
– Radim Bača
Nov 9 at 20:29












It says more than one row returned. However, I got it to work using: (SELECT SUM(catProjection) from projections where user = 309) as TotalCatProjection
– Tom N.
Nov 9 at 20:30




It says more than one row returned. However, I got it to work using: (SELECT SUM(catProjection) from projections where user = 309) as TotalCatProjection
– Tom N.
Nov 9 at 20:30












But now I'm wondering how I can divide CatProjection by TotalCatProjection
– Tom N.
Nov 9 at 20:30




But now I'm wondering how I can divide CatProjection by TotalCatProjection
– Tom N.
Nov 9 at 20:30












Wait your update does work! Thank you, I missed the update you made somehow
– Tom N.
Nov 9 at 20:33




Wait your update does work! Thank you, I missed the update you made somehow
– Tom N.
Nov 9 at 20:33


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232212%2fusing-different-group-by-statements-in-db2%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings