Skip to content
Advertisement

SQL: How to partition data by the key and transpose the triples in the defined order? (The tool for a human-language translator.)

I do have the table with translated texts to (possibly) many languages. When new text is entered in the chosen language, the lang_hash is calculated. If the text is not bound to any existing text in other languages, the lang_hash is repeated in the key_hash, and–together with the lang code–the new record is inserted. For the Czech, the English, and the German languages, the content of the table could look like this:

The cs and en languages are meant to be obligatory. Here the de language is meant to be filled by a human translator. Anyway, a German user is also allowed to enter some new terms. The first entered term defines the key. I have separated the records with the same key.

From time to time, the human translator should look at the data and fill or the obligatory cs and en terms, or to add the de translation for the cs and en equivalents.

He or she should always works with exactly three languages: cs, en, and the chosen one. Say, the de is the chosen one for now. The translator should see a table like this (the key_hash will not be visible):

enter image description here

The task is to enter all the empty (pink) fields.

How can I write the SQL query to get the result organized as shown on the picture, with NULL values for the empty fields? Is there any elegant way to partition the data based on the key_hash and then transpose the language records in the order of cs, en, de?

Update: So far, I have the following query that shows what I need. However, it seems a bit uggly to me. And also, I will have much more data in future, and I am not sure it would work for them. (Sorry for the Czech identifiers: klic means key, jazyk means language, preklady means translations)

enter image description here

Advertisement

Answer

You need conditional aggregation:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement