how to make group by column value should appear at only first row












0














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.










share|improve this question




















  • 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
















0














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.










share|improve this question




















  • 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














0












0








0







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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer





















  • thanks so much worked for me..!
    – Shoaib Ahmed
    Nov 21 '18 at 16:58













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


}
});














draft saved

draft discarded


















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









0














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.






share|improve this answer





















  • thanks so much worked for me..!
    – Shoaib Ahmed
    Nov 21 '18 at 16:58


















0














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.






share|improve this answer





















  • thanks so much worked for me..!
    – Shoaib Ahmed
    Nov 21 '18 at 16:58
















0












0








0






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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




















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%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





















































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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'