我有以下查询,它根据帐户类型返回值:
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)的数据需要整理并显示为描述“OTHER”,其中包括NUMBEROFACCOUNTS和TOTAL的总和,用于类代码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