how to make group by column value should appear at only first row
In oracle, query is resturning this dataset
DEPT | EMP_NAME | SALARY
-----+----------+-------
10 | MARY | 30000
10 | JOHN | 20000
10 | SCOTT | 20000
20 | BOB | 50000
20 | BETTY | 50000
my objective is to make it as below
DEPT | EMP_NAME | SALARY
-----+----------+-------
10 | MARY | 30000
| JOHN | 20000
| SCOTT | 20000
20 | BOB | 50000
| BETTY | 50000
dep name should appear at once until new dep start.
sql oracle
add a comment |
In oracle, query is resturning this dataset
DEPT | EMP_NAME | SALARY
-----+----------+-------
10 | MARY | 30000
10 | JOHN | 20000
10 | SCOTT | 20000
20 | BOB | 50000
20 | BETTY | 50000
my objective is to make it as below
DEPT | EMP_NAME | SALARY
-----+----------+-------
10 | MARY | 30000
| JOHN | 20000
| SCOTT | 20000
20 | BOB | 50000
| BETTY | 50000
dep name should appear at once until new dep start.
sql oracle
1
doing that requires some sort of ordering. What makes "shoaib" the first row? It might not always be the first row returned, unless you've got one or more columns to order by.
– Boneist
Nov 21 '18 at 15:44
This is something you shouldn't do in SQL, but in your GUI layer (app or Website) where you are displaying the data in some grid or table.
– Thorsten Kettner
Nov 21 '18 at 15:53
i have updated my question. see now
– Shoaib Ahmed
Nov 21 '18 at 15:53
I don't see any update.
– Thorsten Kettner
Nov 21 '18 at 16:04
add a comment |
In oracle, query is resturning this dataset
DEPT | EMP_NAME | SALARY
-----+----------+-------
10 | MARY | 30000
10 | JOHN | 20000
10 | SCOTT | 20000
20 | BOB | 50000
20 | BETTY | 50000
my objective is to make it as below
DEPT | EMP_NAME | SALARY
-----+----------+-------
10 | MARY | 30000
| JOHN | 20000
| SCOTT | 20000
20 | BOB | 50000
| BETTY | 50000
dep name should appear at once until new dep start.
sql oracle
In oracle, query is resturning this dataset
DEPT | EMP_NAME | SALARY
-----+----------+-------
10 | MARY | 30000
10 | JOHN | 20000
10 | SCOTT | 20000
20 | BOB | 50000
20 | BETTY | 50000
my objective is to make it as below
DEPT | EMP_NAME | SALARY
-----+----------+-------
10 | MARY | 30000
| JOHN | 20000
| SCOTT | 20000
20 | BOB | 50000
| BETTY | 50000
dep name should appear at once until new dep start.
sql oracle
sql oracle
edited Nov 21 '18 at 15:47
Thorsten Kettner
50.5k22542
50.5k22542
asked Nov 21 '18 at 15:40
Shoaib Ahmed
102
102
1
doing that requires some sort of ordering. What makes "shoaib" the first row? It might not always be the first row returned, unless you've got one or more columns to order by.
– Boneist
Nov 21 '18 at 15:44
This is something you shouldn't do in SQL, but in your GUI layer (app or Website) where you are displaying the data in some grid or table.
– Thorsten Kettner
Nov 21 '18 at 15:53
i have updated my question. see now
– Shoaib Ahmed
Nov 21 '18 at 15:53
I don't see any update.
– Thorsten Kettner
Nov 21 '18 at 16:04
add a comment |
1
doing that requires some sort of ordering. What makes "shoaib" the first row? It might not always be the first row returned, unless you've got one or more columns to order by.
– Boneist
Nov 21 '18 at 15:44
This is something you shouldn't do in SQL, but in your GUI layer (app or Website) where you are displaying the data in some grid or table.
– Thorsten Kettner
Nov 21 '18 at 15:53
i have updated my question. see now
– Shoaib Ahmed
Nov 21 '18 at 15:53
I don't see any update.
– Thorsten Kettner
Nov 21 '18 at 16:04
1
1
doing that requires some sort of ordering. What makes "shoaib" the first row? It might not always be the first row returned, unless you've got one or more columns to order by.
– Boneist
Nov 21 '18 at 15:44
doing that requires some sort of ordering. What makes "shoaib" the first row? It might not always be the first row returned, unless you've got one or more columns to order by.
– Boneist
Nov 21 '18 at 15:44
This is something you shouldn't do in SQL, but in your GUI layer (app or Website) where you are displaying the data in some grid or table.
– Thorsten Kettner
Nov 21 '18 at 15:53
This is something you shouldn't do in SQL, but in your GUI layer (app or Website) where you are displaying the data in some grid or table.
– Thorsten Kettner
Nov 21 '18 at 15:53
i have updated my question. see now
– Shoaib Ahmed
Nov 21 '18 at 15:53
i have updated my question. see now
– Shoaib Ahmed
Nov 21 '18 at 15:53
I don't see any update.
– Thorsten Kettner
Nov 21 '18 at 16:04
I don't see any update.
– Thorsten Kettner
Nov 21 '18 at 16:04
add a comment |
1 Answer
1
active
oldest
votes
Assuming your results are ordered by department, employee name, you could use the LAG function to find the first row for each department. You'd need to check each row to see if it has a different department than the previous row, something like this:
SELECT CASE /* Check if there's a different department number on this row compared to the previous row */
WHEN NVL(LAG(t.dept) OVER (ORDER BY t.dept, t.emp_name),-1) <> t.dept /* If previous row is null (ie, this is the first row), use -1 as previous dept number so comparison does not fail */
THEN t.dept
END AS display_dept,
t.emp_name,
t.salary
FROM myTable t
ORDER BY t.dept, t.emp_name
That said, as others have pointed out, this really isn't something SQL is meant to do; you'd be much better off having this logic be in whatever is displaying the query results than in the query itself.
thanks so much worked for me..!
– Shoaib Ahmed
Nov 21 '18 at 16:58
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%2f53415617%2fhow-to-make-group-by-column-value-should-appear-at-only-first-row%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
Assuming your results are ordered by department, employee name, you could use the LAG function to find the first row for each department. You'd need to check each row to see if it has a different department than the previous row, something like this:
SELECT CASE /* Check if there's a different department number on this row compared to the previous row */
WHEN NVL(LAG(t.dept) OVER (ORDER BY t.dept, t.emp_name),-1) <> t.dept /* If previous row is null (ie, this is the first row), use -1 as previous dept number so comparison does not fail */
THEN t.dept
END AS display_dept,
t.emp_name,
t.salary
FROM myTable t
ORDER BY t.dept, t.emp_name
That said, as others have pointed out, this really isn't something SQL is meant to do; you'd be much better off having this logic be in whatever is displaying the query results than in the query itself.
thanks so much worked for me..!
– Shoaib Ahmed
Nov 21 '18 at 16:58
add a comment |
Assuming your results are ordered by department, employee name, you could use the LAG function to find the first row for each department. You'd need to check each row to see if it has a different department than the previous row, something like this:
SELECT CASE /* Check if there's a different department number on this row compared to the previous row */
WHEN NVL(LAG(t.dept) OVER (ORDER BY t.dept, t.emp_name),-1) <> t.dept /* If previous row is null (ie, this is the first row), use -1 as previous dept number so comparison does not fail */
THEN t.dept
END AS display_dept,
t.emp_name,
t.salary
FROM myTable t
ORDER BY t.dept, t.emp_name
That said, as others have pointed out, this really isn't something SQL is meant to do; you'd be much better off having this logic be in whatever is displaying the query results than in the query itself.
thanks so much worked for me..!
– Shoaib Ahmed
Nov 21 '18 at 16:58
add a comment |
Assuming your results are ordered by department, employee name, you could use the LAG function to find the first row for each department. You'd need to check each row to see if it has a different department than the previous row, something like this:
SELECT CASE /* Check if there's a different department number on this row compared to the previous row */
WHEN NVL(LAG(t.dept) OVER (ORDER BY t.dept, t.emp_name),-1) <> t.dept /* If previous row is null (ie, this is the first row), use -1 as previous dept number so comparison does not fail */
THEN t.dept
END AS display_dept,
t.emp_name,
t.salary
FROM myTable t
ORDER BY t.dept, t.emp_name
That said, as others have pointed out, this really isn't something SQL is meant to do; you'd be much better off having this logic be in whatever is displaying the query results than in the query itself.
Assuming your results are ordered by department, employee name, you could use the LAG function to find the first row for each department. You'd need to check each row to see if it has a different department than the previous row, something like this:
SELECT CASE /* Check if there's a different department number on this row compared to the previous row */
WHEN NVL(LAG(t.dept) OVER (ORDER BY t.dept, t.emp_name),-1) <> t.dept /* If previous row is null (ie, this is the first row), use -1 as previous dept number so comparison does not fail */
THEN t.dept
END AS display_dept,
t.emp_name,
t.salary
FROM myTable t
ORDER BY t.dept, t.emp_name
That said, as others have pointed out, this really isn't something SQL is meant to do; you'd be much better off having this logic be in whatever is displaying the query results than in the query itself.
answered Nov 21 '18 at 16:05
Josh Eller
73015
73015
thanks so much worked for me..!
– Shoaib Ahmed
Nov 21 '18 at 16:58
add a comment |
thanks so much worked for me..!
– Shoaib Ahmed
Nov 21 '18 at 16:58
thanks so much worked for me..!
– Shoaib Ahmed
Nov 21 '18 at 16:58
thanks so much worked for me..!
– Shoaib Ahmed
Nov 21 '18 at 16:58
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%2f53415617%2fhow-to-make-group-by-column-value-should-appear-at-only-first-row%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
1
doing that requires some sort of ordering. What makes "shoaib" the first row? It might not always be the first row returned, unless you've got one or more columns to order by.
– Boneist
Nov 21 '18 at 15:44
This is something you shouldn't do in SQL, but in your GUI layer (app or Website) where you are displaying the data in some grid or table.
– Thorsten Kettner
Nov 21 '18 at 15:53
i have updated my question. see now
– Shoaib Ahmed
Nov 21 '18 at 15:53
I don't see any update.
– Thorsten Kettner
Nov 21 '18 at 16:04