How to create a group based on multiple different columns in SQL?











up vote
-2
down vote

favorite












I have a dataset that has six different columns (UserID, School and four different Tests) as below:



UserID  School  T1    T2    T3    T4     Group
M1 A 1 3 G1
M2 C 3 G1
M3 A 5 G1
M4 B 9 7 G2
M5 D 2 G3
M6 D 4 G3
M7 A G1
M8 E 6 2 G3
M9 F 1 G1
M10 B 8 G2


I want to add a calculated Group column using the first six columns, the corresponding values for this new Group column have shown in the example.



I want to assign a Group Number to each UserID in Group Column. The users will be in same group if they have at least one similar test (test type is not important) or school. e.g. M5 and M6 are in same group because they have same school and M8 is also in G3 because it has common test(2) with M6.










share|improve this question
























  • Unsure of what you're asking. Are you saying that you want add a calculated Group column, and that based on the sample data you've provided for the first six columns, you've shown what the corresponding values for this new Group column should be? If so, I'm not seeing how you're arriving at the values for the Group column. Please edit your question to include more of an explanation, or respond to my comment letting me know where my assumptions have led me astray
    – Bob Kaufman
    Nov 21 at 21:29






  • 2




    mysql <> sql server. Pick the one you actually use as the each uses their own sql dialect.
    – SMor
    Nov 21 at 21:32










  • Removed conflicting product tags, pls add the one back that you actually use.
    – Shadow
    Nov 21 at 21:46










  • @BobKaufman Thanks for your comment. I added more explanation to my question.
    – Famrin
    Nov 21 at 21:50






  • 1




    Why is M4 in G2, if it shares test 3 with M2, which is in G1?
    – Shadow
    Nov 21 at 21:55















up vote
-2
down vote

favorite












I have a dataset that has six different columns (UserID, School and four different Tests) as below:



UserID  School  T1    T2    T3    T4     Group
M1 A 1 3 G1
M2 C 3 G1
M3 A 5 G1
M4 B 9 7 G2
M5 D 2 G3
M6 D 4 G3
M7 A G1
M8 E 6 2 G3
M9 F 1 G1
M10 B 8 G2


I want to add a calculated Group column using the first six columns, the corresponding values for this new Group column have shown in the example.



I want to assign a Group Number to each UserID in Group Column. The users will be in same group if they have at least one similar test (test type is not important) or school. e.g. M5 and M6 are in same group because they have same school and M8 is also in G3 because it has common test(2) with M6.










share|improve this question
























  • Unsure of what you're asking. Are you saying that you want add a calculated Group column, and that based on the sample data you've provided for the first six columns, you've shown what the corresponding values for this new Group column should be? If so, I'm not seeing how you're arriving at the values for the Group column. Please edit your question to include more of an explanation, or respond to my comment letting me know where my assumptions have led me astray
    – Bob Kaufman
    Nov 21 at 21:29






  • 2




    mysql <> sql server. Pick the one you actually use as the each uses their own sql dialect.
    – SMor
    Nov 21 at 21:32










  • Removed conflicting product tags, pls add the one back that you actually use.
    – Shadow
    Nov 21 at 21:46










  • @BobKaufman Thanks for your comment. I added more explanation to my question.
    – Famrin
    Nov 21 at 21:50






  • 1




    Why is M4 in G2, if it shares test 3 with M2, which is in G1?
    – Shadow
    Nov 21 at 21:55













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have a dataset that has six different columns (UserID, School and four different Tests) as below:



UserID  School  T1    T2    T3    T4     Group
M1 A 1 3 G1
M2 C 3 G1
M3 A 5 G1
M4 B 9 7 G2
M5 D 2 G3
M6 D 4 G3
M7 A G1
M8 E 6 2 G3
M9 F 1 G1
M10 B 8 G2


I want to add a calculated Group column using the first six columns, the corresponding values for this new Group column have shown in the example.



I want to assign a Group Number to each UserID in Group Column. The users will be in same group if they have at least one similar test (test type is not important) or school. e.g. M5 and M6 are in same group because they have same school and M8 is also in G3 because it has common test(2) with M6.










share|improve this question















I have a dataset that has six different columns (UserID, School and four different Tests) as below:



