My SQL, contains five subquery, TotalAmount, TotalPaid sort will lead to errors, PenaltyInterest, PenaltyPaid, PenaltyChange does not lead to,
Confirm that sort field no repeat, use the Id will be same out-of-order,
SQL is as follows:
SELECT *
The FROM (SELECT ROW_NUMBER () OVER (ORDER BY bill. AddTime DESC) rn,
Bill AddTime AS BillAddTime,
T. *,
(SELECT ISNULL (SUM (pd) Amount), 0)
The FROM Tengen_PenaltyDetails pd
WHERE pd. Marks=0
AND pd. BillId=bill. Id
AND pd. The Category=0) AS PenaltyInterest,
(SELECT ISNULL (SUM (pd) Amount), 0)
The FROM Tengen_PenaltyDetails pd
WHERE pd. Marks=0
AND pd. BillId=bill. Id
AND pd. The Category=1) AS PenaltyPaid,
(SELECT ISNULL (SUM (pd) Amount), 0)
The FROM Tengen_PenaltyDetails pd
WHERE pd. Marks=0
AND pd. BillId=bill. Id
AS PenaltyChange AND pd. The Category=2),
(SELECT SUM (RPDS. TotalAmount)
The FROM Tengen_RepaymentPlanDetails RPDS
WHERE RPDS. Marks=0
AND RPD. PlanId=rp. Id) AS TotalAmount,
(SELECT ISNULL (SUM (rr) Amount), 0)
The FROM Tengen_RepaymentRecords rr
WHERE rr. Marks=0
AND rr. BillId=bill. Id) AS TotalPaid
The FROM Company_Bills t
LEFT the JOIN Company_Corps corp ON corp., Id=tc orpId
LEFT the JOIN Tengen_Bills bill ON bill. Id=t.B illId
LEFT the JOIN Tenant_Customers cus ON cus. Id=bill. The CustomerId
LEFT the JOIN Tengen_RepaymentPlans rp ON rp. BillId=bill. Id
WHERE t.M arks=0
AND t.A uditStatus=1
AND bill. Marks=0
AND bill. AuditStatus=1
AND t.Com panyId=2
AND t.A uditStatus=1
AND t.L oanDate IS NOT NULL) paged
WHERE an rn & gt;=1
AND rn & lt;=10
The results of figure 1 is a TotalAmount, TotalPaid
Figure 2 is does not include the TotalAmount, TotalPaid results
Could you tell me what reason be cause?