SQL Inner Join query returns no results - sql

I'm refactoring a Java program written by someone else a couple of years ago, can't get in contact with them to find out anything about the SQL / database, but this query is not working (not returning any results when having two queries separately does). I know it's annoying to ask without more info, but I haven't really got much choice at the moment.
"SELECT " + CLMHDR + ".POLBRC, "
+ CLMHDR + ".POLTYC, " + CLMHDR + ".POLNOC," + CLMHDR + ".CLTKYC, "
+ POLHDR + ".INCPTP FROM "+ CLMHDR +
"INNER JOIN " + POLHDR + " ON " + CLMHDR + ".CLTKYC = " + POLHDR+ ".CLTKYP"
+ " WHERE POLNOC = "+ polnocSearch
+ " AND POLBRC = '" + polbrcSearch + "'"
+ " AND POLTYC = '" + poltycSearch + "'"
+ " AND DATRPC <= " + claimDate
+ " GROUP BY POLBRC, POLTYC, POLNOC, CLTKYC"
The tables CMLHDR and POLHDR do contain the columns it is referencing, and CLTKYC and CLTKYP are keys in each table. Sorry about the horrible names, we're stuck with RPG as well.
Edit:
What does work is this:
"SELECT POLBRC, POLTYC, POLNOC, CLTKYC FROM "+ CLMHDR
+ " WHERE POLNOC = "+ polnocSearch
+ " AND POLBRC = '" + polbrcSeach + "'"
+ " AND POLTYC = '" + poltycSearch + "'"
+ " AND DATRPC <= " + claimDate
+ " GROUP BY POLBRC, POLTYC, POLNOC, CLTKYC"
followed by this:
"SELECT INCPTP, TRMTHP FROM "+ POLHDR + " WHERE POLNOP = "+ polnocSearch
+ " AND POLBRP = '"+ polbrcSearch+ "' AND POLTYP = '"+ poltycSearch + "'"
but I'd really prefer all the data to be returned at once.

There is a space missing between the FROM and the INNER JOIN clause:
FROM "+ CLMHDR +
"INNER JOIN
It is should be this:
FROM "+ CLMHDR +
" INNER JOIN

In addition to the inner join problem, you have an issue with the group by. It should have INCPTP. In any database except for MySQL, this will generate an error.
By the way, it would be easier to answer your question if it included two things:
The database engine you are using
The resulting query string with the values filled in

Related

Use the same query for multiple tables

I have this two similar queries for different tables, one for loan_offers table and one for special_offers table
for loan_offers
#Query(
value = "SELECT " +
"CAST(o.id AS VARCHAR), o.user_id, " +
"o.partner_key, o.partner_name, " +
"o.originator_key, o.originator_name, " +
"o.type, o.response, " +
"COALESCE(lo.recommendation_score, 0) AS recommendation_score " +
"FROM offers o " +
"INNER JOIN loan_offers lo " +
"ON lo.offer_id = o.id " +
"WHERE o.created_at > EXTRACT(epoch FROM now() - INTERVAL '30' DAY) * 1000 " +
"AND o.user_id = ?1 " +
"ORDER BY recommendation_score DESC",
nativeQuery = true)
List<OffersResponse> fetchLoanOffers(String userId);
for special_offers
#Query(
value = "SELECT " +
"CAST(o.id AS VARCHAR), o.user_id, " +
"o.partner_key, o.partner_name, " +
"o.originator_key, o.originator_name, " +
"o.type, o.response, " +
"COALESCE(so.recommendation_score, 0) AS recommendation_score " +
"FROM offers o " +
"INNER JOIN special_offers so " +
"ON so.offer_id = o.id " +
"WHERE o.created_at > EXTRACT(epoch FROM now() - INTERVAL '30' DAY) * 1000 " +
"AND o.user_id = ?1 " +
"ORDER BY recommendation_score DESC",
nativeQuery = true)
List<OffersResponse> fetchSpecialOffers(String userId);
So my question is how not to write so much similar code, maybe by using UDF or there is a better solution.
Use a variable to declare which table you want and use the variable in the query string. Change the alias of the table to be the same and it looks like it'd be the same query.
I don't really recommend it though as they are different tables that could change and the circumstances for the need. Its better to have the defined query for its use in a business object or something.
But we do these things to save typing some times.

"ORA-00920: invalid relational operator" Error

