Solved

Selecting distinct records

Posted on 2010-08-28
7
642 Views
Last Modified: 2012-05-10
I am writing a select query from a table TEST having three colums A,B,C in which i want to select distinct records on the basis of both the colums A and B.

If i give:

select distinct A,B,C from test;

it gives only the distinct records on the basis of column A. I tried this also:

select distinct A,B,C from test group by A,B;

but not working.  Kindly suggest.
0
Comment
Question by:mohitgyl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 33549286
Try using:

select A,B,MAX(C) as C
from test
group by A,B;

or

select A,B,MAX(C) as C
from test
group by A,B;
0
 

Author Comment

by:mohitgyl
ID: 33549381
I dont understand why did u suggest using max(C), i think that will select max value for column C but my problem is different.

I want to select distinct records on the basis of both column A and B but not column C.
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 33549393
Then try this:

SELECT A, B, C
FROM (
select A,B,C, ROW_NUMBER() OVER ( Partition By A, B order by C) as rnum
from test ) temp
WHERE rnum = 1
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33549479
Hi mohitgyl,

Can you explain a bit about what you see in the original query?

  SELECT distinct a, b, c FROM mytable;

That query will filter out all rows where a,b,c are ALL exactly like the values of another row.  (It will be selected only once.)  I does not filter only on column "a".


Kent
0
 
LVL 27

Expert Comment

by:tliotta
ID: 33580564
...it gives only the distinct records on the basis of column A.

I am not clear at all what the problem is. It sounds as if you are trying to get DISTINCT A, DISTINCT B and DISTINCT C, all somehow included in a single result set.

But that doesn't make sense to me.

Can you supply a few example rows from a table and show what result you expect to see from those rows?

Tom
0
 
LVL 27

Accepted Solution

by:
tliotta earned 500 total points
ID: 33599260
I mistakenly included "DISTINCT C" in my previous comment. But it's still not clear about the meaning two DISTINCT columns while a third column is not DISTINCT.

...distinct records on the basis of both column A and B but not column C.

You also said "why did u suggest using max(C)". If you don't use max(C) or some similar function, how else while the query know which C value to retrieve?

A   B   C
--  --  ---
a1 b1 c1
a1 b1 c2
a2 b2 c3
a2 b2 c4
a3 b4 c5
a3 b5 c6
a4 b4 c7

DISTINCT values for A are (a1, a2, a3, a4). DISTINCT values for B are (b1, b2, b4, b5).

So, DISTINCT A CONCAT B values are (a1b1, a2b2, a3b4, a3b5, a4b4).

For the (a2b2) value, which value for C should be in the result set -- c3 or c4? There can be only one because the (a2b2) value must be DISTINCT. You need some way to choose which value of C should be picked. The max(C) function will make the choice between (c3) and (c4)

Show a sample of data and show your expected result, please.

Tom
0
 

Author Closing Comment

by:mohitgyl
ID: 33767381
Satisfied
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question