区别于不在另一台服务器的select数据库中工作

时间:2018-03-09 20:49:43

标签: sql sql-server tsql distinct

我在这里选择:

SELECT * FROM (
    SELECT DISTINCT cod_arq, 
           (SELECT description FROM [serverTwo].[perg].dbo.marc_section WHERE paragraph = 10 AND cod_arq = AC.cod_arq) AS author,
           (SELECT description FROM [serverTwo].[perg].dbo.marc_section WHERE paragraph = 24 AND cod_arq = AC.cod_arq) AS title,
           (SELECT description FROM [serverTwo].[perg].dbo.marc_section WHERE paragraph = 25 AND cod_arq = AC.cod_arq) AS edt
    FROM [serverTwo].[pergamarc].dbo.marc_section AC
) AS ACN
WHERE ACN.author IS NOT NULL 
    AND ACN.title IS NOT NULL

我在serverOne上测试了一些sql到serverTwo,没关系,我可以从一台服务器调用它们到另一台服务器。 但是如果我在" serverTwo"上运行它,那么上面的sql工作正常。 (表示sintaxe是正确的),但如果我从serverOne运行它,则不行。但是,如果我删除了DISTINCT关键字,它就可以工作(显示重复的行,但有效)。

这个DISTINCT可能会发生什么?也许有些COLLATE冲突?

1 个答案:

答案 0 :(得分:1)

我认为你只想要条件聚合:

SELECT acn.*
FROM (SELECT ac.cod_arq, 
           MAX(CASE WHEN paragraph = 10 THEN description END) as author,
           MAX(CASE WHEN paragraph = 24 THEN description END) as title,
           MAX(CASE WHEN paragraph = 25 THEN description END) as edt
      FROM [serverTwo].[pergamarc].dbo.marc_section AC
      GROUP BY ac.cod_arq
     ) AS ACN
WHERE ACN.author IS NOT NULL AND ACN.title IS NOT NULL;

子查询不是绝对必要的。您可以改为使用HAVING子句。