2

我正在做一个查询,它从表的行中获取数据并将其放在同一列下:

SELECT [t].*,
       [d].[Description] AS [Name],
FROM   [TrainerClass] AS [t] join [DescriptionTranslation] AS [d] on [t].[Code] = [d].[Code]
WHERE  [d].[TableName] = 'TrainerClass' AND 
       [d].[FieldName] = 'Name' AND 
       [d].[Language] = 'en-EN';

这工作得很好,但它返回一个单列。我想用更多的列得到相同的结果,但我不知道如何使用 WHERE 语句来做到这一点。我想实现类似的目标:

SELECT [t].*,
       [d].[Description] AS [Name] WHERE [d].[FieldName] = 'Name',
       [d].[Description] AS [Info] WHERE [d].[FieldName] = 'Info',
FROM   [TrainerClass] AS [t] join [DescriptionTranslation] AS [d] on [t].[Code] = [d].[Code]
WHERE  [d].[TableName] = 'TrainerClass' AND 
       [d].[Language] = 'en-EN';

我知道这不是一个正确的语法,这就是我问你如何得到这个结果的原因。先感谢您 :)

编辑:

我不想要的:

不

我想要的是:

是的

(仅以一条记录制作的示例,应该还有更多)

4

4 回答 4

4

您可以使用CASE如下声明(我认为这就是您要查找的内容)。请注意,每种情况下不匹配的行都会得到空值,或者您可以将 else 部分添加到 case 语句中。

SELECT [t].*,
       CASE WHEN [d].[FieldName] = 'Name' THEN [d].[Description] END AS [Name],
       CASE WHEN [d].[FieldName] = 'Info' THEN [d].[Description] END AS [Info]

FROM   [TrainerClass] AS [t] join [DescriptionTranslation] AS [d] 
                             on [t].[Code] = [d].[Code]
WHERE  [d].[TableName] = 'TrainerClass' AND  --[d].[FieldName] = 'Name' AND 
       [d].[Language] = 'en-EN';

更新:

根据您的更新,要从显示的图片表中获得预期结果,您可以使用以下Max()功能Group by

;With cte as 
(
    --your first query goes here 
)
Select Id,Code, Max(Name) Name, Max(info) Info
from cte
Group by Id, Code

你可以不做CTE

SELECT t.Id, t.Code,
       Max(CASE WHEN d.FieldName = 'Name' THEN d.Description END) AS Name,
       Max(CASE WHEN d.FieldName = 'Info' THEN d.Description END) AS Info
FROM   [TrainerClass] AS t join [DescriptionTranslation] AS d  on t.Code = d.Code
WHERE  d.TableName = 'TrainerClass' AND  --[d].[FieldName] = 'Name' AND 
       d.Language = 'en-EN'
GROUP BY t.Id, t.Code;
于 2013-10-03T17:08:25.770 回答
1

您可以使用子查询进行查询的另一种方式,它与您的意图非常相似。但是这种方法只有一个警告,那就是如果你有大量的记录集,它可能会很慢。


SELECT [t].*,
( SELECT [d].[Description]  FROM [DescriptionTranslation] [d] WHERE [d].[FieldName] = 'Name' and [t].[Code] = [d].[Code] [d].[TableName] = 'TrainerClass' AND 
[d].[Language] = 'en-EN') AS [Name], 
(SELECT [d].[Description]  FROM [DescriptionTranslation] [d] WHERE [d].[FieldName] = 'Info' and [t].[Code] = [d].[Code] [d].[TableName] = 'TrainerClass' AND 
[d].[Language] = 'en-EN') AS [Info]     
FROM   [TrainerClass] AS [t] 
于 2013-10-03T19:37:43.193 回答
1

如果我正确阅读了您的请求,则您需要使用不同的过滤器两次加入表格。

select t.*, dName.Description as [Name], dInfo.Description as [Info]
from TrainerClass t
join DescriptionTranslation dName
    on t.Code = dName.Code and dName.FieldName = 'Name'
join DescriptionTranslation dInfo
    on t.Code = dInfo.Code and dInfo.FieldName = 'Info'
where
    dName.TableName = 'TrainerClass' and dName.Language = 'en-EN'
    and dInfo.TableName = 'TrainerClass' and dInfo.Language = 'en-EN'
于 2013-10-03T16:52:11.700 回答
0

使用下面CROSS APPLY的查询

SELECT  [t].*,
        [DTNameDescription].[Description] AS [Name]
        [DTInfoDescription].[Description] AS [Info] 
FROM    [TrainerClass] AS [t] 
        CROSS APPLY (
            SELECT  [Description] 
            FROM    [DescriptionTranslation] AS [d] 
            WHERE   [t].[Code] = [d].[Code] 
                        AND [d].[FieldName] = 'Name'
        ) AS DTNameDescription
        CROSS APPLY (
            SELECT  [Description] 
            FROM    [DescriptionTranslation] AS [d] 
            WHERE   [t].[Code] = [d].[Code] 
                        AND [d].[FieldName] = 'Info'
        ) AS DTInfoDescription
WHERE   [d].[TableName] = 'TrainerClass' AND 
        [d].[Language] = 'en-EN';
于 2017-08-18T09:34:26.177 回答