Search code examples
sybasesap-ase

Sybase: Get the first data in a Group


I'm having a problem getting the first row data in group in my data collection. Currently I'm using Sybase as my datasource.

I also used below query but not working.

SELECT Id, Product, RANK() OVER (PARTITION BY Id ORDER BY Id) FROM ProductTbl

SELECT Id, Product FROM ProductTbl as X
WHERE Id = (
SELECT min(Id)
  FROM ProductTbl WHERE Id = X.Id
)

Below example are the data that I'm working on.

Id     Product  
1111    Apple  
1111    Orange  
1111    Banana  
2222    Guava  
2222    JackFruit  
2222    Grape  
3333    ProductA  
3333    ProductB  

My expected output should be

Id      Product
1111    Apple
2222    Guava
3333    ProductA

Solution

  • Why not:

    SELECT Id, MIN(Product) FROM ProductTbl GROUP BY Id
    

    Have I misunderstood?