similarity - SQLite combine values of similar records into one -


in sqlite database have table called tracks made of following columns: artist, track, genre1, genre2, genre3. table contains many values have same artist , track values different genre1, genre2, genre3 values. example below:

artist | track   | genre1  | genre2 | genre3 abba   | song 1  | rock    | rock   |  rock u2     | song 4  | rock    | rock   |  rock abba   | song 1  | pop     | pop    |  pop u2     | song 4  | pop     | pop    |  pop abba   | song 1  | 70s     | 70s    |  70s u2     | song 4  | 90s     | 90s    |  90s 

i need create sqlite statement amalgamate unique genre values artist , track same, example shown below:

artist | track   | genre1  | genre2 | genre3 abba   | song 1  | rock    | pop    |  70s u2     | song 4  | pop     | rock   |  90s 

any hugely appreciated.

unfortunately looks suffering poor database design. in design above limited 3 genre per song, , when 1 genre applicable song repeat same value across genre fields. in design should able have many or little genre entries per song, can reduce amount of data required store genre values.

so lets talk many-to-many relations , mapping tables. many-to-many relationship in situation many unique genre values belonging many unique song entries. should true reverse, because have many unique songs belonging many (or possibly none) genres.

so how achieve this... first lets make table called genres. should have 2 fields: integer primary key, , string genre value, unique constraint on latter. next need mapping table (for futer reference lets call songgenre), table has 2 foreign key fields, 1 referencing song table , 1 referencing genre table. each record genre belongs song, , can have multiple entries per song.

once mapping table setup can achieve want

select artist, track, group_concat(genre.genrename)  song  join songgenre using (songid)  join genre using (genreid)  group artist, track; 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -