The code below is repository
#Query
(
value = "select min(id) as id, coalesce (sum(case when points > 0 then points end), 0) as points, userid" +
" from books" +
" where userid = ?1" +
" group by userid", nativeQuery = true
)
JamInfo findPlus(Long userid);
#Query
(
value = "select min(id) as id, coalesce(sum(case when points < 0 then points*-1 end),0)as points, userid" +
" from books" +
" where userid = ?1" +
" group by userid", nativeQuery = true
)
JamInfo findMinus(Long userid);
The code below is controller
JamInfo jamplus = jamInfoRepository.findPlus(id);
JamInfo jamminus = jamInfoRepository.findMinus(id);
model.addAttribute("jamplus", jamplus);
model.addAttribute("jamminus", jamminus);
If you print it like this, it will be unified with the value of jamplus written first. How can the two values be output differently?
I'm trying to perform calculations on select columns using aliases and a grouping by. Query is below(problem on line before the from):
select r.res_id,
r.arrive_date,
r.depart_date,
r.res_type,
if(DATEDIFF(r.depart_date, r.arrive_date) >29, 'LT', 'ST') as 'StayType',
SUM(r.rent + r.fee_arr_early + r.fee_dep_late + r.fee_peace_waiver + r.fee_pool + r.city_tax + r.fee_cleaning + r.fee_pet + r.fee_tshirt + r.fee_misc + r.fee_non_tax + r.fee_processing + r.fee_travel_ins + r.fee_event + r.fee_cancel) as 'folioTotal',
coalesce((select SUM(g.amount) from guest_payments as g where g.resId = r.res_id and charge_type = 'charge' and approved = 'Y'),0) as 'payments',
coalesce((select SUM(g.amount) from guest_payments as g where g.resId = r.res_id and charge_type = 'credit' and approved = 'Y'),0) as 'credits',
(SUM('folioTotal') - SUM('payments') + SUM('credits')) as 'folioBalance'
from reservations as r
join guest_payments as g
on r.res_id = g.resId
group by r.res_id
I've tried putting this inside another sum with the same outcome.
I was being stupid, I was referencing the aliases inside single ticks which is why it wasn't calculating. Solution:
select r.res_id,
r.arrive_date,
r.depart_date,
r.res_type,
g.entry_date,
if(DATEDIFF(r.depart_date, r.arrive_date) >29, 'LT', 'ST') as 'StayType',
(select SUM(r.rent + r.fee_arr_early + r.fee_dep_late + r.fee_peace_waiver + r.fee_pool + r.city_tax + r.fee_cleaning + r.fee_pet + r.fee_tshirt + r.fee_misc + r.fee_non_tax + r.fee_processing + r.fee_travel_ins + r.fee_event + r.fee_cancel) from reservations as r where r.res_id = g.resId) as 'folioTotal',
coalesce((select SUM(g.amount) from guest_payments as g where g.resId = r.res_id and charge_type = 'charge' ),0) as 'payments',
coalesce((select SUM(g.amount)* -1 from guest_payments as g where g.resId = r.res_id and charge_type = 'credit' and approved = 'Y'),0) as 'credits',
(select folioTotal - payments + credits)
from reservations as r
join guest_payments as g
on r.res_id = g.resId
group by r.res_id
I know the values for aw_id, ad_id and grp_id (e.g., aw_id = 5, ad_id = 46 and grp_id =2).
I want to display all the at_cub_details where:
at_cub_details.grp_id = 2 AND
at_cub_details.cd_id = at_cub_awards.cd_id AND
at_cub_awards.aw_id = 5 AND
at_cub_awards.ca_awarded_date IS NULL
OR at_cub_details.cd_id = at_cub_awards.cd_id AND
at_cub_awards.aw_id = 5 does not exist
Where at_cub_details.cd_id = at_cub_awards.cd_id AND
at_cub_awards.aw_id = 5 AND
at_cub_awards.ca_awarded_date = NULL does exist **then**
at_cub_awards.ca_id = at_cub_award_date.ca_id
AND at_cub_award_date.ad_id = 46
AND at_cub_award_date.cad_task_completion_date IS NULL OR
at_cub_awards.ca_id = at_cub_award_date.ca_id AND
at_cub_award_date.ad_id = 46 does not exist
I have have tried all manner of RIGHT and LEFT joins on this without any luck.
The solution was:
String selectQry = ("SELECT * from ( " +
"SELECT DISTINCT at_cub_details.cd_id as cdid, " +
"at_cub_details.grp_id as grpid, " +
"at_cub_details.cd_surname as surname, " +
"at_cub_details.cd_first_name as firstName, " +
"at_cub_details.cd_dob as dob, " +
"at_cub_details.cd_photograph as photograph, " +
"at_cub_details.cd_archived as archived, " +
"at_cub_details.cd_scout_no as scoutNo " +
"FROM at_account_group, at_cub_details " +
"LEFT JOIN at_cub_awards ON (at_cub_details.cd_id = at_cub_awards.cd_id AND at_cub_awards.aw_id = ?) " +
"WHERE at_cub_awards.cd_id IS NULL " +
"AND at_cub_details.grp_id = at_account_group.grp_id " +
"AND at_account_group.acc_id = ? " +
"UNION " +
"SELECT DISTINCT at_cub_details.cd_id as cdid, " +
"at_cub_details.grp_id as grpid, " +
"at_cub_details.cd_surname as surname, " +
"at_cub_details.cd_first_name as firstName, " +
"at_cub_details.cd_dob as dob, " +
"at_cub_details.cd_photograph as photograph, " +
"at_cub_details.cd_archived as archived, " +
"at_cub_details.cd_scout_no as scoutNo " +
"FROM at_account_group, at_cub_details, at_cub_awards, at_cub_award_date " +
"WHERE at_cub_details.grp_id = at_account_group.grp_id " +
"AND at_account_group.acc_id = ? " +
"AND at_cub_awards.cd_id = at_cub_details.cd_id " +
"AND (at_cub_awards.aw_id = ? AND at_cub_awards.ca_awarded_date IS NULL) " +
"AND (at_cub_awards.ca_id = at_cub_award_date. ca_id " +
"AND at_cub_award_date.cad_task_completion_date IS NULL " +
"AND at_cub_award_date.ad_id = ?) " +
"UNION " +
"SELECT DISTINCT at_cub_details.cd_id as cdid, " +
"at_cub_details.grp_id as grpid, " +
"at_cub_details.cd_surname as surname, " +
"at_cub_details.cd_first_name as firstName, " +
"at_cub_details.cd_dob as dob, " +
"at_cub_details.cd_photograph as photograph, " +
"at_cub_details.cd_archived as archived, " +
"at_cub_details.cd_scout_no as scoutNo " +
"FROM at_account_group, at_cub_details, at_cub_awards " +
"LEFT JOIN at_cub_award_date ON (at_cub_awards.ca_id = at_cub_award_date.ca_id) " +
"WHERE at_cub_award_date.ca_id IS NULL " +
"AND at_cub_awards.ca_awarded_date IS NULL AND at_cub_awards.aw_id = ? " +
"AND at_cub_details.grp_id = at_account_group.grp_id " +
"AND at_account_group.acc_id = ? " +
"AND at_cub_awards.cd_id = at_cub_details.cd_id " +
" ) a " +
"ORDER BY surname, firstName;");
I am trying to create a new chart in PrimeFaces 3.5RC1 but for some reason when all the data have been collected and populated in the chart it does not display on the screen. I know there is nothing wrong with the Java code because it is working but the chart is blank.
Please see java and xhtml code below:
Java Bean:
public void searchMonthTotals() {
String sql = null;
if(retailID.equals("999") && billerID.equals("999")){
sql = "WITH CTE AS("
+ " SELECT Value,DAY(IntervalStartTime) AS DAY,MONTH(IntervalStartTime) AS MONTH,YEAR(IntervalStartTime) AS YEAR, "
+ " CASE WHEN MONTH(IntervalStartTime) = MONTH(GETDATE()) THEN 'Current' ELSE 'Previous' END AS Type "
+ " FROM tblpay#Stat s (nolock) "
+ " LEFT OUTER JOIN tblPay#StatConfig c (nolock) ON s.configId = c.id "
+ " LEFT OUTER JOIN tblpay#StatSlot l (nolock) ON s.slotId = l.id "
+ " WHERE IntervalStartTime >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) "
+ " AND IntervalEndTime <= DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) "
+ " AND s.configId = " + configID
+ " GROUP BY IntervalStartTime,value) "
+ " SELECT SUM(Value) AS Value, DAY,MONTH,YEAR,Type "
+ " FROM CTE "
+ " GROUP BY DAY,Type,MONTH,YEAR "
+ " ORDER BY Type ";
}else if(billerID.equals("999")){
sql = "WITH CTE AS("
+ " SELECT cm.Name AS Network,Value,DAY(IntervalStartTime) AS DAY,MONTH(IntervalStartTime) AS MONTH,YEAR(IntervalStartTime) AS YEAR, "
+ " CASE WHEN MONTH(IntervalStartTime) = MONTH(GETDATE()) THEN 'Current' ELSE 'Previous' END AS Type "
+ " FROM tblpay#Stat s (nolock) "
+ " LEFT OUTER JOIN tblPay#StatConfig c (nolock) ON s.configId = c.id "
+ " LEFT OUTER JOIN tblpay#StatSlot l (nolock) ON s.slotId = l.id "
+ " LEFT OUTER JOIN tblpay#company cm (nolock) ON s.networkId = cm.RecID "
+ " WHERE IntervalStartTime >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) "
+ " AND IntervalEndTime <= DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) "
+ " AND s.configId = " + configID
+ " AND s.networkId = " + retailID
+ " GROUP BY cm.Name,IntervalStartTime,value) "
+ " SELECT Network,SUM(Value) AS Value, DAY,MONTH,YEAR,Type "
+ " FROM CTE "
+ " GROUP BY Network,DAY,Type,MONTH,YEAR "
+ " ORDER BY Network,Type ";
}else if(retailID.equals("999")){
sql = "WITH CTE AS("
+ " SELECT cm.Name AS Biller,Value,DAY(IntervalStartTime) AS DAY,MONTH(IntervalStartTime) AS MONTH,YEAR(IntervalStartTime) AS YEAR, "
+ " CASE WHEN MONTH(IntervalStartTime) = MONTH(GETDATE()) THEN 'Current' ELSE 'Previous' END AS Type "
+ " FROM tblpay#Stat s (nolock) "
+ " LEFT OUTER JOIN tblPay#StatConfig c (nolock) ON s.configId = c.id "
+ " LEFT OUTER JOIN tblpay#StatSlot l (nolock) ON s.slotId = l.id "
+ " LEFT OUTER JOIN tblpay#company cm (nolock) ON s.issuerId = cm.RecID "
+ " WHERE IntervalStartTime >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) "
+ " AND IntervalEndTime <= DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) "
+ " AND s.configId = " + configID
+ " AND s.networkId = " + retailID
+ " GROUP BY cm.Name,IntervalStartTime,value) "
+ " SELECT Biller,SUM(Value) AS Value, DAY,MONTH,YEAR,Type "
+ " FROM CTE "
+ " GROUP BY Biller,DAY,Type,MONTH,YEAR "
+ " ORDER BY Biller,Type ";
}else{
sql = " WITH CTE AS( "
+ " SELECT cm.Name AS Network,co.Name AS Biller,DAY(IntervalStartTime) AS DAY,MONTH(IntervalStartTime) AS MONTH,YEAR(IntervalStartTime) AS YEAR, "
+ " CASE WHEN MONTH(IntervalStartTime) = MONTH(GETDATE()) THEN 'Current' ELSE 'Previous' END AS Type "
+ " FROM tblpay#Stat s (nolock) "
+ " LEFT OUTER JOIN tblPay#StatConfig c (nolock) ON s.configId = c.id "
+ " LEFT OUTER JOIN tblpay#StatSlot l (nolock) ON s.slotId = l.id "
+ " LEFT OUTER JOIN tblpay#company cm (nolock) ON s.networkId = cm.RecID "
+ " LEFT OUTER JOIN tblpay#company co (nolock) ON s.issuerId = co.RecID "
+ " WHERE IntervalStartTime >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) "
+ " AND IntervalEndTime <= DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) "
+ " AND s.configId = " + configID
+ " AND s.networkId = " + retailID
+ " AND s.issuerId = " + billerID
+ " GROUP BY IntervalStartTime,cm.Name,value,co.Name) "
+ " SELECT Network,Biller, SUM(Value) AS Value,DAY,MONTH,YEAR,Type "
+ " FROM CTE "
+ " GROUP BY Network,DAY,MONTH,YEAR,Type,Biller "
+ " ORDER BY Network,Type ";
}
try {
Connection con = db.getDBConnection(DatabaseTypes.TRANSACTION_DATABASE);
ResultSet rs = DatabaseHandler.executeQuery(con, sql);
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
maxTransactions = 0;
maxDays = 31;
boolean isEmpty = !rs.first();
if (!isEmpty) {
monthTotalsPrevious = new ArrayList<MonthlyGraphs>();
monthTotalsCurrent = new ArrayList<MonthlyGraphs>();
}
rs.beforeFirst();
while (rs.next()) {
MonthlyGraphs t = new MonthlyGraphs();
Calendar cal2 = Calendar.getInstance();
cal2.set(rs.getInt("YEAR"), rs.getInt("MONTH") - 1, rs.getInt("DAY"));
t.setDate(dateFormat.format(cal2.getTime()));
t.setTotalTransactions(rs.getInt("Value"));
t.setDay(rs.getInt("DAY"));
if (rs.getString("Type").equals("Current")) {
monthTotalsCurrent.add(t);
} else {
monthTotalsPrevious.add(t);
}
if (t.getTotalTransactions() > maxTransactions) {
maxTransactions = t.getTotalTransactions();
}
}
rs.close();
con.close();
createCategoryModel();
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "An error occurred.", e.getMessage()));
}
}
private void createCategoryModel() {
chartModel = new CartesianChartModel();
ChartSeries dailyTotalsPrevious = new ChartSeries();
dailyTotalsPrevious.setLabel("Previous");
for (int i = 0; i < monthTotalsPrevious.size(); i++) {
dailyTotalsPrevious.set(monthTotalsPrevious.get(i).getDay(), monthTotalsPrevious.get(i).getTotalTransactions());
}
chartModel.addSeries(dailyTotalsPrevious);
ChartSeries dailyTotalsCurrent = new ChartSeries();
dailyTotalsCurrent.setLabel("Current");
for (int i = 0; i < monthTotalsCurrent.size(); i++) {
dailyTotalsCurrent.set(monthTotalsCurrent.get(i).getDay(), monthTotalsCurrent.get(i).getTotalTransactions());
}
chartModel.addSeries(dailyTotalsCurrent);
}
xhtml page:
<p:toolbar id ="graphtool">
<p:toolbarGroup align="left">
<h:panelGrid columns="6" cellpadding="5" >
<p:selectOneMenu id="billIssuerSelect" value="#{graphsBean.billerID}" style="width:250px" >
<f:selectItems value="#{generalBean.billIssuerList}" var="b" itemValue="#{b.billIssuerId}" itemLabel="#{b.billIssuerName}"/>
</p:selectOneMenu>
<p:selectOneMenu id="retailerSelect" value="#{graphsBean.retailID}" style="width: 250px" >
<f:selectItems value="#{generalBean.retailerList}" var="r" itemValue="#{r.retailerId}" itemLabel="#{r.retailerName}" />
</p:selectOneMenu>
<p:selectOneMenu id="configSelect" value="#{graphsBean.configID}" style="width: 250px" >
<f:selectItems value="#{graphsBean.configList}" var="r" itemValue="#{r.ID}" itemLabel="#{r.configName}" />
</p:selectOneMenu>
<p:commandButton id="refreshButton"
value="Generate"
icon="ui-icon-refresh"
update="graph"
ajax="true"
global="false"
actionListener="#{graphsBean.searchMonthTotals()}"/>
</h:panelGrid>
</p:toolbarGroup>
<p:toolbarGroup align="right">
<h:panelGrid columns="2" cellpadding="8" >
<h:commandLink>
<p:graphicImage style="border: none" value="../resources/excel24.png" />
<p:dataExporter type="xls" target="graph" fileName="Month Graph" />
</h:commandLink>
</h:panelGrid>
</p:toolbarGroup>
</p:toolbar>
<p:spacer width="10" height="10" />
<p:panel header="Month Graph">
<p:lineChart id="graph" value="#{graphsBean.chartModel}" style="width:500px;height:300px"
minY="0" maxY="#{graphsBean.maxTransactions}"
minX="1" maxX="#{graphsBean.maxDays}"
showMarkers="false"
legendPosition="n"
extender="customExtender"/>
</p:panel>
If anyone can guide me in the correct direction that would be great.
Thanks
I resolved the issue, there was a Javascript code in the xhtml page that needed to be included to generate the graph.
This is what I've tried so far:
SELECT M.ID, M.NAME, M.SEQ
FROM M JOIN MCA
ON M.ID = MCA.M_ID
JOIN MC
ON MCA.CAT_ID = MC._ID
WHERE MCA.CAT_ID = "
+ inputCategoryID
+ " AND (M.NAME LIKE '"
+ inputSearch.replace("'", "''")
+ "%' OR M.NAME LIKE '%"
+ inputSearch.replace("'", "''")
+ "%' ) AND M.START_DATE <= CURRENT_DATE " +
AND M.EXPIRY_DATE >= CURRENT_DATE " +
ORDER BY M.SEQ DESC, M.NAME ASC
SAMPLE RESULTS:
ID NAME SEQ
1 Tes1 0
2 Arj2 0
3 Jfa3 1
4 Pof4 0
DESIRED RESULTS:
ID NAME SEQ
3 Jfa3 1
2 Arj2 0
4 Pof4 0
1 Tes1 0
But if the same SEQ value (i.e., 0, 0), it should display Name alphabetically. Any ideas how to do that? Thanks.