SQL在表中找到缺少的语言条目

时间:2015-06-29 10:54:59

标签: sql sql-server tsql

我有一张表缺少某种语言的某些条目。如何获得所有英语语言文本列表(表1中的lang 1)缺少外语翻译对应文本(lang 2)

我的表格如下

<Style x:Key="MyFont" TargetType="Label">
        <Setter Property="FontFamily" Value="Resources/#BuxtonSketch.ttf" />
    </Style>

例如,使用上面的表数据,只应返回以下内容:

PageName | LanguageNo | TranslationName | TranslationText   |
main     |     1      | SomeName        | some english text |
main     |     2      | SomeName        | some foreign text |
main     |     1      | SomeName2       | some english 2    |
other    |     1      | SomeName3       | some english 3    |
other    |     2      | SomeName3       | some foreign 3    |

如何编写SQL语句来实现此目的?

由于

5 个答案:

答案 0 :(得分:2)

您可以尝试以下操作:

-- Create demo data
CREATE TABLE #translation(pageName nvarchar(10), LanguageNo int, TranslationName nvarchar(25), TranslationText nvarchar(50))

INSERT INTO #translation(pageName, LanguageNo, TranslationName, TranslationText)
VALUES  ('main',1,'SomeName','some english text'),
        ('main',2,'SomeName','some foreign text'),
        ('main',1,'SomeName2','some english 2'),
        ('other',1,'SomeName3','some english 3'),
        ('other',2,'SomeName3','some foreign 3')
        --,('other',3,'SomeName3','some foreign 3') -- uncomment for language3 demo

-- your work:
SELECT availTrans.*
FROM #translation t
-- get all needed combinations
RIGHT JOIN(
        SELECT DISTINCT t.pageName, t.TranslationName, langs.LanguageNo
        FROM #translation as t
        CROSS JOIN (SELECT DISTINCT LanguageNo FROM #translation) as langs
    ) as availTrans
    ON t.pageName = availTrans.pageName
    AND t.TranslationName = availTrans.TranslationName
    AND t.LanguageNo = availTrans.LanguageNo
WHERE t.pageName IS NULL

-- Cleanup
DROP TABLE #translation

给定输入:

pageName   LanguageNo  TranslationName           TranslationText
---------- ----------- ------------------------- ---------------------
main       1           SomeName                  some english text
main       2           SomeName                  some foreign text
main       1           SomeName2                 some english 2
other      1           SomeName3                 some english 3
other      2           SomeName3                 some foreign 3

产生这个结果:

pageName   TranslationName           LanguageNo
---------- ------------------------- -----------
main       SomeName2                 2

答案 1 :(得分:1)

有几种方法,但这里有一种方法,使用not exists

select t.*
from mytable t
where t.LanguageNo = 1 and
      not exists (select 1
                  from mytable t2
                  where t2.pagename = t.pagename and 
                        t2.translationname = t.translationname and
                        t2.LanguageNo = 2
                 );

Here是一个SQL小提琴。

答案 2 :(得分:1)

SELECT t1.*
FROM tblTranslation t1
LEFT JOIN tblTranslation t2 ON t2.PageName = t1.PageName  
                            AND t2.TranslationName = t1.TranslationName 
                            AND t2.LanguageNo = 2
WHERE t1.LanguageNo = 1
AND t2.LanguageNo IS NULL

答案 3 :(得分:1)

SELECT t1.*
  FROM tblTranslation t1
  LEFT JOIN tblTranslation t2 
    ON t2.TranslationName = t1.TranslationName        
   AND t2.LanguageNo = 2
 WHERE t2.TranslationName IS NULL
   AND t1.LanguageNo = 1

答案 4 :(得分:1)

您也可以将SUMOVER()这样使用。

<强>查询

;WITH CTE AS
(
    SELECT pageName, LanguageNo, TranslationName, TranslationText,
    SUM(CASE WHEN LanguageNo = 1 THEN 1 ELSE 0 END) OVER(PARTITION BY TranslationName) L1,
    SUM(CASE WHEN LanguageNo = 2 THEN 1 ELSE 0 END)  OVER(PARTITION BY TranslationName) L2
    FROM #translation t
)
SELECT pageName, LanguageNo, TranslationName, TranslationText FROM CTE
WHERE L1 >=1 AND L2 = 0

<强>输出

pageName    LanguageNo  TranslationName TranslationText
main    1   SomeName2   some english 2