sql 分组查询 碰到的问题,, 100结贴,恭候各位大神
SELECT (CASE WHEN N .[State] = '入金成功' THEN isnull(SUM(N .FundApplication), 0) ELSE 0 END) AS 入金总数,
(CASE WHEN N .[State] = '出金成功' THEN isnull(SUM(N .FundApplication), 0) ELSE 0 END) AS 出金总数
FROM dbo.NowApplyForTb AS N group by n.state
我想查出入金成功的总数和出金成功的总数,,数据应该只有一行,,但是上面查询的结果如下
------解决方案--------------------SELECT sum(CASE WHEN N .[State] = '入金成功' THEN N .FundApplication ELSE 0 END) AS 入金总数,
sum(CASE WHEN N .[State] = '出金成功' THEN N .FundApplication ELSE 0 END) AS 出金总数
FROM dbo.NowApplyForTb AS N group by n.state
------解决方案--------------------SELECT
SUM(CASE WHEN N .[State] = '入金成功' THEN isnull(N .FundApplication, 0 ELSE 0 END) AS 入金总数,
SUM(CASE WHEN N .[State] = '出金成功' THEN isnull(N .FundApplication, 0) ELSE 0 END) AS 出金总数
FROM dbo.NowApplyForTb AS N group by n.state
把SUM放到外面
------解决方案--------------------select (select sum(FundApplication) from Nowapplyfortb where state='入金成功') as '入金总数' ,
(select sum(FundApplication) from Nowapplyfortb where state='出金成功') as '出金总数'
这样查出来的就只有一行,, 应该是你要的