我在更大的查询中有以下子查询。我收到一个错误“子查询返回超过1的值”。我不确定如何解决这个问题,并且仍然会对两个查询的结果进行划分。我正在使用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
答案 0 :(得分: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