Hi all!
I have a database table that looks somewhat like this (stripped for the sake of clarity):
customer
====================
name | track
====================
John | one
Sue | two
Greg | one
Loreen | three
Niles | two
Now, I would like to generate a new color for each unique track by my select statement, something like:
SELECT name, track, ('#' || HEX(RANDOMBLOB(3))) AS color
FROM customer;
Unfortunately I can’t modify the table content or the database structure, hence, I’m forced to generate the colors on the fly. The above SELECT query produces something like this:
name | track | color
====================================
John | one | #000000
Sue | two | #444444
Greg | one | #888888
Loreen | three | #CCCCCC
Niles | two | #FFFFFF
i.e. a new color for each row. But that is not what I want! I want all unique tracks to keep the same color, like so:
name | track | color
====================================
John | one | #000000
Sue | two | #444444
Greg | one | #000000
Loreen | three | #888888
Niles | two | #444444
i.e. all ‘one’ tracks should have the ‘#000000’ color, all ‘two’ tracks should have the ‘#444444’ color and so on.
As mentioned before I don’t have the luxury of altering the database, I can only query it. Also: it’s an SQLite database running on an Android device, so I can’t afford those hairy server-side queries either.
My efforts so far:
I have come up with the following query so far:
SELECT a.name, a.track, b.color
FROM 'customer' AS a
LEFT JOIN (
SELECT track, ('#' || HEX(RANDOMBLOB(3))) AS color
FROM 'customer'
GROUP BY track) AS b
ON (b.track == a.track);
But that query is bordering to what I’m allowed to do technically and what I am willing to do architecturally. The changes to the code-base required to make such a query in a well defined way is almost more expensive than the result of it would be worth.
So, what say ye, fellow architects? How can I make this as efficiently as possible?
Why not just create a class that will maintain unique colors per input and have it persist it’s settings through another table or just on the harddisk?
If you really can’t modify the database then I think creating an sql statement that will add colors will just complicate things. What happens when someone says two colors are too similar so they want you to vary them more? It will be easier to manage that in just another class