Group by a field in my sql and concatinate another field values as one string
My table is something like that.
Location | Building | Department |
L1 | B1 | D1
L2 | B2 | D2
L3 | B1 | D3
What I want to do is a query that counts locations and groups by buildings, but then display departments in that building as a string
Building | Count L | Departments |
B1 | 2 | D1,D3
B2 | 1 | D2
mysql
add a comment |
My table is something like that.
Location | Building | Department |
L1 | B1 | D1
L2 | B2 | D2
L3 | B1 | D3
What I want to do is a query that counts locations and groups by buildings, but then display departments in that building as a string
Building | Count L | Departments |
B1 | 2 | D1,D3
B2 | 1 | D2
mysql
add a comment |
My table is something like that.
Location | Building | Department |
L1 | B1 | D1
L2 | B2 | D2
L3 | B1 | D3
What I want to do is a query that counts locations and groups by buildings, but then display departments in that building as a string
Building | Count L | Departments |
B1 | 2 | D1,D3
B2 | 1 | D2
mysql
My table is something like that.
Location | Building | Department |
L1 | B1 | D1
L2 | B2 | D2
L3 | B1 | D3
What I want to do is a query that counts locations and groups by buildings, but then display departments in that building as a string
Building | Count L | Departments |
B1 | 2 | D1,D3
B2 | 1 | D2
mysql
mysql
edited Nov 13 '18 at 0:32
dwir182
1,418618
1,418618
asked Nov 13 '18 at 0:28
user6437700user6437700
6919
6919
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use Group_Concat
function
Definition:
This function returns a string result with the concatenated non-NULL
values from a group. It returns NULL if there are no non-NULL values
The query would be like:
Select
Building,
count(Location) as `Count L`,
group_concat(Department) as Departments
From
your_table
Group By
Building
add a comment |
select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,''))
from yourtable
group by building
This will help with nulls also.
2
Actually use backtick `` inmysql
to escape that name column.. Not single quote.. :)
– dwir182
Nov 13 '18 at 0:43
I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
– user6437700
Nov 14 '18 at 22:01
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%2f53272067%2fgroup-by-a-field-in-my-sql-and-concatinate-another-field-values-as-one-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Group_Concat
function
Definition:
This function returns a string result with the concatenated non-NULL
values from a group. It returns NULL if there are no non-NULL values
The query would be like:
Select
Building,
count(Location) as `Count L`,
group_concat(Department) as Departments
From
your_table
Group By
Building
add a comment |
You can use Group_Concat
function
Definition:
This function returns a string result with the concatenated non-NULL
values from a group. It returns NULL if there are no non-NULL values
The query would be like:
Select
Building,
count(Location) as `Count L`,
group_concat(Department) as Departments
From
your_table
Group By
Building
add a comment |
You can use Group_Concat
function
Definition:
This function returns a string result with the concatenated non-NULL
values from a group. It returns NULL if there are no non-NULL values
The query would be like:
Select
Building,
count(Location) as `Count L`,
group_concat(Department) as Departments
From
your_table
Group By
Building
You can use Group_Concat
function
Definition:
This function returns a string result with the concatenated non-NULL
values from a group. It returns NULL if there are no non-NULL values
The query would be like:
Select
Building,
count(Location) as `Count L`,
group_concat(Department) as Departments
From
your_table
Group By
Building
edited Nov 13 '18 at 5:16
Madhur Bhaiya
19.5k62236
19.5k62236
answered Nov 13 '18 at 0:34
dwir182dwir182
1,418618
1,418618
add a comment |
add a comment |
select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,''))
from yourtable
group by building
This will help with nulls also.
2
Actually use backtick `` inmysql
to escape that name column.. Not single quote.. :)
– dwir182
Nov 13 '18 at 0:43
I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
– user6437700
Nov 14 '18 at 22:01
add a comment |
select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,''))
from yourtable
group by building
This will help with nulls also.
2
Actually use backtick `` inmysql
to escape that name column.. Not single quote.. :)
– dwir182
Nov 13 '18 at 0:43
I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
– user6437700
Nov 14 '18 at 22:01
add a comment |
select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,''))
from yourtable
group by building
This will help with nulls also.
select building, Count(location) as 'Count L', GROUP_CONCAT(COALESCE(Departments,''))
from yourtable
group by building
This will help with nulls also.
answered Nov 13 '18 at 0:41
GauravsaGauravsa
2,3251816
2,3251816
2
Actually use backtick `` inmysql
to escape that name column.. Not single quote.. :)
– dwir182
Nov 13 '18 at 0:43
I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
– user6437700
Nov 14 '18 at 22:01
add a comment |
2
Actually use backtick `` inmysql
to escape that name column.. Not single quote.. :)
– dwir182
Nov 13 '18 at 0:43
I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
– user6437700
Nov 14 '18 at 22:01
2
2
Actually use backtick `` in
mysql
to escape that name column.. Not single quote.. :)– dwir182
Nov 13 '18 at 0:43
Actually use backtick `` in
mysql
to escape that name column.. Not single quote.. :)– dwir182
Nov 13 '18 at 0:43
I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
– user6437700
Nov 14 '18 at 22:01
I thought the GROUP_CONCAT does not return the null values on its own. At least this is what I get from the documentation. "This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values " dev.mysql.com/doc/refman/8.0/en/…
– user6437700
Nov 14 '18 at 22:01
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.
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.
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%2f53272067%2fgroup-by-a-field-in-my-sql-and-concatinate-another-field-values-as-one-string%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