Sql 对子查询返回的数据进行分组
我有以下根据帐户类型返回值的查询:Sql 对子查询返回的数据进行分组,sql,Sql,我有以下根据帐户类型返回值的查询: SELECT COALESCE(B.classdescription, 'S CORPORATION') AS "DESCRIPTION", Count(*) AS "NUMBEROFACCOUNTS", Sum(Cast(current_cert_shares AS DECIMAL(20, 3))) + Sum(Cast(current_
SELECT COALESCE(B.classdescription, 'S CORPORATION') AS "DESCRIPTION",
Count(*) AS "NUMBEROFACCOUNTS",
Sum(Cast(current_cert_shares AS DECIMAL(20, 3)))
+ Sum(Cast(current_book_shares AS DECIMAL(20, 3)))
+ Sum(Cast(dr_curr_shares AS DECIMAL(20, 3))) AS TOTAL
FROM testtable1 A
LEFT JOIN testtable2 B
ON A.class_code = B.classcode
WHERE conumber = 00000
AND A.special_account_code NOT IN( 001 )
GROUP BY B.classdescription,
A.class_code
要求仅显示类别代码“001”、“002”、“003”、“004”、“005”的数据。其余类别代码(006至010)的数据需要进行整理,并显示为说明“其他”,以及类别代码006至010的科目数和总计
下图将显示所需的视图。无需计算%OS值:
如何通过单个查询实现此视图
“其他”的总值可由以下公式得出:
SELECT Sum(a.numberofaccounts) AS other,
Sum(a.total) AS othertotal
FROM (
SELECT COALESCE(b.classdescription, 'S CORPORATION') AS "DESCRIPTION",
Count(*) AS "NUMBEROFACCOUNTS",
Sum(Cast(current_cert_shares AS DECIMAL(20,3))) +
Sum(Cast(current_book_shares AS DECIMAL(20,3))) +
Sum(Cast(dr_curr_shares AS DECIMAL(20,3))) AS total
FROM accountmaster A
LEFT JOIN classcodes B
ON a.class_code = b.classcode
WHERE conumber = 11991
AND a.special_account_code NOT IN(02) --and
class_code NOT IN( '042', '010', '012', '022', '011')
GROUP BY b.classdescription,
a.class_code ) a
向我们显示数据库模式、示例数据、当前和预期输出。请阅读,这里是学习如何提高问题质量和获得更好答案的好地方。尝试在Show us db schema、示例数据、当前和预期输出中创建一个示例。请阅读,这里是学习如何提高问题质量和获得更好答案的好地方。尝试在中创建一个示例