| | |
DISTINCT on One Column Only
Please support our Oracle advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 8
Reputation:
Solved Threads: 0
Hello,
I've been trying to find a way, using Oracle Express, to return more than one column but have only one column be distinct. The following will make all three columns distinct. I want only the Job_ID column to be distinct. Can this be done in Oracle?
Thanks Much,
Lewis
I've been trying to find a way, using Oracle Express, to return more than one column but have only one column be distinct. The following will make all three columns distinct. I want only the Job_ID column to be distinct. Can this be done in Oracle?
sql Syntax (Toggle Plain Text)
SELECT DISTINCT Job_ID, Employee_ID, Last_Name FROM Employees ORDER BY Last_Name
Thanks Much,
Lewis
Last edited by peter_budo; Sep 22nd, 2009 at 9:24 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Try something like this.
sql Syntax (Toggle Plain Text)
SELECT DISTINCT emp.Job_ID, a.Employee_ID, a.Last_Name FROM Employees emp JOIN (SELECT job_id,employee_id,Lastname FROM Employees)a on emp.Job_id = a.Job_id ORDER BY a.Last_Name
•
•
Join Date: Sep 2009
Posts: 8
Reputation:
Solved Threads: 0
Thank you CGYROB. But, your solution doesn't seem to limit the recordset. It returns the same thing as if I removed the DISTINCT and the subquery. And when I use your code and only take out the DISTINCT, I get something that looks like a UNION ALL. Please let me know if you have any other ideas, but it might be that this just can't be done.
Thanks,
Lewis
Thanks,
Lewis
•
•
•
•
Try something like this.
sql Syntax (Toggle Plain Text)
SELECT DISTINCT emp.Job_ID, a.Employee_ID, a.Last_Name FROM Employees emp JOIN (SELECT job_id,employee_id,Last_name FROM Employees)a on emp.Job_id = a.Job_id ORDER BY a.Last_Name
I'm not quite sure what you are trying to achieve.
If you just want the job_id then grab it seperately, if you want to see all the employees associated which each job_id you have to return all rows for each job_id, unless you have exact duplicate rows which would not make sense in your db design.
What you are asking to achieve doesn't seem to make any sense without context of your ultimate goal.
If you just want the job_id then grab it seperately, if you want to see all the employees associated which each job_id you have to return all rows for each job_id, unless you have exact duplicate rows which would not make sense in your db design.
What you are asking to achieve doesn't seem to make any sense without context of your ultimate goal.
•
•
Join Date: Sep 2009
Posts: 8
Reputation:
Solved Threads: 0
Actually, the example I provided was not a good one because the job_ID is the only one in the database that has duplicate values. But if you could pretend that the other columns had some duplicate values that might make the task make more sense. I'm not doing this for a current project, but rather for my own knowledge. But, I do remember needing to do this in the past. So, the idea is simply to make the job_id DISTINCT, but not the other columns. I doesn't matter how I do that. It could be by subquery or query of queries, whatever. But, I can't see a way. I saw some posts when I did an Internet search but none were for Oracle, and I'm not convinced they worked for MySQL or whatever database either.
Thanks much,
Lewis
Thanks much,
Lewis
•
•
•
•
I'm not quite sure what you are trying to achieve.
If you just want the job_id then grab it seperately, if you want to see all the employees associated which each job_id you have to return all rows for each job_id, unless you have exact duplicate rows which would not make sense in your db design.
What you are asking to achieve doesn't seem to make any sense without context of your ultimate goal.
I know it is possible as I have done similar queries in the past using subqueries. I provided something I put together quickly, not a very good example but it does accomplish what you were inquiring about.
sql Syntax (Toggle Plain Text)
SELECT DISTINCT a.JOB_ID, (SELECT EMPLOYEE_ID FROM EMPLOYEE b WHERE b.JOB_ID = a.JOB_ID AND rownum = 1) , (SELECT LAST_NAME FROM EMPLOYEE c WHERE c.JOB_ID = a.JOB_ID AND rownum = 1) FROM EMPLOYEEE
•
•
Join Date: Sep 2009
Posts: 8
Reputation:
Solved Threads: 0
Thank you. I think this is what I'm looking for. But, I don't understand what the "rownum = 1" does.
Without the rownum = 1, I get this message:
ORA-01427: single-row subquery returns more than one row
I thought "rownum =1" would just return one row. So, I'm confused about the effect of using rownum. Is there any actual restriction on the recordset from using it? Or, does it just sort of cheat the system and allow us to get what I need here?
Lewis
Without the rownum = 1, I get this message:
ORA-01427: single-row subquery returns more than one row
I thought "rownum =1" would just return one row. So, I'm confused about the effect of using rownum. Is there any actual restriction on the recordset from using it? Or, does it just sort of cheat the system and allow us to get what I need here?
Lewis
•
•
•
•
I know it is possible as I have done similar queries in the past using subqueries. I provided something I put together quickly, not a very good example but it does accomplish what you were inquiring about.
sql Syntax (Toggle Plain Text)
SELECT DISTINCT a.JOB_ID, (SELECT EMPLOYEE_ID FROM EMPLOYEE b WHERE b.JOB_ID = a.JOB_ID AND rownum = 1) , (SELECT LAST_NAME FROM EMPLOYEE c WHERE c.JOB_ID = a.JOB_ID AND rownum = 1) FROM EMPLOYEEE
•
•
Join Date: Sep 2009
Posts: 2
Reputation:
Solved Threads: 2
if Employee_ID and Last_Name can be any, within a specified Job_ID,
You can also use the group functions, eg
You can also use the group functions, eg
Oracle Syntax (Toggle Plain Text)
SELECT Job_ID, MAX(Employee_ID), MAX(Last_Name) FROM Employees GROUP BY Job_ID
What rownum = 1 is doing is returning only the top row of employee information for each job_id. without the rownum =1 the query will return all matches which is why you get the error message.
BabyDBA's solution should return the same results and is actually a much better way to go as there is only 1 scan of the table and not 3 plus it is alot cleaner and easier to read.
So to answer your question it is possible to get the distinct value of one field while displaying multiple fields.
BabyDBA's solution should return the same results and is actually a much better way to go as there is only 1 scan of the table and not 3 plus it is alot cleaner and easier to read.
So to answer your question it is possible to get the distinct value of one field while displaying multiple fields.
•
•
Join Date: Sep 2009
Posts: 8
Reputation:
Solved Threads: 0
Thank you. This is valuable. So, if rownum= 1 is returning only the top row for each job_Id, what would happen if I used subqueries for all the columns in the SELECT statement, with no distinct? Actually, I did that. I can see that it only returns the first JOb_ID but it returns 12 of them, and there aren't 12 in the database with that Id. In fact, there is only one. What is happening?
Also, I'm new to this community. I noticed something about clicking a link to promote you or give you points or something. I can't remember. I certainly intend to do that, but what does it mean?
Many thanks,
Lewis
Also, I'm new to this community. I noticed something about clicking a link to promote you or give you points or something. I can't remember. I certainly intend to do that, but what does it mean?
Many thanks,
Lewis
•
•
•
•
What rownum = 1 is doing is returning only the top row of employee information for each job_id. without the rownum =1 the query will return all matches which is why you get the error message.
BabyDBA's solution should return the same results and is actually a much better way to go as there is only 1 scan of the table and not 3 plus it is alot cleaner and easier to read.
So to answer your question it is possible to get the distinct value of one field while displaying multiple fields.
![]() |
Similar Threads
- Update Stmt - Update same record multiple times (MySQL)
- Need a Distinct Column in my inner join statement (ASP)
- Count/Sum distinct problem (MS SQL)
- distinct Multi Union help (MS SQL)
- Enum vlues into Combo (Visual Basic 4 / 5 / 6)
- multi column data display (PHP)
- Addition 2 column in sql (MS SQL)
Other Threads in the Oracle Forum
- Previous Thread: removing first blank line in csv file
- Next Thread: Sql query to get max 5 values
| Thread Tools | Search this Thread |
2009predictions acquisition amazon.com bartz bernanke cia citrix cloudcomputing crm database dell economy editor enterprise enterprise2.0 enterprisesoftware federalreserve forbes hp ibm intellipedia internet larryellison layoffs linux loughridge mediawiki michaeljackson microsoft neverland nortel notebooks oil operatingsystem oracle palm rimm saas salesforce sap seagate socialcomputing sql sun sybase technologystocks virtualiron virtualization vmware wiki wikipedia xen yahoo zoho