UserID  School  T1    T2    T3    T4     Group
M1 A 1 3 G1
M2 C 3 G1
M3 A 5 G1
M4 B 9 7 G2
M5 D 2 G3
M6 D 4 G3
M7 A G1
M8 E 6 2 G3
M9 F 1 G1
M10 B 8 G2


I want to add a calculated Group column using the first six columns, the corresponding values for this new Group column have shown in the example.



I want to assign a Group Number to each UserID in Group Column. The users will be in same group if they have at least one similar test (test type is not important) or school. e.g. M5 and M6 are in same group because they have same school and M8 is also in G3 because it has common test(2) with M6.







sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 23:06

























asked Nov 21 at 21:24









Famrin

93




93












  • Unsure of what you're asking. Are you saying that you want add a calculated Group column, and that based on the sample data you've provided for the first six columns, you've shown what the corresponding values for this new Group column should be? If so, I'm not seeing how you're arriving at the values for the Group column. Please edit your question to include more of an explanation, or respond to my comment letting me know where my assumptions have led me astray
    – Bob Kaufman
    Nov 21 at 21:29






  • 2




    mysql <> sql server. Pick the one you actually use as the each uses their own sql dialect.
    – SMor
    Nov 21 at 21:32










  • Removed conflicting product tags, pls add the one back that you actually use.
    – Shadow
    Nov 21 at 21:46










  • @BobKaufman Thanks for your comment. I added more explanation to my question.
    – Famrin
    Nov 21 at 21:50






  • 1




    Why is M4 in G2, if it shares test 3 with M2, which is in G1?
    – Shadow
    Nov 21 at 21:55


















  • Unsure of what you're asking. Are you saying that you want add a calculated Group column, and that based on the sample data you've provided for the first six columns, you've shown what the corresponding values for this new Group column should be? If so, I'm not seeing how you're arriving at the values for the Group column. Please edit your question to include more of an explanation, or respond to my comment letting me know where my assumptions have led me astray
    – Bob Kaufman
    Nov 21 at 21:29






  • 2




    mysql <> sql server. Pick the one you actually use as the each uses their own sql dialect.
    – SMor
    Nov 21 at 21:32










  • Removed conflicting product tags, pls add the one back that you actually use.
    – Shadow
    Nov 21 at 21:46










  • @BobKaufman Thanks for your comment. I added more explanation to my question.
    – Famrin
    Nov 21 at 21:50






  • 1




    Why is M4 in G2, if it shares test 3 with M2, which is in G1?
    – Shadow
    Nov 21 at 21:55
















Unsure of what you're asking. Are you saying that you want add a calculated Group column, and that based on the sample data you've provided for the first six columns, you've shown what the corresponding values for this new Group column should be? If so, I'm not seeing how you're arriving at the values for the Group column. Please edit your question to include more of an explanation, or respond to my comment letting me know where my assumptions have led me astray
– Bob Kaufman
Nov 21 at 21:29




Unsure of what you're asking. Are you saying that you want add a calculated Group column, and that based on the sample data you've provided for the first six columns, you've shown what the corresponding values for this new Group column should be? If so, I'm not seeing how you're arriving at the values for the Group column. Please edit your question to include more of an explanation, or respond to my comment letting me know where my assumptions have led me astray
– Bob Kaufman
Nov 21 at 21:29




2




2




mysql <> sql server. Pick the one you actually use as the each uses their own sql dialect.
– SMor
Nov 21 at 21:32




mysql <> sql server. Pick the one you actually use as the each uses their own sql dialect.
– SMor
Nov 21 at 21:32












Removed conflicting product tags, pls add the one back that you actually use.
– Shadow
Nov 21 at 21:46




Removed conflicting product tags, pls add the one back that you actually use.
– Shadow
Nov 21 at 21:46












@BobKaufman Thanks for your comment. I added more explanation to my question.
– Famrin
Nov 21 at 21:50




@BobKaufman Thanks for your comment. I added more explanation to my question.
– Famrin
Nov 21 at 21:50




1




1




Why is M4 in G2, if it shares test 3 with M2, which is in G1?
– Shadow
Nov 21 at 21:55




Why is M4 in G2, if it shares test 3 with M2, which is in G1?
– Shadow
Nov 21 at 21:55

















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%2f53420657%2fhow-to-create-a-group-based-on-multiple-different-columns-in-sql%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




















































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%2f53420657%2fhow-to-create-a-group-based-on-multiple-different-columns-in-sql%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)