Diberdayakan oleh Blogger.

Jumat, 26 September 2014

Select distinct value from one column in SQL

by Joielechong sipayung  |  in SQLite at  Jumat, September 26, 2014

As I am developing an Android application called BukuEnde (Still in alpha testing, contact me if you want to test it then I will send the link via your google account), I'm facing a problem of showing every unique category of songs from SQLite table. Here is the format of my table (SongCategory table) :

Table 1. SongCategory
 Category                                   SubCategory  
 --------------------------------------------------------------------------------------------------------------------  
 Ende di Hasasaor ni Tondi Parbadia       
 Ende di Trinitatis       
 Ende taringot tu harajaon ni Debata       Manopoti haporseaon  
 Ende taringot tu harajaon ni Debata       Ulaon na badia  
 Ende taringot tu harajaon ni Debata       Pasahathon tohonan Pandita  
 Ende taringot tu harajaon ni Debata       Laho marabagas  
 Ende taringot tu hasesaan ni dosa       
 Ende taringot tu haporseaon       
 Ende taringot tu parungkilon       
 Ende pangapulon       
As we see from the Table 1, the Category column is filled with non unique data because there are same data like 'Ende taringot tu harajaon ni Debata'. The problem here, I want to show only unique category data ignoring subCategory column data. I want to get the data like Table 2 below:
Table 2. Result I want
    Category                                   SubCategory   
  -----------------------------------------------------------------------------------------------------------------------  
  Ende di Hasasaor ni Tondi Parbadia      
  Ende di Trinitatis      
  Ende taringot tu harajaon ni Debata        Manopoti haporseaon  
  Ende taringot tu hasesaan ni dosa      
  Ende taringot tu haporseaon      
  Ende taringot tu parungkilon      
  Ende pangapulon    
We can achive the result by simple query below:
 select * from SongCategory group by Category;  
This query working perfectly but for the moment when I using the query, I'm not seeing the exact result as I want. After some precious time, I realize that the query did not return like the Table 2 because it did not order it by its row id. we can achieve the solution like the Table 2 by adding order by clause. Here the most perfect code (Sorry for using exaggerating word :P) :
 select * from SongCategory group by Category order by Id;  
Now I can move forward doing another improvement.. :D


Reference : http://stackoverflow.com/questions/17277152/sqlite-select-distinct-of-one-column-and-get-the-others

0 comments:

Silahkan tinggalkan komentar anda: