我可以将select语句中的select转换为自联接吗?

时间:2017-02-02 15:01:59

标签: sql-server tsql select join

这可能是一个愚蠢的问题,但我无法弄清楚这一点 我有这样的查询:

select TERM_SID
,max(PUBLICDATA) as PublicData
,max(PUBLIC_AGGREGATE) as Public_AGGregate
,max(INTERNAL) as Internal
,max(INTERNAL_AGGREGATE) as internal_Aggregate
,max(LIMITED) as Limited
,max(RESTRICTED) as Restricted
from
(
      SELECT  TERM_SID,
      CASE WHEN d2.DATA_CLASSIFICATION_DESC='Public data' THEN 'Y' ELSE 'N' END AS 'PUBLICDATA',
      CASE WHEN d2.DATA_CLASSIFICATION_DESC='Public data in aggregate' THEN 'Y'  ELSE 'N' END as 'PUBLIC_AGGREGATE',
      CASE WHEN d2.DATA_CLASSIFICATION_DESC='Internal data' THEN 'Y'  ELSE 'N' END as 'INTERNAL',
      CASE WHEN d2.DATA_CLASSIFICATION_DESC='Internal data in aggregate' THEN 'Y'  ELSE 'N' END as 'INTERNAL_AGGREGATE',
      CASE WHEN d2.DATA_CLASSIFICATION_DESC='Limited data' THEN 'Y'  ELSE 'N' END as 'LIMITED',
      CASE WHEN d2.DATA_CLASSIFICATION_DESC='Restricted data' THEN 'Y'  ELSE 'N' END as 'RESTRICTED'
      FROM [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION] d2
      JOIN [UDW_DATA_DICTIONARY].[dbo].[DATA_CLASSIFICATION_MAPPING] dm2 ON dm2.DATA_CLASSIFICATION_SID=d2.DATA_CLASSIFICATION_SID
) mat
group by Term_SID

并且我试图转换它但是我一直在内部选择中加入绊倒,有没有办法通过连接来重建它以提高性能。

1 个答案:

答案 0 :(得分:2)

使用Common Table Expression(CTE)将其转换为派生表,您可以像引用任何其他表一样引用它:

[Activity(Label = "APItest", MainLauncher = true)]