tags:

views:

70

answers:

6

Hi,

I have table with values as

    ColumnA   ColumnB

    ASD       A
    CSD       B
    DSD       C
    ESD       D
    FSD       D
    GSD       D

Column A is primary key, I need result as

    ColumnA   ColumnB

    ASD       A
    CSD       B
    DSD       C
    ESD       D

I want all unique values from column B & their associated first values columnA.

I tried few queries & looked on Google but couldn't find ant solution .

Thanks, Rohit.

+3  A: 

First is First in key order (i.e. alphabetically).

SELECT MIN(ColumnA) AS ColumnA, ColumnB
FROM tbl
GROUP BY ColumnB

You've tagged both MySQL and Oracle for some reason. The above will work in both.

Oracle also has analytical functions that can help with this general type of query.

Martin Smith
+2  A: 
select min(columna) columna, columnb
from mytable
group by columnb;
Tony Andrews
+2  A: 

With analytics (Oracle):

SQL> WITH tab AS (
  2     SELECT 'ASD' columnA, 'A' columnB FROM DUAL
  3     UNION ALL SELECT 'CSD', 'B' FROM DUAL
  4     UNION ALL SELECT 'DSD', 'C' FROM DUAL
  5     UNION ALL SELECT 'ESD', 'D' FROM DUAL
  6     UNION ALL SELECT 'FSD', 'D' FROM DUAL
  7     UNION ALL SELECT 'GSD', 'D' FROM DUAL
  8  )
  9  SELECT columnA, columnB
 10    FROM (SELECT columnA, columnB,
 11                 rank() over(PARTITION BY columnB ORDER BY columnA) rnk
 12            FROM tab)
 13   WHERE rnk = 1;

COLUMNA COLUMNB
------- -------
ASD     A
CSD     B
DSD     C
ESD     D
Vincent Malgrat
I like it: easier to change if the inferred definition of 'first' turns out to be incorrect. Out of interest, Oracle doesn't require a name for a derived table, then? Why did you choose to use a derived table over a CTE?
onedaywhen
@onedaywhen : I'm not sure I fully understand your questions since I'm not familiar with the SQL Server vocabulary. Oracle doesn't require an alias when using a subquery (is it the same as a derived table?). The `WITH` clause is called a subquery factoring in Oracle, I find it's useful to give a small self-contained example: you can run the query in your environment without having to create any additional object (You can't create #temporary tables that will self-destruct when the session ends in Oracle).
Vincent Malgrat
I'm actually using Standard SQL terms :) Your subquery that defines the table naned `tab` (`SELECT...UNION ALL SELECT...`) is known as a common table expression (CTE) and your subquery that further queries the `tab` CTE is known as a derived table. But I'll try re-phrasing my question in Oracle terms...
onedaywhen
Your subquery `(SELECT columnA, columnB, ... FROM tab)` can be written using a second WITH clause subquery factoring e.g. `WITH tab AS (<first subquery>), tab2 AS (<second subquery>) SELECT columnA, columnB FROM tab2 WHERE rnk = 1;` -- so I'm wondering why you used two subqueries in two different ways. Thanks in advance.
onedaywhen
A: 
select min(ColumnA) as ColumnA,ColumnB from table
group by ColumnB
Madhivanan
A: 
CREATE TABLE #t_Val
(
    ColumnA VARCHAR(3) PRIMARY KEY
,   ColumnB CHAR(1) NOT NULL
)

INSERT #t_Val
SELECT 'ASD' ,  'A'
UNION
SELECT 'CSD' ,  'B'
UNION
SELECT 'DSD' ,  'C'
UNION
SELECT 'ESD' ,  'D'
UNION
SELECT 'FSD' ,  'D'
UNION
SELECT 'GSD' ,  'D'


SELECT  MIN(ColumnA),ColumnB
FROM #t_Val
GROUP BY ColumnB
greektreat
A: 

Just another way of doing the same thing:

SELECT DISTINCT
    FIRST_VALUE(ColumnA)
    OVER (PARTITION BY ColumnB
          ORDER BY ColumnA) AS ColumnA,
    ColumnB
FROM tbl

This has the advantage of being usable with more than one column (i.e. where the GROUP BY method doesn't work).

Jeffrey Kemp