构建复杂的SQL查询

时间:2016-07-04 06:46:56

标签: sql sql-server

我正在使用MS SQL Server Management Studio。我有2个查询,我需要合并为一个。

第一个是(你不需要理解它的含义,只需跳到下一个文本):

SELECT     TOP (100) PERCENT dbo.project.name AS Project_name, Test_Run.documentKey AS TestRun_Key, Test_Run.entitytype, Test_Run.documentKey, 
                  Test_Case.documentKey AS TestCase_Key, document_1.name AS TestPlan_Name, dbo.lookup.name AS TestCase_Release_Status, 
                  Test_Run.testRunStatus AS TestRun_Status, Test_Run.active, dbo.project.id AS Project_ID, dbo.version.versionNumber, Test_Run.executionDate AS Execution_Date, 
                  Test_Run.globalId AS TR_glID, Test_Case.globalId AS TC_glID, Test_Run.createdDate AS TR_Created_Date, Test_Case.id AS TestCase_ID, 
                  Test_Run.id AS TestRun_ID
FROM         dbo.[document] AS Test_Run INNER JOIN
                  dbo.project ON Test_Run.projectId = dbo.project.id INNER JOIN
                  dbo.[document] AS Test_Case ON Test_Run.testCaseId = Test_Case.id INNER JOIN
                  dbo.testrunset ON Test_Run.testRunSetId = dbo.testrunset.id INNER JOIN
                  dbo.[document] ON dbo.testrunset.testCycleId = dbo.[document].id INNER JOIN
                  dbo.[document] AS document_1 ON dbo.[document].testPlanId = document_1.id INNER JOIN
                  dbo.version ON Test_Run.testCaseVersionNumber = dbo.version.versionNumber AND Test_Run.testCaseId = dbo.version.originDocumentId INNER JOIN
                  dbo.documentcustomfieldvalue ON dbo.version.documentId = dbo.documentcustomfieldvalue.documentId INNER JOIN
                  dbo.lookup ON dbo.documentcustomfieldvalue.textValue = dbo.lookup.id
WHERE     (Test_Run.documentTypeId = 55) AND (dbo.documentcustomfieldvalue.fieldId = 216) AND (Test_Run.active = N'T') AND (Test_Case.testCaseStatusId <> 2)

ORDER BY TestCase_Key 我已将此查询保存为 MyView 视图,并在以下查询中使用它:

select TestCase_Key, TestRun_Status, Project_Name

from (select TestCase_Key, TestRun_Status, Project_Name,
         row_number() over(partition by TestCase_Key order by Execution_Date desc) as rn
  from MyView) as T

where rn = 1 and (Project_Name = 'TestProject')
    ORDER BY TestRun_Status, TestCase_Key

现在我需要合并这两个查询,即我不允许在我的用例中使用视图。有人可以帮忙或暗示一下吗?我已经尝试过“in”子句,但没有成功。

1 个答案:

答案 0 :(得分:0)

由于您需要每个TestCase_Key的最新执行日期,因此我修改了原始答案。

我已将您的原始视图用作派生表,并删除了不必要的排序所需的排序和前100%。

select TestCase_Key, TestRun_Status, Project_Name

from (select TestCase_Key, TestRun_Status, Project_Name,
         row_number() over(partition by TestCase_Key order by Execution_Date desc) as rn
  from 
  (SELECT    dbo.project.name AS Project_name, Test_Run.documentKey AS TestRun_Key, Test_Run.entitytype, Test_Run.documentKey, 
                  Test_Case.documentKey AS TestCase_Key, document_1.name AS TestPlan_Name, dbo.lookup.name AS TestCase_Release_Status, 
                  Test_Run.testRunStatus AS TestRun_Status, Test_Run.active, dbo.project.id AS Project_ID, dbo.version.versionNumber, Test_Run.executionDate AS Execution_Date, 
                  Test_Run.globalId AS TR_glID, Test_Case.globalId AS TC_glID, Test_Run.createdDate AS TR_Created_Date, Test_Case.id AS TestCase_ID, 
                  Test_Run.id AS TestRun_ID
FROM         dbo.[document] AS Test_Run INNER JOIN
                  dbo.project ON Test_Run.projectId = dbo.project.id INNER JOIN
                  dbo.[document] AS Test_Case ON Test_Run.testCaseId = Test_Case.id INNER JOIN
                  dbo.testrunset ON Test_Run.testRunSetId = dbo.testrunset.id INNER JOIN
                  dbo.[document] ON dbo.testrunset.testCycleId = dbo.[document].id INNER JOIN
                  dbo.[document] AS document_1 ON dbo.[document].testPlanId = document_1.id INNER JOIN
                  dbo.version ON Test_Run.testCaseVersionNumber = dbo.version.versionNumber AND Test_Run.testCaseId = dbo.version.originDocumentId INNER JOIN
                  dbo.documentcustomfieldvalue ON dbo.version.documentId = dbo.documentcustomfieldvalue.documentId INNER JOIN
                  dbo.lookup ON dbo.documentcustomfieldvalue.textValue = dbo.lookup.id
WHERE     (Test_Run.documentTypeId = 55) AND (dbo.documentcustomfieldvalue.fieldId = 216) AND (Test_Run.active = N'T') AND (Test_Case.testCaseStatusId <> 2)) 
 as DerivedTable) as T

where rn = 1 and (Project_Name = 'TestProject')
    ORDER BY TestRun_Status, TestCase_Key

如果这是您所需要的,那么您可以通过删除其中一个图层来展平它,并删除最终结果不需要的额外列和表

这里是扁平化的,希望你能够在内部select语句中删除一些连接:

    SELECT TestCase_Key
    , TestRun_Status
    , Project_Name
FROM
    (
    SELECT dbo.project.name AS Project_name
        , Test_Case.documentKey AS TestCase_Key
        , Test_Run.testRunStatus AS TestRun_Status
        , Test_Run.executionDate AS Execution_Date
    FROM dbo.[document] AS Test_Run
        INNER JOIN dbo.project ON Test_Run.projectId = dbo.project.id
    INNER JOIN dbo.[document] AS Test_Case ON Test_Run.testCaseId = Test_Case.id
    INNER JOIN dbo.testrunset ON Test_Run.testRunSetId = dbo.testrunset.id
    INNER JOIN dbo.[document] ON dbo.testrunset.testCycleId = dbo.[document].id
    INNER JOIN dbo.[document] AS document_1 ON dbo.[document].testPlanId = document_1.id
    INNER JOIN dbo.version ON Test_Run.testCaseVersionNumber = dbo.version.versionNumber
                         AND Test_Run.testCaseId = dbo.version.originDocumentId
    INNER JOIN dbo.documentcustomfieldvalue ON dbo.version.documentId = dbo.documentcustomfieldvalue.documentId
    INNER JOIN dbo.lookup ON dbo.documentcustomfieldvalue.textValue = dbo.lookup.id
    WHERE(Test_Run.documentTypeId = 55)
        AND (dbo.documentcustomfieldvalue.fieldId = 216)
        AND (Test_Run.active = N'T')
        AND (Test_Case.testCaseStatusId <> 2)
    ) AS DerivedTable
WHERE ROW_NUMBER() OVER(PARTITION BY TestCase_Key ORDER BY Execution_Date DESC) = 1
     AND (Project_Name = 'TestProject')
ORDER BY TestRun_Status
      , TestCase_Key;