开发者

Oracle - Unique values combinations from multiple columns, but returning other columns

开发者 https://www.devze.com 2023-02-24 11:19 出处:网络
I\'m trying to figure out how to pull different data scenarios from my table for testing.Assume my table has these three fields FIELD1, FIELD2, FIELD3.If I want to find the various scenarios in my dat

I'm trying to figure out how to pull different data scenarios from my table for testing. Assume my table has these three fields FIELD1, FIELD2, FIELD3. If I want to find the various scenarios in my data I could simply do this

select distinct FIELD1, FIELD2, FIELD3 from <table>

And this works. The problem is I have a plethora of other fields I need data from as well, but it doesn't matter what that data is as long as it's associated with the resulting record. So for example if the above query returned this as one of the results

FIELD1  FIELD2  FIELD3
----------------------
Y       Blue    31

I would want to see the other content fields (4 through 10 lets say). Now wether those came from record number #30 or record #20000 (assuming those records contained those 3 returned values) it doesn't matter as long as all the fields were returned by said record.

Hope that makes sense and someone ca开发者_开发问答n help!


Something like

SELECT field1, 
       field2,
       field3,
       field4,
       ....
       field10
  FROM( SELECT field1,
               field2,
               ...,
               field10,
               rowid rid,
               min(rowid) OVER (partition by field1, field2, field3) min_rid
          FROM your_table_name )
 WHERE rid = min_rid

should work. If there is a primary key, you could use that rather than the ROWID, I'm just using that to have something that is guaranteed to be unique.

For your curveball

SELECT field1, 
       field2,
       field3,
       field4,
       ....
       field10
  FROM( SELECT field1,
               field2,
               (CASE WHEN field3 IS NULL
                     THEN 'NULL'
                     ELSE field3
                 END) field3,
               ...,
               field10,
               rowid rid,
               min(rowid) OVER (partition by field1, 
                                             field2, 
                                             (CASE WHEN field3 IS NULL
                                                   THEN 'NULL'
                                                   ELSE field3
                                               END) min_rid
          FROM your_table_name )
 WHERE rid = min_rid


Try this:

select a.* 
from tblA a, 
     (select min(rowid), col_1, col_2 from tblA
      group by col_1, col_2) b
where a.rowid=b.rid;      
0

精彩评论

暂无评论...
验证码 换一张
取 消