我有3个查询:
// (1)
String sql = "SELECT tblClientInfo.ClientID, tblrefmarket.MarketDesc, tblclientinfo.LastName, tblledger.LoanAmount, "
+ "tblledger.DateStarted, tblledger.DailyPay, tblledger.Expiry FROM tblclientinfo Inner Join tblbusinessinfo ON tblbusinessinfo.ClientID = tblclientinfo.ClientID "
+ "Inner Join tblrefmarket ON tblbusinessinfo.MarketID = tblrefmarket.MarketID "
+ "Inner Join tblledger ON tblledger.ClientID = tblclientinfo.ClientID where MarketDesc = ?";
// (2)
String sumSQL = "SELECT ClientID, sum(tblloanpayment.AmountPaid) as sum FROM tblloanpayment where tblloanpayment.ClientID= ? ";
// (3)
String balSQL = "SELECT (SELECT tblLedger.LoanAmount from tblLedger WHERE tblLedger.ClientID = ?) - (SELECT SUM(tblLoanPayment.AmountPaid) "
+ "FROM tblLoanPayment WHERE tblLoanPayment.ClientID = ?) as balance FROM dual; ";
我已执行此3个查询以在jTable上显示信息。它很成功。
现在我的问题是我使用JasperReports生成报告(或打印)。
我只能显示第一个查询,因为它在数据库中。而查询2和3则不是。它们只是对查询1中付款的计算。
我如何加入这个以便能够显示所有必要的信息?
这是我的代码:
private void cmdPrintActionPerformed(java.awt.event.ActionEvent evt) {
int row = tableMarket.getSelectedRow();
try {
JasperDesign jasperDesign = JRXmlLoader.load("notes receivables.jrxml");
String sql = "SELECT tblClientInfo.ClientID, tblrefmarket.MarketDesc, tblclientinfo.LastName, tblledger.LoanAmount, "
+ "tblledger.DateStarted, tblledger.DailyPay, tblledger.Expiry FROM tblclientinfo Inner Join tblbusinessinfo ON tblbusinessinfo.ClientID = tblclientinfo.ClientID "
+ "Inner Join tblrefmarket ON tblbusinessinfo.MarketID = tblrefmarket.MarketID "
+ "Inner Join tblledger ON tblledger.ClientID = tblclientinfo.ClientID where MarketDesc = '" + tableMarket.getModel().getValueAt(row, 0).toString() + "'";
JRDesignQuery newQuery = new JRDesignQuery();
newQuery.setText(sql);
jasperDesign.setQuery(newQuery);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
JasperViewer.viewReport(jasperPrint);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
}
此代码仅显示第一个查询。
答案 0 :(得分:1)
您已在第一个查询中选择tblLedger.LoanAmount
,因此第二个和第三个查询所需的唯一附加信息是sum(tblloanpayment.AmountPaid)
。尝试:
SELECT c.ClientID,
m.MarketDesc,
c.LastName,
l.LoanAmount,
l.DateStarted,
l.DailyPay,
l.Expiry,
s.sumPaid,
l.LoanAmount - s.sumPaid as balance
FROM tblclientinfo c
Inner Join tblbusinessinfo b ON b.ClientID = c.ClientID
Inner Join tblrefmarket m ON b.MarketID = m.MarketID
Inner Join tblledger l ON l.ClientID = c.ClientID
left join (SELECT ClientID, sum(AmountPaid) as sumPaid
FROM tblloanpayment group by ClientID) s on c.ClientID = s.ClientID
where m.MarketDesc = ?
答案 1 :(得分:0)
在第二次和第三次查询中删除客户端ID的限制。将它们设置为子选择(即将它们放在括号中)并创建一个大的选择,将所有三个子选择连接到客户端ID上。
修改强>
因此,如果三个查询是query1,query2,query3,那么你最终会得到
select ... from (query1) baseSql
join (query2) sumSql on baseSql.clientId = sumSql.clientId
join (query3) balSql on baseSql.clientId = balSql.clientId
如果left join
或join
中缺少baseSql
中的行,则可能需要使用sumSql
代替balSql
。