0

より大きなクエリ内に次のサブクエリがあります。「サブクエリが複数の値を返しました」というエラーが表示されます。どうすればこれを修正でき、2 つのクエリの結果が分割されるのかわかりません。SQL Server 2005 を使用しています。

ありがとう。

SELECT 
    sample_fields, -- some fields here
    (SELECT 
        c1/c2 AS department_occupancy_rate  -- doing division of results of both queries
     FROM 
        property as c
        JOIN (
               SELECT store_id, cast(count(*) as decimal(10,2)) AS c1
               FROM property 
               WHERE 
                   non_ha =1 
               AND property_type LIKE '%587%'     
               GROUP BY store_id
             ) AS sub1 
             ON c.store_id = sub1.store_id
             JOIN (
                SELECT store_id, cast(count(*) as decimal(10,2)) AS c2
                FROM property 
                WHERE 
                    property_type LIKE '%587%'
                GROUP BY store_id
             ) AS sub2 
             ON c.store_id = sub2.store_id
        ) as results,
FROM 
    sample_table -- a table here
    INNER JOIN sample_table1 
    ON sample_table2 -- joining here
GROUP BY sample_field -- grouping
4

1 に答える 1

2

内部クエリと外部クエリを関連付ける条件がないため、これでは何をしたいのかわかりません。これは store_id になると思います。そうであれば、次のようにする必要があります。

SELECT sample_fields,-- some fields here
       results.department_occupancy_rate
FROM   sample_table -- a table here
       INNER JOIN sample_table1
               ON sample_table2 -- joining here
       JOIN (SELECT c.store_id,
                    c1 / c2 AS department_occupancy_rate
             -- doing division of results of both queries
             FROM   property AS c
                    JOIN (SELECT store_id,
                                 Cast(Count(*) AS DECIMAL(10, 2)) AS c1
                          FROM   property
                          WHERE  non_ha = 1
                                 AND property_type LIKE '%587%'
                          GROUP  BY store_id) AS sub1
                      ON c.store_id = sub1.store_id
                    JOIN (SELECT store_id,
                                 Cast(Count(*) AS DECIMAL(10, 2)) AS c2
                          FROM   property
                          WHERE  property_type LIKE '%587%'
                          GROUP  BY store_id) AS sub2
                      ON c.store_id = sub2.store_id) AS results
         ON ( sample_table.store_id = results.store_id )
GROUP  BY sample_field -- grouping
于 2012-09-14T16:09:35.323 に答える