如何在子查询中进行求和和分组[关闭]

问题描述 投票:0回答:1

我正在尝试在子查询中计算以一对多关系连接的列的总和,并按外键分组,以便对于与同一键相关的每个记录,都有所有这些记录的小计.

我尝试的子查询是

(SELECT SUM(est_valuation.total_value) 
FROM est_valuation
WHERE est_valuation.current_val = 'Y'
GROUP BY est_valuation.site_id)
AS 'Combined Valuation(s) Total'

它可以在没有

GROUP BY
子句的情况下工作,但没有用,并且无法在主查询中计算,因为我只想要 WHERE est_valuation.current_val = 'Y' 行专门用于此计算,并且不想要按查询其余部分的当前评估状态进行筛选。

我尝试完成此操作但出现错误

SQLSTATE[21000]:基数违规:1242 子查询返回更多 超过 1 行

我已经没有主意了

e:

这是整个查询

SELECT 
site.site_code AS 'Site Code', 
site.site_desc AS 'Site Description', 
site.site_alias AS 'Site Alias', 
site_type.site_type_desc AS 'Site Description', 
site_designation.site_designation_desc AS 'Site Designation', 
site_classification.site_classification_desc AS 'Site Classification', 
site.uprn AS 'UPRN', 
site.addr_first_obj AS 'Address First Line', 
site.addr_second_obj AS 'Address Second Line', 
site.addr_street AS 'Address Street', 
site.addr_locality AS 'Address Locality', 
site.addr_town AS 'Address Town', 
site.addr_region AS 'Address Region', 
site.addr_postcode AS 'Address Postcode', 
ward.ward_desc AS 'Ward', 
parish.parish_desc AS 'Parish', 
district.district_desc AS 'District', 
prop_facility_asset_category_type.prop_facility_asset_category_type_desc AS 'IFRS Type',   
prop_facility_asset_category.prop_facility_asset_category_desc AS 'IFRS Category', 
site_usage.site_usage_desc AS 'Site Usage', 
committee.committee_desc AS 'Committee', 
site.area AS 'Area',  
SUM(building.gia) AS 'GIA', 
CONCAT(building_age.building_age_desc) AS 'Building Age', 
site.vacant AS 'Vacant', 
site.vacant_date AS 'Vacancy Date', 
site.comment AS 'Comment', 
gen_userdef_group.user_text1, 
gen_userdef_group.user_text2, 
gen_userdef_group.user_text3',
gen_userdef_group.user_text4, 
gen_userdef_group.user_text5, 
gen_userdef_group.user_text6, 
gen_userdef_group.user_check1, 
gen_userdef_group.user_check2, 
gen_userdef_group.user_check3, 
gen_userdef_group.user_check4,
prop_strategy.strategy_strap_line,
prop_strategy.current_strategy AS,
prop_strategy.strategy_maintenance,
prop_strategy.comments,
prop_strategy.last_review_date,

(SELECT SUM(est_valuation.total_value) 
FROM est_valuation
WHERE est_valuation.current_val = 'Y'
GROUP BY est_valuation.site_id)
AS 'Combined Valuation(s) Total


FROM site

LEFT JOIN site_type
ON site.site_type_id = site_type.site_type_id
LEFT JOIN site_designation
ON site.site_designation_id = site_designation.site_designation_id
LEFT JOIN site_classification
ON site.site_classification_id = site_classification.site_classification_id
LEFT JOIN ward
ON site.ward_id = ward.ward_id
LEFT JOIN parish
ON site.parish_id = parish.parish_id
LEFT JOIN district
ON site.district_id = district.district_id
LEFT JOIN prop_facility_asset_category
ON site.prop_facility_asset_category_id = prop_facility_asset_category.prop_facility_asset_category_id
LEFT JOIN site_usage
ON site.site_usage_id = site_usage.site_usage_id
LEFT JOIN committee
ON site.committee_id = committee.committee_id
LEFT JOIN building
ON site.site_id = building.site_id
LEFT JOIN building_age
ON building.building_age_id = building_age.building_age_id
LEFT JOIN gen_userdef_group
ON site.gen_userdef_group_id = gen_userdef_group.gen_userdef_group_id
LEFT JOIN prop_facility_asset_category_type
ON prop_facility_asset_category.prop_facility_asset_category_type_id = prop_facility_asset_category_type.prop_facility_asset_category_type_id
LEFT JOIN prop_facility_asset_subcategory
ON prop_facility_asset_category.prop_facility_asset_subcategory_id = prop_facility_asset_subcategory.prop_facility_asset_subcategory_id
LEFT JOIN prop_strategy
ON site.site_id = prop_strategy.site_id
LEFT JOIN est_valuation
ON site.site_id = est_valuation.site_id

WHERE site.active = 'Y'

GROUP BY site.site_code;````

04/07/2024 update:

I've picked this one back up again, I've tried making it a correlated subquery as advised

adding

````SELECT total_value

and

WHERE site.active = 'Y'

AND total_value =
(SELECT SUM(est_valuation.total_value)  
FROM est_valuation
WHERE est_valuation.current_val = 'Y') ````

which makes the query valid again but also returns zero rows
sql group-by sum mariadb subquery
1个回答
-1
投票

您的子查询应包含现场过滤器,以确保它返回 1 行。删除分组依据并尝试

WHERE est_valuation.current_val = 'Y' AND est_valuation.site_id=site.site_code

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.