SQL Basket Analysis grouping query









up vote
0
down vote

favorite












I am trying to build a query to create a basket analysis of products based on their type grouping. They have two levels of grouping beyond the product ids.



Dept level 
1
2
3
4


and buying group level



MA 
M
MC
WA
W
WC
KA
KC
K


the hierarchy is



  • 1 > W, WC

  • 2 > M, MC

  • 3 > K, KC

  • 4 > MA, KA, WA

right now the query I have



Select 
i.buying_group,
Sum(d.sales),
Sum(d.units),
count(distinct d.trans_nbr) transaction_count
From
sales_details d, item_data i, (select trans_nbr from sales_details where item_dept = 1 group by trans_nbr) main_group
Where
d.trans_nbr = main_group.trans_nbr
d.item_nbr = i.item_nbr
group by i.buying_group;


Right now I get the data that I need for most of the buying groups but because this is being run at the dept level it does not give me the correct basket information for W and WC. Is there a way to do this at the dept level that would show if the customer bought something from either of these groups and had the other in their basket without double counting it?



the results at the moment are something like this



buyyin_group Sum(sales) Sum(units) transaction_count
MA 100 5 4
M 75 3 3
MC 56 1 1
WA 48 3 2
W 250 6 6
WC 200 9 9
KA 164 7 5
KC 400 12 7
K 521 14 12 `









share|improve this question























  • Sample data and desired results would really help.
    – Gordon Linoff
    Nov 8 at 16:05










  • Please don't join tables using , notation. It was replaced by explicit joins in ANSI-92 (over 25 years ago). INNER JOIN, LEFT JOIN, etc, are your friends. Whomever taught you otherwise should be re-educated.
    – MatBailie
    Nov 8 at 16:09















up vote
0
down vote

favorite












I am trying to build a query to create a basket analysis of products based on their type grouping. They have two levels of grouping beyond the product ids.



Dept level 
1
2
3
4


and buying group level



MA 
M
MC
WA
W
WC
KA
KC
K


the hierarchy is



  • 1 > W, WC

  • 2 > M, MC

  • 3 > K, KC

  • 4 > MA, KA, WA

right now the query I have



Select 
i.buying_group,
Sum(d.sales),
Sum(d.units),
count(distinct d.trans_nbr) transaction_count
From
sales_details d, item_data i, (select trans_nbr from sales_details where item_dept = 1 group by trans_nbr) main_group
Where
d.trans_nbr = main_group.trans_nbr
d.item_nbr = i.item_nbr
group by i.buying_group;


Right now I get the data that I need for most of the buying groups but because this is being run at the dept level it does not give me the correct basket information for W and WC. Is there a way to do this at the dept level that would show if the customer bought something from either of these groups and had the other in their basket without double counting it?



the results at the moment are something like this



buyyin_group Sum(sales) Sum(units) transaction_count
MA 100 5 4
M 75 3 3
MC 56 1 1
WA 48 3 2
W 250 6 6
WC 200 9 9
KA 164 7 5
KC 400 12 7
K 521 14 12 `









share|improve this question























  • Sample data and desired results would really help.
    – Gordon Linoff
    Nov 8 at 16:05










  • Please don't join tables using , notation. It was replaced by explicit joins in ANSI-92 (over 25 years ago). INNER JOIN, LEFT JOIN, etc, are your friends. Whomever taught you otherwise should be re-educated.
    – MatBailie
    Nov 8 at 16:09













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to build a query to create a basket analysis of products based on their type grouping. They have two levels of grouping beyond the product ids.



Dept level 
1
2
3
4


and buying group level



MA 
M
MC
WA
W
WC
KA
KC
K


the hierarchy is



  • 1 > W, WC

  • 2 > M, MC

  • 3 > K, KC

  • 4 > MA, KA, WA

right now the query I have



Select 
i.buying_group,
Sum(d.sales),
Sum(d.units),
count(distinct d.trans_nbr) transaction_count
From
sales_details d, item_data i, (select trans_nbr from sales_details where item_dept = 1 group by trans_nbr) main_group
Where
d.trans_nbr = main_group.trans_nbr
d.item_nbr = i.item_nbr
group by i.buying_group;


Right now I get the data that I need for most of the buying groups but because this is being run at the dept level it does not give me the correct basket information for W and WC. Is there a way to do this at the dept level that would show if the customer bought something from either of these groups and had the other in their basket without double counting it?



the results at the moment are something like this



buyyin_group Sum(sales) Sum(units) transaction_count
MA 100 5 4
M 75 3 3
MC 56 1 1
WA 48 3 2
W 250 6 6
WC 200 9 9
KA 164 7 5
KC 400 12 7
K 521 14 12 `









share|improve this question















I am trying to build a query to create a basket analysis of products based on their type grouping. They have two levels of grouping beyond the product ids.



Dept level 
1
2
3
4


and buying group level



MA 
M
MC
WA
W
WC
KA
KC
K


the hierarchy is



  • 1 > W, WC

  • 2 > M, MC

  • 3 > K, KC

  • 4 > MA, KA, WA

right now the query I have



Select 
i.buying_group,
Sum(d.sales),
Sum(d.units),
count(distinct d.trans_nbr) transaction_count
From
sales_details d, item_data i, (select trans_nbr from sales_details where item_dept = 1 group by trans_nbr) main_group
Where
d.trans_nbr = main_group.trans_nbr
d.item_nbr = i.item_nbr
group by i.buying_group;


Right now I get the data that I need for most of the buying groups but because this is being run at the dept level it does not give me the correct basket information for W and WC. Is there a way to do this at the dept level that would show if the customer bought something from either of these groups and had the other in their basket without double counting it?



the results at the moment are something like this



buyyin_group Sum(sales) Sum(units) transaction_count
MA 100 5 4
M 75 3 3
MC 56 1 1
WA 48 3 2
W 250 6 6
WC 200 9 9
KA 164 7 5
KC 400 12 7
K 521 14 12 `






sql oracle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 16:17









Gordon Linoff

743k32285390




743k32285390










asked Nov 8 at 16:02









Jacob Green

34




34











  • Sample data and desired results would really help.
    – Gordon Linoff
    Nov 8 at 16:05










  • Please don't join tables using , notation. It was replaced by explicit joins in ANSI-92 (over 25 years ago). INNER JOIN, LEFT JOIN, etc, are your friends. Whomever taught you otherwise should be re-educated.
    – MatBailie
    Nov 8 at 16:09

















  • Sample data and desired results would really help.
    – Gordon Linoff
    Nov 8 at 16:05










  • Please don't join tables using , notation. It was replaced by explicit joins in ANSI-92 (over 25 years ago). INNER JOIN, LEFT JOIN, etc, are your friends. Whomever taught you otherwise should be re-educated.
    – MatBailie
    Nov 8 at 16:09
















Sample data and desired results would really help.
– Gordon Linoff
Nov 8 at 16:05




Sample data and desired results would really help.
– Gordon Linoff
Nov 8 at 16:05












Please don't join tables using , notation. It was replaced by explicit joins in ANSI-92 (over 25 years ago). INNER JOIN, LEFT JOIN, etc, are your friends. Whomever taught you otherwise should be re-educated.
– MatBailie
Nov 8 at 16:09





Please don't join tables using , notation. It was replaced by explicit joins in ANSI-92 (over 25 years ago). INNER JOIN, LEFT JOIN, etc, are your friends. Whomever taught you otherwise should be re-educated.
– MatBailie
Nov 8 at 16:09


















active

oldest

votes











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%2f53211590%2fsql-basket-analysis-grouping-query%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53211590%2fsql-basket-analysis-grouping-query%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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)