简体   繁体   中英

SQL query to format table data for DataSource in GridView

I am looking for a SQL Server query that could transfer source SQL table data:

TextID | Text  | LanguageID
-------|-------|-------------------------------------
app.aa | Hi    | 6a13ea09-46ea-4c93-9b6a-e26bdc6ff4d8
app.cc | Hund  | 0c894bb7-4937-4903-906a-d1b1dd64935c
app.aa | Hallo | 0c894bb7-4937-4903-906a-d1b1dd64935c
app.cc | Dog   | 6a13ea09-46ea-4c93-9b6a-e26bdc6ff4d8
app.bb | Star  | 6a13ea09-46ea-4c93-9b6a-e26bdc6ff4d8
... 

into table like this one:

TextID | Original | Translated
-------|----------|-----------
app.aa | Hi       | Hallo
app.bb | Star     | -
app.cc | Dog      | Hund
...

so that I can use it as a DataSource for GridView in ASP .NET. Thank you in advance for your help.

Whenever you need to combine data from two different rows into one, you need to join . For example:

select src.TextID "TextID", src.Text "Original", tr.Text "Translated"
from source_table src
left join source_table tr
on src.TextID = tr.TextID
and src.LangID = 'xxx'  -- xxx is the source language id
and tr.LangID = 'yyy' -- yyy is the target language id

The left join ensures that untranslated words are included with a null translated value. To make a table for your DataSource, you'll need to wrap create table (or maybe create view ) around the select :

create table translations as
    select ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM