我想对我的sql查询的两个结果求和,并且求和结果将再次用于对第二个sql查询求和
SQL SERVER
首选SQL SERVER查询
SELECT b.paytype, CONVERT(nvarchar, CAST(SUM(b.principal) AS money), 1) as 'Cash Sales'
FROM fin_tbl_crjhead a
INNER JOIN fin_tbl_crjlot b
ON a.uid = b.headuid
left JOIN fin_tbl_crjsundries c ON b.headuid = c.uid
WHERE a.ordate BETWEEN '2018-12-01' AND '2018-12-31'
AND a.brcode = '010'
GROUP BY b.paytype
ORDER BY b.paytype ASC
我得到正确的结果,但是我不能将两个结果相加:
第二个SQL Server查询
SELECT SUM(a.credit)
FROM [TFINANCE].[dbo].[fin_tbl_crjsundries] a INNER JOIN fin_tbl_crjlot b
on a.headuid = b.headuid INNER JOIN fin_tbl_crjhead c ON c.uid = b.headuid
where a.acctcode = '100 200-02' and a.brcode = '010' and c.ordate
between '2018-12-01' and '2018-12-31'
第二个查询的结果为2783.07
现在我想添加fp +分期付款+ 2783.07
答案 0 :(得分:0)
SELECT Sum( CASE WHEN paytype IN ('fp', 'installment') THEN b.principal ELSE 0
END )
+ (SELECT Sum(a.credit)
FROM [TFINANCE].[dbo].[fin_tbl_crjsundries] a
INNER JOIN fin_tbl_crjlot b
ON a.headuid = b.headuid
INNER JOIN fin_tbl_crjhead c
ON c.uid = b.headuid
WHERE a.acctcode = '100 200-02'
AND a.brcode = '010'
AND c.ordate BETWEEN '2018-12-01' AND '2018-12-31')
FROM (SELECT b.paytype,
Cast(Sum(b.principal) AS MONEY) AS 'Cash Sales'
FROM fin_tbl_crjhead a
INNER JOIN fin_tbl_crjlot b
ON a.uid = b.headuid
LEFT JOIN fin_tbl_crjsundries c
ON b.headuid = c.uid
WHERE a.ordate BETWEEN '2018-12-01' AND '2018-12-31'
AND a.brcode = '010'
GROUP BY b.paytype
ORDER BY b.paytype ASC)tbl1
答案 1 :(得分:0)
//您可以尝试这个。
SELECT b.paytype, CONVERT(nvarchar, CAST(SUM(b.principal) AS money), 1) as Cash_sale
into #tblfirstsum FROM fin_tbl_crjhead a
INNER JOIN fin_tbl_crjlot b
ON a.uid = b.headuid
left JOIN fin_tbl_crjsundries c ON b.headuid = c.uid
WHERE a.ordate BETWEEN '2018-12-01' AND '2018-12-31'
AND a.brcode = '010'
GROUP BY b.paytype
ORDER BY b.paytype ASC
//第二个查询
SELECT SUM(a.credit) as credit
into #tblsecondSum FROM [TFINANCE].[dbo].[fin_tbl_crjsundries] a INNER JOIN fin_tbl_crjlot b
on a.headuid = b.headuid INNER JOIN fin_tbl_crjhead c ON c.uid = b.headuid
where a.acctcode = '100 200-02' and a.brcode = '010' and c.ordate
between '2018-12-01' and '2018-12-31'
Create table #tblFinal(paytype nvarchar(100),Total_Sum double(18,6))
insert into #tblFinal (paytype,Total_Sum)
select paytype,Cash_sale from #tblfirstsum where paytype='fp' union all
select paytype,Cash_sale from #tblfirstsum where paytype='installment' union all
select 'Credit Amount',credit from #tblsecondSum
select paytype,Total_Sum from #tblFinal
select Total=Sum(Total_Sum) from #tblFinal
答案 2 :(得分:0)
您可以尝试从子查询中使用UNION和SUM,如下所示。
SELECT SUM(COALESCE([Cash Sales],0)) FROM
(
SELECT b.paytype AS paytype, CONVERT(nvarchar, CAST(SUM(b.principal) AS money), 1)
as [Cash Sales]
FROM fin_tbl_crjhead a
INNER JOIN fin_tbl_crjlot b
ON a.uid = b.headuid
left JOIN fin_tbl_crjsundries c ON b.headuid = c.uid
WHERE a.ordate BETWEEN '2018-12-01' AND '2018-12-31'
AND a.brcode = '010'
GROUP BY b.paytype
UNION
SELECT 'credit' AS paytype, SUM(a.credit) AS [Cash Sales]
FROM [TFINANCE].[dbo].[fin_tbl_crjsundries] a INNER JOIN fin_tbl_crjlot b
on a.headuid = b.headuid INNER JOIN fin_tbl_crjhead c ON c.uid = b.headuid
where a.acctcode = '100 200-02' and a.brcode = '010' and c.ordate
between '2018-12-01' and '2018-12-31') S
WHERE S.paytype IN ('fp', 'installment', 'credit')