如何减少在SQL中查询此视图的次数?

时间:2015-11-13 17:39:35

标签: sql-server

我在下面有一个查询,但它可以访问相同的视图12次。

有没有办法减少这种观点的使用?

SELECT TOP 1 METED_Applications = (
        SELECT COUNT(hcc_programtypename)
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - Met-Ed - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,METED_MWh = (
        SELECT SUM(hcc_identifiedkwhsavings) / 1000
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - Met-Ed - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,METED_Incentives = (
        SELECT SUM(hcc_identifiedincentivedollars_base) / 1000
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - Met-Ed - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,PENELEC_Applications = (
        SELECT COUNT(hcc_programtypename)
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - Penelec - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,PENELEC_MWh = (
        SELECT SUM(hcc_identifiedkwhsavings) / 1000
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - Penelec - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,PENELEC_Incentives = (
        SELECT SUM(hcc_identifiedincentivedollars_base) / 1000
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - Penelec - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,PENNPOWER_Applications = (
        SELECT COUNT(hcc_programtypename)
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - Penn Power - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,PENNPOWER_MWh = (
        SELECT SUM(hcc_identifiedkwhsavings) / 1000
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - Penn Power - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,PENNPOWER_Incentives = (
        SELECT SUM(hcc_identifiedincentivedollars_base) / 1000
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - Penn Power - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,WESTPENNPOWER_Applications = (
        SELECT COUNT(hcc_programtypename)
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - West Penn Power - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,WESTPENNPOWER_MWh = (
        SELECT SUM(hcc_identifiedkwhsavings) / 1000
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - West Penn Power - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )
    ,WESTPENNPOWER_Incentives = (
        SELECT SUM(hcc_identifiedincentivedollars_base) / 1000
        FROM Filteredhcc_project
        WHERE hcc_programtypename IN ('FE - West Penn Power - Institutional')
            AND statecode = '0'
            AND statuscode <> '801000017'
            AND cr_reservationdate <= '2015-09-02'
        )

1 个答案:

答案 0 :(得分:2)

您可以使用针对基表/视图和条件聚合的一个查询来执行此操作。

SELECT A = COUNT(CASE WHEN hcc_programtypename = 'FE - Met-Ed - Institutional' THEN 1 END),
       B = SUM(CASE WHEN hcc_programtypename = 'FE - Met-Ed - Institutional' THEN hcc_identifiedkwhsavings END) / 1000,
       C = SUM(CASE WHEN hcc_programtypename = 'FE - Met-Ed - Institutional' THEN hcc_identifiedincentivedollars_base END) / 1000,
       D = COUNT(CASE WHEN hcc_programtypename = 'FE - Penelec - Institutional' THEN 1 END),
       E = SUM(CASE WHEN hcc_programtypename = 'FE - Penelec - Institutional' THEN hcc_identifiedkwhsavings END) / 1000,
       F = SUM(CASE WHEN hcc_programtypename = 'FE - Penelec - Institutional' THEN hcc_identifiedincentivedollars_base END) / 1000,
       G = COUNT(CASE WHEN hcc_programtypename = 'FE - Penn Power - Institutional' THEN 1 END),
       H = SUM(CASE WHEN hcc_programtypename = 'FE - Penn Power - Institutional' THEN hcc_identifiedkwhsavings END) / 1000,
       I = SUM(CASE WHEN hcc_programtypename = 'FE - Penn Power - Institutional' THEN hcc_identifiedincentivedollars_base END) / 1000,
       J = COUNT(CASE WHEN hcc_programtypename = 'FE - West Penn Power - Institutional' THEN 1 END),
       K = SUM(CASE WHEN hcc_programtypename = 'FE - West Penn Power - Institutional' THEN hcc_identifiedkwhsavings END) / 1000,
       L = SUM(CASE WHEN hcc_programtypename = 'FE - West Penn Power - Institutional' THEN hcc_identifiedincentivedollars_base END) / 1000
FROM   Filteredhcc_project
WHERE  statecode = '0'
    AND statuscode <> '801000017'
    AND cr_reservationdate <= '2015-09-02'
    AND hcc_programtypename IN ( 'FE - Met-Ed - Institutional', 
                                    'FE - Penelec - Institutional', 
                                    'FE - Penn Power - Institutional', 
                                    'FE - West Penn Power - Institutional' )