473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

distinct with order by usage is not bringing unique values

I have written this query:

select distinct company_id,company_name from (select company_id, company_name from companies order by UPPER(company_name) asc);


It brings out an ordered company name list. But the values aren't unique.
Can you please help me understand what is wrong with the query?

I am using db2 V10.1 (LUW).
Jul 1 '15 #1

✓ answered by Rabbit

If you need id, then you need to tell it which id to return because it is no longer distinct only on name if you include the id.

1, bob
2, bob
3, bob

There's only 1 name, but 3 different ids. If you want it distinct on name, it has no idea which id to return. So you have to tell it. Either min, max, or some other criteria that you choose.

3 2081
Rabbit
12,516 Expert Mod 8TB
That should give you the distinct combinations of id and name. If you want just distinct name, then take id out of the query.
Jul 1 '15 #2
I want just distinct names, but need to display list of id,names in the results.
How do I do that?

Running this query will not fetch me distinct names as the id, name combination is taken together to check distinct probably.
Jul 1 '15 #3
Rabbit
12,516 Expert Mod 8TB
If you need id, then you need to tell it which id to return because it is no longer distinct only on name if you include the id.

1, bob
2, bob
3, bob

There's only 1 name, but 3 different ids. If you want it distinct on name, it has no idea which id to return. So you have to tell it. Either min, max, or some other criteria that you choose.
Jul 1 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jo Goos | last post by:
Hello, I would like to count the unique values of a specific element in an XPath statement. Let's say I have the next XML document ... <CLUB> <MEMBER> <NAME>Fred</NAME>...
0
by: web1110 | last post by:
Hi y'all, I was playing with the NorthWind database, displaying subtables in a DataGrid. It worked fine for 3 levels, but when I added a fourth, I got the following error: These columns...
8
by: nescio | last post by:
hello, i have an array and i don't know the content of it, but i want only unique values. in php there is a function to do this, but how must i do this in javascript? i have tried a lot and...
11
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation a table with columns and rows
6
by: shira | last post by:
Hi, Looking to see if someone might have an explanation for this behavior. Is it a bug? Corruption? I have been able to reproduce the problem with only 2 rows and 1 field. Here is the table:...
1
by: OllyJ | last post by:
Hi guys, probably an easy one for you. I know how to limit a row source sor a combo on a form (setting unique values). The problem i'm having is that the query is the record source for a...
10
by: jss787 | last post by:
Let me start by saying this is my first database. The problem I am having is I have 7 combo boxes on a form that are dependent on each other and only two of them are diplaying unique values. The rest...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.