Getting back movie ratings excluding my own - java

Basically, I have a table called ratings with 3 columns FILM, EMAIL, and RATING in mysql.
I can get back the average rating for all the films, but I must then display the ratings
of the movies that I haven't already rated , so I must only display all the films everybody else rated except me.
String email = Main_Login.accounttext.getText().trim();
statement = (PreparedStatement) con
.prepareStatement(""
+ "SELECT FILM,(film_total_ratings/number_of_ratings) as ratings_rating "
+ "FROM( "
+ "SELECT COUNT(*) as number_of_ratings, FILM, "
+ " SUM(RATING) as film_total_ratings "
+ " FROM ratings GROUP BY film "
+ "ORDER BY rating DESC"
+ ") TMP_Film");
result = (ResultSet) statement.executeQuery();
int i = 0;
while (result.next() && i <= 4) {
i++;
// if (result.getString(1).contains(email)) {
System.out.println((i) + ")" + " " + "[Movies You Might Like]"
+ " " + result.getString(1) + " " + "[Rating]" + " "
+ result.getString(2));

You may try to use CASE to add a column indicating that movie has been rated by the respective email or not. This is not tested but you may get the concept:
statement = (PreparedStatement) con
.prepareStatement(""
+ "SELECT FILM,(film_total_ratings/number_of_ratings) as ratings_rating "
+ "FROM( "
+ "SELECT COUNT(*) as number_of_ratings, FILM, "
+ " SUM(RATING) as film_total_ratings, "
+ " SUM(CASE WHEN EMAIL LIKE '%"+email+"%' THEN 1 ELSE 0 END) as rated"
+ " FROM ratings GROUP BY film HAVING rated=0 "
+ "ORDER BY rating DESC"
+ ") TMP_Film");

Related

SQL Query - FindCostumerWithHighestBill

I am try to find the costumer with the highest bill using this sql that I wrote , but apparently I run into this error :
Every derived table must have its own alias
This is what I wrote:
public static void findCustomerWithHighestBill(Connection c) throws SQLException {
String query = "SELECT CustomerID, CustomerName, CITY, sum(sum_all) As 'data'\r\n"
+ "FROM (SELECT *, sum(Price) As 'sum_all'\r\n"
+ " FROM (SELECT *, Customer.NAME AS \"CustomerName\"\r\n"
+ " FROM Transactions\r\n"
+ " INNER JOIN Product ON Product.ID = Transactions.ProductID\r\n"
+ " INNER JOIN Customer ON Customer.ID = Transactions.CustomerID)\r\n"
+ " GROUP BY CustomerID, ProductID\r\n"
+ " ) \r\n"
+ "GROUP BY CustomerID\r\n"
+ "ORDER BY data DESC";
Statement statement = c.createStatement();
ResultSet set = statement.executeQuery(query);
set.next();
System.out.println("CustomerID: " + set.getInt("CustomerID") +
", Name: " + set.getString("CustomerName") + ", City: " +
set.getString("CITY") + ", Bill: " + set.getFloat("data"));
}
Can please somoane help me zith this ?
Coming back ,
It seems that indeed ad most of you says in the comments I should identify my tables first but, apparently I also have miss to avoid the duplicate ID column.
Here goes my new query:
String query = "SELECT CID, CustomerName, City, sum(sum_all) As data" +
" FROM (SELECT *, sum(PPROD) As sum_all" +
" FROM (SELECT Product.ID as PID,Customer.ID as CID,Product.Price as PPROD,City, Customer.NAME AS CustomerName" +
" FROM Transactions" +
" INNER JOIN Product ON Product.ID = Transactions.ProductID" +
" INNER JOIN Customer ON Customer.ID = Transactions.CustomerID) As t2" +
" GROUP BY CID, PID" +
" ) As t1" +
" GROUP BY CID" +
" ORDER BY data DESC;";

UNION in Spring data JPA native query two table

I have a requirement where i need to fetch number of record both the table using JPA native UNION query for Pagination
table 1=>txrh_bcaterms
table 2=> txrm_bcaterms
Ex: txrh_bcaterms UNION txrm_bcaterms
In Below code i am trying to fetching the record in both table Using UNION
#Query(nativeQuery = true,value= "SELECT "
+ " customerid, "
+ "finance_type, "
----------
-------etc----
+ "institution, "
+ "limit_groupid, "
+ "tx_booking_location, "
+ "finance_tenor_days, "
+ "margin_type , "
+ "additional_margin_percentage, "
+ "margin_percentage, "
+ "stale_document_condition_proforma_fin, "
+ "customer_name "
+ "from "
+ "txrh_bcaterms "
+ "where "
+ " institution=:institution "
+ "AND tx_booking_location=:txBookingLocation "
+ "OR finance_type=:financeType "
+ "OR customerid=:customerID "
+ "OR limit_groupid=:limitGroupID "
+ "UNION "
+ "SELECT "
+ "customerid, "
+ "finance_type, "
----------
-------etc----
+ "institution, "
+ "limit_groupid, "
+ "tx_booking_location, "
+" finance_tenor_days, "
+ "margin_type, "
+ "additional_margin_percentage, "
+ "margin_percentage, "
+ "stale_document_condition_proforma_fin, "
+ "customer_name "
+ "from txrm_bcaterms "
"
+ " institution=:institution "
+ "AND tx_booking_location=:txBookingLocation "
+ "OR finance_type=:financeType "
+ "OR customerid=:customerID "
+ "OR limit_groupid=:limitGroupID "
)
Page<Map<String,Object>> historyAndMaster(#Param("institution") String institution, #Param("txBookingLocation") String txBookingLocation,
#Param("financeType") String financeType, #Param("customerID") String customerID, #Param("limitGroupID") String limitGroupID, Pageable page);
NOTE:Actually stepid is the primary key txrh_bcaterms table and stepid is not there txrm_bcaterms table
ERROR
-------
in ui i passed institution =BC/AC/EC then UNION query able to execute and getting data
but if i passed SC then getting below exception
Caused by ERROR: each UNION query must have the same number of columns
Position: 581
at org.axonframework.axonserver.connector.ErrorCode.lambda$static$15(ErrorCode.java:107)
... 9 common frames omitted
=>If there is no same number of columns then how it is worked BC/AC/EC
I am new the Spring Data JPA Could you please help me to resolve this issue
Any other better suggestion welcome!

JDBC query returning zero when using simple arithmetic operations and alias even though the data in table is not zero

I am building a web portal through android and a query i am running through JDBC drivers is returning 0 where data should not be zero.
This is the query:
ResultSet set = statement.executeQuery("select it.itcod, it.itnam, it.packn, it.tradp, " +
"sum(nvl(itd.slbox,0) - nvl(itd.srbox,0) - nvl(itd.brbox,0) - nvl(itd.gsbox,0)) as sbox, " +
"sum(nvl(itd.slbbx,0) - nvl(itd.srbbx,0) - nvl(itd.brbbx,0) - nvl(itd.gsbbx,0)) as sbbx, " +
"SUM(NVL(itd.PRBOX,0) - NVL(itd.RPBOX,0) - NVL(itd.TRBOX,0)) as pbox, " +
"SUM(NVL(itd.PRBBX,0) - NVL(itd.RPBBX,0) - NVL(itd.TRBBX,0)) as pbbx " +
"from items it " +
"LEFT join item_daily itd " +
"on (it.cocod = itd.cocod " +
"and it.itcod = itd.itcod " +
"and ITD.ddate between " + fdate + " and " + tdate + ")" +
"WHERE IT.COCOD = " + COCOD +
"AND IT.DCODE = " + DCODE +
"AND NVL(IT.FREZE,'N')!='Y' " +
"group by it.cocod, it.itcod, it.itnam, it.packn, " +
" it.tradp, it.pkqty, it.dcode, it.freze, " +
" it.ishow, it.sltax, it.dcont, it.mcode, " +
" it.nwcod " +
"order by itnam ");
I have tried using resultsetmetadata but that does not work either.
you should be careful to this line
+ fdate + " and " + tdate
because you should use like this:
" .. to_date('"+fdate+"','***your_date_format') and to_date('"+fdate+"','***your_date_format')"

Sql query that outputs to ONE Joptionpane

String sql = "SELECT TOP 10 id, Trailer, Block, Location, Day, SetTime, Comment FROM TrailerLocation"
+ " ORDER BY id DESC";
rs = st.executeQuery(sql);
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
String trailer = rs.getString("Trailer");
String block = rs.getString("Block");
String location = rs.getString("Location");
String date = rs.getString("Day");
String comment = rs.getString("Comment");
//Display values
JOptionPane.showMessageDialog(null,
"ID: " + id
+ ", Trailer: " + trailer
+ ", Block: " + block
+ ", Location: " + location
+ ", Date & Time: " + date
+ ", Comment: " + comment);
}
I want the Joptionpane to only display once with all the data not ten times.
You could move the JOptionPane.showMessageDialog(null, ... ) after the while loop.
And, in the while loop, add the informations you want in one unique string which you will display in you JOptionPane.
rs = st.executeQuery(sql);
StringBuilder str = new StringBuilder();
while(rs.next()){
//Retrieve by column name
str.append("ID: " + rs.getInt("id"));
str.append(", Trailer: " + rs.getString("Trailer"));
str.append(", Block: " + rs.getString("Block"));
str.append(", Location: " + rs.getString("Location"));
str.append(", Date: " + rs.getString("Day"));
str.append(", Comment: " + rs.getString("Comment"));
//new line
str.append("\n");
}
JOptionPane.showMessageDialog(null,str.toString());

How to Compare date in Prepared statement

I have Sybase Statement which is mentioned below :
select s.airbill_nbr, s.orig, s.dest,p.swa_acct_nbr, s.prod_type, s.outbnd_date, s.shppr_name, s.consgn_name,s.shipping_chrg, s.misc_chrg, s.decl_val_chrg, s.tax_amt, s.fuel_chrg, s.scrty_chrg, s.tot_chrgs, s.tot_pcs, s.tot_chrgbl_wt, s.tot_actl_wt from shipment s, shipment_outbnd_stn o, payment p
where s.sa_shpmt_nbr = o.sa_shpmt_nbr
and o.sa_shpmt_nbr = p.sa_shpmt_nbr
and o.outbnd_date=convert(date,'01/29/2015',101)
and s.outbnd_empl_id is not null
and p.fop_cd = 'SW'
and p.swa_acct_nbr =40584
and s.void_flag is null
I have to create a prepared statement for the above code in Java and I have created the below One :
String variable1="29/01/2015";
psRetrieveAwbData = cattsDbc.prepareStatement( "select s.airbill_nbr, s.orig, s.dest, " +
"p.swa_acct_nbr, s.prod_type, s.outbnd_date, s.shppr_name, s.consgn_name, " +
"s.shipping_chrg, s.misc_chrg, s.decl_val_chrg, s.tax_amt, s.fuel_chrg, " +
"s.scrty_chrg, s.tot_chrgs, s.tot_pcs, s.tot_chrgbl_wt, s.tot_actl_wt " +
"from shipment s, shipment_outbnd_stn o, payment p " +
"where s.sa_shpmt_nbr = o.sa_shpmt_nbr " +
"and o.sa_shpmt_nbr = p.sa_shpmt_nbr " +
"and o.outbnd_date = '"+ variable1 +"'" +
"and s.outbnd_empl_id is not null " +
"and p.fop_cd = 'SW' " +
"and p.swa_acct_nbr = " + iAccount +
"and s.void_flag is null" );
but I dont know how to put date in date Format because the way I have put it is not working by taking the variable1, as variable1 It is taking as String and not finding out the Solution.Could you please tell me how to create the prepared statement where I can directly put date or put the date by storing it in some variable.
PreparedStatement pstmt = con.prepareStatement("select s.airbill_nbr, s.orig, s.dest, " +
"p.swa_acct_nbr, s.prod_type, s.outbnd_date, s.shppr_name, s.consgn_name, " +
"s.shipping_chrg, s.misc_chrg, s.decl_val_chrg, s.tax_amt, s.fuel_chrg, " +
"s.scrty_chrg, s.tot_chrgs, s.tot_pcs, s.tot_chrgbl_wt, s.tot_actl_wt " +
"from shipment s, shipment_outbnd_stn o, payment p " +
"where s.sa_shpmt_nbr = o.sa_shpmt_nbr " +
"and o.sa_shpmt_nbr = p.sa_shpmt_nbr " +
"and o.outbnd_date = ?" +
"and s.outbnd_empl_id is not null " +
"and p.fop_cd = 'SW' " +
"and p.swa_acct_nbr = " + iAccount +
"and s.void_flag is null" );");
pstmt.setDate (dt);

Categories