select
ic.item_name,
lh.locn_brcd from_locn,
lh2.locn_brcd to_locn,
wl.from_container,
wl.to_container,
wl.units,
wl.prev_from_container_status prev_from_lpn_status,
wl.curr_from_container_status curr_from_lpn_status,
wl.prev_to_container_status prev_to_lpn_status,
wl.curr_to_container_status curr_to_lpn_status,
wl.work_batch_number,
wl.transaction_name,
wl.action,
wl.work_id,
wl.date_updated,
wl.source_updated,
wl.tote_number,
wl.chute
from m_work_log wl
LEFT join item_cbo ic on wl.item_id=ic.item_id
left join locn_hdr lh on wl.from_location_id = lh.locn_id
left join locn_hdr lh2 on wl.to_location_id = lh2.locn_id
where wl.action in (:action)
and trunc(wl.date_updated) between :start_date and :end_date
and (ic.item_name in (:list) OR
wl.source_updated = :username OR
wl.to_container in (:LPNList) OR
(:list is null and :username is null and :LPNList is null)
)
order by date_updated desc
Hi everyone,
when I run this code through Oracle SQL Developer and I add two items to the :list parameter and two items to the :action parameter it works fine. But when I run this through SSRS (report builder ) it fails to run and I get an "ORA-00920: invalid relational operator". I'm new to SQL and i'm not sure what I am doing incorrectly here. Any help is greatly appreciated. Thanks!
There are 2 ways to do this:
Multiple Value Parameter:
First of all, you must use Oracle provider, multiple value paramaters does not work with neither ODBC nor OLEDB connections (reference).
Here is an external link explaining in detail here.
Using an expression as the query by putting the whole thing like this ="query_here"
="select "
+ " ic.item_name,"
+ " lh.locn_brcd from_locn,"
+ " lh2.locn_brcd to_locn,"
+ " wl.from_container,"
+ " wl.to_container,"
+ " wl.units,"
+ " wl.prev_from_container_status prev_from_lpn_status,"
+ " wl.curr_from_container_status curr_from_lpn_status,"
+ " wl.prev_to_container_status prev_to_lpn_status,"
+ " wl.curr_to_container_status curr_to_lpn_status,"
+ " wl.work_batch_number,"
+ " wl.transaction_name,"
+ " wl.action,"
+ " wl.work_id,"
+ " wl.date_updated,"
+ " wl.source_updated,"
+ " wl.tote_number,"
+ " wl.chute"
+ "from m_work_log wl"
+ " LEFT join item_cbo ic on wl.item_id=ic.item_id"
+ " left join locn_hdr lh on wl.from_location_id = lh.locn_id"
+ " left join locn_hdr lh2 on wl.to_location_id = lh2.locn_id"
+ "where wl.action in (:action)"
+ " and trunc(wl.date_updated) between :start_date and :end_date"
+ " and (ic.item_name in ('" + Join(Parameters!list.Value , "', '")" + ') OR"
+ " wl.source_updated = :username OR"
+ " wl.to_container in ('" + Join(Parameters!LPNList.Value , "', '")" + ') OR"
+ " (:list = '_N/A_' and :username is null and :LPNList = '_N/A_')"
+ " )"
+ " order by date_updated desc"
In this case you will need to provide default empty values to your lists. I used 'N/A' in my example.

"where" restrictions not working on hql query with join clause

First of all clarify that I am quite bad with databases, so please do not be to mean with my code :P
I have a problem with a query on hibernate using join and restrictions. I have a huge list of Assignments and some of them have an Asr object.
queryString.append("select new commons.bo.assignment.AssignmentAssociateExtract("
+ " assignment.id, assignment.contract.assignmentStatus,"
+ " assignment.contract.beginDate, assignment.contract.endDate,"
+ " assignment.contract.contractType, assignment.organizationalData.homeCountryKey,"
+ " assignment.organizationalData.hostCountryKey,"
+ " assignment.organizationalData.homeOrgUnitKey,"
+ " assignment.associate.globalIdAssociate,"
+ " assignment.associate.localIdHome,"
+ " assignment.associate.firstName,"
+ " assignment.associate.lastName)"
+ " from Assignment assignment left join assignment.asr asr"
+ " where assignment.contract.assignmentStatus.code = 5"
+ " and asr.homeAsrElegibility is not 'X'"
+ " or asr.homeAsrElegibility is null"
);
I was creating this query step by step. Before I created it without the join clause left join assignment.asr asr and it was working well but of course it was not showing the Assignments that did not have a Asr object. After I added the join clause, now it shows every single Assignment (10.000 records when those who have an assignmentStatus = 5 are just 4.000) and the restrictions like
where assignment.contract.assignmentStatus.code = 5
are not reflected in the result anymore.
So to sum up: I need a list with all assignments with assignmentStatus = 5 and asr.homeAsrElegibility != 'X'. But it needs to include also all assignments with assignmentStatus = 5 even if they do not have an Asr object.
Any ideas?? Thanks!
Parenthesis helps to clarify the situation.
queryString.append("select new commons.bo.assignment.AssignmentAssociateExtract("
+ " assignment.id, assignment.contract.assignmentStatus,"
+ " assignment.contract.beginDate, assignment.contract.endDate,"
+ " assignment.contract.contractType, assignment.organizationalData.homeCountryKey,"
+ " assignment.organizationalData.hostCountryKey,"
+ " assignment.organizationalData.homeOrgUnitKey,"
+ " assignment.associate.globalIdAssociate,"
+ " assignment.associate.localIdHome,"
+ " assignment.associate.firstName,"
+ " assignment.associate.lastName)"
+ " from Assignment assignment left join assignment.asr asr"
+ " where assignment.contract.assignmentStatus.code = 5"
+ " and ((asr.homeAsrElegibility is not null and asr.homeAsrElegibility is not 'X')"
+ " or (asr.homeAsrElegibility is null))"
);

Sql Three Table Inner Join?

I'm trying to join 3 tables in a view; here is my situation:
I have a table that contains Sale it Contain Sale Details For per Item
Another Sale Master A All Sale Details of All Item..
And Another Inventory Details
String query = "SELECT SALE.ITEM_CODE, SALE.ITEM_NAME, SALE.UNIT, "
+ "SALE.QNTY, SALE.AMOUNT, SALE_MASTER.LONGDATE, SALE_MASTER.BILL_NO, "
+ "SALE_MASTER.LEDGER_CODE, SALE_MASTER.LEDGER_NAME FROM SALE "
+ "INNER JOIN SALE_MASTER"
+ " ON SALE.BILL_NO = SALE_MASTER.BILL_NO SALE"
+ "INNER JOIN INVENTORY ON SALE.ITEM_CODE = INVENTORY.ITEM_CODE"
+ "WHERE "+CATORINORG+" LIKE '%"+LIKE+"%' "
+ "AND (SALE_MASTER.LONGDATE >= " + From + " AND SALE_MASTER.LONGDATE <= " + To + ")";
is it right way.. thanks adv
SELECT SALE.ITEM_CODE, SALE.ITEM_NAME, SALE.UNIT, "
+ "SALE.QNTY, SALE.AMOUNT, SALE_MASTER.LONGDATE, SALE_MASTER.BILL_NO, "
+ "SALE_MASTER.LEDGER_CODE, SALE_MASTER.LEDGER_NAME FROM SALE "
+ "INNER JOIN SALE_MASTER"
+ " ON SALE.BILL_NO = SALE_MASTER.BILL_NO"
+ " INNER JOIN INVENTORY ON INVENTORY.ITEM_CODE = SALE.ITEM_CODE"
+ " WHERE "+CATORINORG+" LIKE '%"+LIKE+"%' "
+ "AND (SALE_MASTER.LONGDATE >= " + From + " AND SALE_MASTER.LONGDATE <= " + To + ")
its worked for me

Assignment error is thrown in the query

I have a query like this....
selectLeaveDetails =
"SELECT UL.[PK_ID],UD.FIRST_NAME + ' ' + UD.LAST_NAME AS REQUESTBY," +
"UL.[DATE_FROM] AS FROMDATE,UL.[DATE_TO] AS TODATE," +
"UL.LEAVE_REQUEST_ON AS REQUESTON," +
"REPLACE(UL.LEAVE_REQUEST_NOTES, '\n', '<br />') AS REQUESTNOTES," +
"STATUS=CASE " +
" WHEN UL.[LEAVE_STATUS] = '1' THEN 'ACTIVE' " +
" WHEN UL.[LEAVE_STATUS] = '-1' THEN 'CANCELLED' " +
" WHEN UL.[LEAVE_STATUS] = '2' THEN 'REPLACED' END," +
"UL.LEAVE_RESPONSE_ON AS RESPONSEON," +
"ULL.FIRST_NAME + ' ' + ULL.LAST_NAME AS RESPONSEBY," +
"UL.[LEAVE_RESPONSE_NOTES] AS RESPONSENOTES,UL.FK_LEAVE_REQUESTER " +
"FROM (M_USER_LEAVES UL " +
"INNER JOIN M_LEADERLED MLL ON MLL.LED_ID = MUD.PK_ID WHERE MLL.LEADER_ID = '" + Session["UserID"].ToString() ****" +****
"LEFT JOIN M_USER_DETAILS UD ON UD.PK_ID = UL.FK_LEAVE_REQUESTER) " +
"LEFT JOIN M_USER_DETAILS ULL ON ULL.PK_ID = UL.FK_LEAVE_RESPONSE_BY " +
" WHERE UL.DATE_FROM BETWEEN '01/01/" + cmbYearList.SelectedItem.Text + "' AND '12/31/" + cmbYearList.SelectedItem.Text + "'" +
" AND UD.ACTIVE=1";
In the cmbYearList.SelectedItem.Text + "' AND '12/31/" + cmbYearList.SelectedItem.Text + "'" query...only assignment,increment,decrement error is thrown
Can anyone help me?
Your FROM clause is somehow pretty mangled up:
FROM (M_USER_LEAVES UL
INNER JOIN M_LEADERLED MLL ON MLL.LED_ID = MUD.PK_ID
WHERE MLL.LEADER_ID = 'XXXX"
LEFT JOIN M_USER_DETAILS UD ON UD.PK_ID = UL.FK_LEAVE_REQUESTER)
You have an INNER JOIN, then a WHERE clause, followed by a LEFT JOIN .... this seems pretty odd..... what exactly are you trying to do here?? Why do you need to put this into a subquery - can't you just INNER JOIN and LEFT JOIN those tables into a single statement and define the necessary WHERE constraints?
Also, your WHERE clause in here gets an opening single quote and a closing double quote - that won't work ......
WHERE MLL.LEADER_ID = 'XXXX"
*** ***
You need to get your SQL query working in SQL Server Management Studio first - then transfer it into your C# code.