1

Suppose i had this table

translationId ItemId    Lang   Content
0             0          FR    Livre  
1             0          DE    Buch
2             0          EN    Book
3             0          NL    Boek
4             1          FR    Stylo
5             1          EN    Pen
6             2          FR    Plaque
7             2          NL    Plaat
8             2          EN    Plate

i want to do a select statement that gives me the following result

ItemId    Lang   Content
  0          FR    Livre  
  0          DE    Buch
  0          EN    Book   
  0          NL    Boek
  1          FR    Stylo
  1          EN    Pen    
  1          DE    Pen     /*it defaulted to EN*/
  1          NL    Pen     /*it defaulted to EN*/
  2          DE    Plate   /*it defaulted to EN*/    
  2          EN    Plate
  2          FR    Plaque
  2          NL    Plaat

what happenes in the result is that if no entery available for a specific language content defaults to english, english is always available. what is the best way to do this?

5
  • Do you have a Language table that has all the possible languages? Commented Dec 4, 2014 at 15:23
  • @TabAlleman no i generate one with select distinct Commented Dec 4, 2014 at 15:24
  • 1
    It's not exactly clear what you want; if a Language code for an ItemId isn't found, you want to return a row with the missing Language Code, ItemId and the Content from it's EN counterpart? Commented Dec 4, 2014 at 15:54
  • @LittleBobbyTables exactly Commented Dec 4, 2014 at 15:56
  • 2
    I was interpretting it incorrectly as well @LittleBobbyTables. What I believe he actually wants, is for each distinct language found in the table, create an entry for that language using the "EN" version of the "Content" should a translation for that language not already exist. Sorry if that's wordy, but I think that's what OP is getting at. Commented Dec 4, 2014 at 15:57

1 Answer 1

2

You can create a subquery that contains the itemId and Content of each Lang='EN' record, cross joining that on a distinct list of languages to get a list of all potential replacement records.

From there, you can a combination of a RIGHT OUTER JOIN and COALESCE to determine what language records are missing for each itemId, and replace them.

-- Create our temp table for testing, 'cause I'm too lazy to fiddle
SELECT 0 as translationId, 0 as ItemId, 'FR' as Lang, 'Livre' as Content
INTO #Test
UNION SELECT 1, 0, 'DE', 'Buch'
UNION SELECT 2, 0, 'EN', 'Book'
UNION SELECT 3, 0, 'NL', 'Boek'
UNION SELECT 4, 1, 'FR', 'Stylo'
UNION SELECT 5, 1, 'EN', 'Pen'
UNION SELECT 6, 2, 'FR', 'Plaque'
UNION SELECT 7, 2, 'NL', 'Plaat'
UNION SELECT 8, 2, 'EN', 'Plate'

-- Query starts here, replace #Test with your table name
SELECT  
    COALESCE(T1.itemId, T2.itemId) AS itemId,
    COALESCE(T1.Lang, T2.Lang) AS Lang,
    COALESCE(T1.Content, T2.Content) AS Content
FROM    #Test T1
RIGHT OUTER JOIN (
    /*
    Create a subquery that contains the Content and itemId records
    for each Lang='EN' record; we'll RIGHT OUTER JOIN on this
    */  
    SELECT itemId, Languages.Lang, Content
    FROM #Test
    CROSS JOIN (
        -- Get our distinct list of languages
        SELECT DISTINCT Lang
        FROM #Test
    ) Languages
    WHERE #Test.Lang = 'EN'
) T2 ON T1.itemId = T2.itemId and T1.Lang = T2.Lang
ORDER BY COALESCE(T1.itemId, T2.itemId)

DROP TABLE #Test  -- Clean up our temp table
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.