java - Fetch results from two queries in single list in DAO class -


i trying fetch 2 different select queries in dao class single list in java.

public list<sbcdocumentdetailsvo> getsbcdetails() throws dataaccessexception, sqlexception{ list<sbcdocumentdetailsvo> sbcdoclist = new arraylist<sbcdocumentdetailsvo>();   mapsqlparametersource namedparameter = new mapsqlparametersource();  //using namedparameterjdbctemplate querying sbcdoclist = this.getnamedparameterjdbctemplate().query(         sbcdetailsquery, namedparameter,         new sbcdetailsmapper()); return sbcdoclist; 

}

my select queries are

select state, marketid, count(marketid) batchreport group state, marketid   select sum(case marketid when 'in' 1 else 0 end) totalcountind  ,sum(case marketid when 'gr' 1 else 0 end) totalcountgrp batchreport 

could 1 me 2 queries in single list.

from point of view have 2 possibilities:


collection merge

the idea execute both queries , add both results single list:

... list result = new arraylist(); result.addall(firstquery()); result.addall(secondquery()); ... 

sql union

for sql union have 1 query used retrieve entire result list. can check how here

in case couldn't this:

select     state,     marketid,     count(marketid)      batchreport  group     state,     marketid  union select     'total',     sum(case marketid when 'in' 1 else 0 end) totalcountind,    sum(case marketid when 'gr' 1 else 0 end) totalcountgrp  batchreport 

i invest time in sql union solution since solves problem quering database once.

you can see demo here: http://sqlfiddle.com/#!2/e5285/1


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -