如何从我的子查询中一次检索一行

时间:2015-12-29 12:38:17

标签: sql

我需要显示一个表格,其中coloumns为TEST_NAME,TEST_PRODUCT,status1,status2。这里status1应该是外部查询中选择的test_name的状态。 状态2也是在外部查询中但在不同环境中选择的相同测试的状态。我的查询存在以下问题。

  1. status1 / status2的子查询返回多行。
  2. 我不知道如何连接外部查询和内部查询,以便获得在外部查询中选择的测试状态
  3. 我也不确定在子查询和外部查询中是否重复了两次相同的表
  4. select TEST_NAME, TEST_PRODUCT,
    
    (
           SELECT  DR_STATUS FROM PRCQA.PRCBUILD_DR_DAILYRUN@FARMJANGO DAILYRUN,PRCQA.PRCBUILD_DR_TEST@FARMJANGO TEST,PRCQA.PRCBUILD_DR_ENV@FARMJANGO ENV WHERE 
             DAILYRUN.TEST_ID=TEST.ID AND 
    TEST.TEST_PRODUCT IN ('Product Model', 'Product and Catalog Management','Product Hub', 'Product Hub Portal')
          AND  ENV.ENV_NAME LIKE :P2_ENV1  AND DAILYRUN.DR_DATE = to_char (to_date(:P2_DATE1,'DD-Mon-YYYY'), 'DD-MM-YYYY') 
    ) as status1,
    
    (
           SELECT  DR_STATUS FROM PRCQA.PRCBUILD_DR_DAILYRUN@FARMJANGO DAILYRUN,PRCQA.PRCBUILD_DR_TEST@FARMJANGO TEST,PRCQA.PRCBUILD_DR_ENV@FARMJANGO ENV WHERE 
            DAILYRUN.TEST_ID=TEST.ID AND 
    TEST.TEST_PRODUCT IN ('Product Model', 'Product and Catalog Management','Product Hub', 'Product Hub Portal')
    
          AND  ENV.ENV_NAME LIKE :P2_ENV2  AND DAILYRUN.DR_DATE = to_char (to_date(:P2_DATE2,'DD-Mon-YYYY'), 'DD-MM-YYYY') 
    ) as status2
    
    
     from PRCQA.PRCBUILD_DR_DAILYRUN@FARMJANGO DAILYRUN, PRCQA.PRCBUILD_DR_TEST@FARMJANGO TESTOUT
    where  DAILYRUN.TEST_ID=TESTOUT.ID AND  TESTOUT.TEST_PRODUCT IN ('Product Model', 'Product and Catalog Management','Product Hub', 'Product Hub Portal')
    

1 个答案:

答案 0 :(得分:0)

希望这会对你有所帮助

SELECT TEST_NAME
    ,TEST_PRODUCT
    ,(
        SELECT TOP (1) DR_STATUS
        FROM PRCQA.PRCBUILD_DR_DAILYRUN@FARMJANGO DAILYRUN
            ,PRCQA.PRCBUILD_DR_TEST@FARMJANGO TEST
            ,PRCQA.PRCBUILD_DR_ENV@FARMJANGO ENV
        WHERE DAILYRUN.TEST_ID = TEST.ID
            AND TEST.TEST_PRODUCT IN (
                'Product Model'
                ,'Product and Catalog Management'
                ,'Product Hub'
                ,'Product Hub Portal'
                )
            AND ENV.ENV_NAME LIKE :P2_ENV1
            AND DAILYRUN.DR_DATE = to_char(to_date(:P2_DATE1, 'DD-Mon-YYYY'), 'DD-MM-YYYY')
        ) AS status1
    ,(
        SELECT TOP (1) DR_STATUS
        FROM PRCQA.PRCBUILD_DR_DAILYRUN@FARMJANGO DAILYRUN
            ,PRCQA.PRCBUILD_DR_TEST@FARMJANGO TEST
            ,PRCQA.PRCBUILD_DR_ENV@FARMJANGO ENV
        WHERE DAILYRUN.TEST_ID = TEST.ID
            AND TEST.TEST_PRODUCT IN (
                'Product Model'
                ,'Product and Catalog Management'
                ,'Product Hub'
                ,'Product Hub Portal'
                )
            AND ENV.ENV_NAME LIKE :P2_ENV2
            AND DAILYRUN.DR_DATE = to_char(to_date(:P2_DATE2, 'DD-Mon-YYYY'), 'DD-MM-YYYY')
        ) AS status2
FROM PRCQA.PRCBUILD_DR_DAILYRUN@FARMJANGO DAILYRUN
    ,PRCQA.PRCBUILD_DR_TEST@FARMJANGO TESTOUT
WHERE DAILYRUN.TEST_ID = TESTOUT.ID
    AND TESTOUT.TEST_PRODUCT IN (
        'Product Model'
        ,'Product and Catalog Management'
        ,'Product Hub'
        ,'Product Hub Portal'
        )