ResultSet Value of Aliased Column without Column Index - java

I'm running the following query using a jdbc template against a MySQL db:
SELECT d.*, a.mongo_id as yyyyy_profile_mongo_id,t.mongo_id as targeting_profile_mongo_id, CAST(xxxxx_type AS SIGNED) AS xxxxx_type_int
LEFT JOIN zzzzz t on t.id = d.targeting_profile_id
LEFT JOIN yyyyy a on a.id = d.yyyyy_profile_id
FROM xxxxx d
When attempting to get a string from a ResultSet using the aliased name "targeting_profile_mongo_id" I get a "Invalid Column Name" error
I can see in the debugger that even though I did "as targeting_profile_mongo_id" the columnName still shows as the original value of "mongo_id" and only the column label is changed.
Are there any work arounds here without having to loop through the metadata to find the column index?

I found a solution after some more digging! Looks like you can somehow get around this by putting a concat around the columns causing the issue.
like this
SELECT d.*, CONCAT(a.mongo_id,'') as yyyyy_profile_mongo_id,CONCAT(t.mongo_id,'') as targeting_profile_mongo_id, CAST(xxxxx_type AS SIGNED) AS xxxxx_type_int
LEFT JOIN zzzzz t on t.id = d.targeting_profile_id
LEFT JOIN yyyyy a on a.id = d.yyyyy_profile_id
FROM xxxxx d
mysql column alias not working, have to create an empty concatenation to make it work

Related

How to use Count(*) in JPQL

I have a JPQL subquery in which I want to return a list of customerIds that meet a specific condition based on a ManyToOne relationship as shown below:
SELECT c.customerId
FROM Customer c
INNER JOIN FETCH c.customersChild cc
LEFT JOIN FETCH c.childsPet cp on cp.name = 'Rover'
GROUP BY c.customerId
HAVING (COUNT(cp.name) / COUNT(*)) = 1
In this case, the customer should only be present in the list if all of their childrens' pet's names are Rover. The HAVING (COUNT(cp.name) / COUNT(*)) = 1 clause works as-is in Oracle (SQL), since COUNT(cp.name) counts the number of non-null rows for each customer, and COUNT(*) counts the total number of rows (including nulls present due to the left join) for each customer... I believe COUNT(cp.name) works in JPQL but it doesn't seem like there is equivalent for COUNT(*)... does anyone know if there is a way to count all the rows within a group including nulls?
I would suggest you rewrite your query to the more understandable anti-join variant:
SELECT c.customerId
FROM Customer c
WHERE NOT EXISTS (
SELECT 1
FROM c.customersChild cc
JOIN cc.childsPet cp
WHERE cp.name = 'Rover'
)

Oracle SQL Developer - JOIN on 2 queries with a one-to-many relationship

I have two queries that I'm trying to join together.
In first_query TABLE2.PROCESS_ID, every PROCESS_ID is unique in that table. In second_query though there are several PROCESS_ID's with the same number in TABLE3, so I think I have to do a one-to-many join. The join_query I have is giving me an error ORA-00933: SQL command not properly ended which I'm assuming has something to do with the one-to-many relationship with the JOIN.
I'm not really sure how to resolve this. Any help would be much appreciated!
first_query = """
SELECT TABLE1.RULE_ID, TABLE2.STATUS, TABLE2.ERROR_MESSAGE, TABLE2.PROCESS_ID
FROM TABLE2 LEFT JOIN
TABLE1
ON TABLE1.RULE_ID = TABLE2.RULE_ID
WHERE TABLE1.RULE_NAME IN ('TEST1', 'TEST2')
"""
second_query = """
SELECT RECORDS_PROCESSED, PROCESS_ID, STATUS
FROM TABLE3
"""
join_query = """
SELECT RULE_ID, STATUS, ERROR_MESSAGE, PROCESS_ID
FROM (first_query) as query_1
INNER JOIN (second_query) as query_2
ON query_1.PROCESS_ID = query_2.PROCESS_ID
GROUP BY PROCESS_ID desc
"""
You can not select 4 columns and group by only one of them unles you include selected columns as part of aggregation fucntion(like max(), sum(),...). One of the options is this:
SELECT query_1.RULE_ID --1
, query_2.STATUS --2
, query_1.ERROR_MESSAGE --3
, query_1.PROCESS_ID --4
FROM (SELECT TABLE1.RULE_ID
, TABLE2.STATUS
, TABLE2.ERROR_MESSAGE
, TABLE2.PROCESS_ID
FROM TABLE2
LEFT JOIN TABLE1
ON TABLE1.RULE_ID = TABLE2.RULE_ID
WHERE TABLE1.RULE_NAME IN ('TEST1', 'TEST2')) query_1
INNER JOIN (SELECT RECORDS_PROCESSED
, PROCESS_ID
, STATUS
FROM TABLE3) query_2
ON query_1.PROCESS_ID = query_2.PROCESS_ID
GROUP BY query_1.RULE_ID
, query_2.STATUS
, query_1.ERROR_MESSAGE
, query_1.PROCESS_ID
Also please do consider using aliases like this(in your first query):
SELECT T1.RULE_ID
, T2.STATUS
, T2.ERROR_MESSAGE
, T2.PROCESS_ID
FROM TABLE2 T2
LEFT JOIN TABLE1 T1 ON T1.RULE_ID = T2.RULE_ID
WHERE T1.RULE_NAME IN ('TEST1', 'TEST2')
Also, apply the same logic with aliases on your final query or else you will have a different kind of error : "ORA-00918: column ambiguously defined"
Here is a small demo
CTE (i.e. the WITH factoring clause) might help.
WITH first_query
AS (SELECT table1.rule_id,
table2.status,
table2.error_message,
table2.process_id
FROM table2 LEFT JOIN table1 ON table1.rule_id = table2.rule_id
WHERE table1.rule_name IN ('TEST1', 'TEST2')),
second_query
AS (SELECT records_processed, process_id, status FROM table3)
SELECT a.rule_id,
a.status,
a.error_message,
a.process_id
FROM first_query a INNER JOIN second_query b ON a.process_id = b.process_id
GROUP BY you used is invalid; you can't group results by only one column. If results have to be unique, use select distinct. If you have to use group by, specify all columns returned by select (which leads you back to what distinct does), or see whether some column(s) have to be aggregates - in that case, group by makes sense.
Also, you should always use table aliases. Without them, query is invalid as database engine doesn't know which table those columns (if they share the same name) belong to.

join and where clause in hibernate

I am using join and where clause in hibernate 3.but i cant reach the solution.I got the error.
Query qry= session.createQuery("SELECT addemployee.eid,addemployee.fname,addemployee.location,"
+ "empdet.jtitle,empdet.leadname FROM addemployee LEFT JOIN empdet ON addemployee.eid = empdet.eid WHERE (addemployee.eid ='"+id+"')");
List l = qry.list();
Iterator it=l.iterator();
while(it.hasNext())
{
Object rows[] = (Object[])it.next();
System.out.println(rows[0]+separator+rows[1]+separator+rows[2]+separator+rows[3]+separator+rows[4]);
}
Issue: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ON near line 1, column 127 [SELECT addemployee.eid,addemployee.fname,addemployee.location,empdet.jtitle,empdet.leadname FROM addemployee LEFT JOIN empdet ON addemployee.eid = empdet.eid WHERE (addemployee.eid ='206')]
Try to use session.createSQLQuery() instead.
and don't put enclosed apostrophe (''), you are inputting numbers, not varchar.
like this.
Query qry= session.createSQLQuery("SELECT addemployee.eid,addemployee.fname,addemployee.location,"
+ "empdet.jtitle,empdet.leadname FROM addemployee LEFT JOIN empdet ON addemployee.eid = empdet.eid WHERE (addemployee.eid ="+id+")");
Hibernate Session's createQuery() method requires valid HQL syntax. You can check how to write joins here.
Basically, in HQL you work with your entities, not SQL tables. So you don't need to write ON, because you already map association between entities.
If you still want to write native SQL query, you need to use
session.createSQLQuery(); instead

One statement to select data from two MySQL tables. Show name instead of/by ID

I have 2 tables
The table .issues looks like this:
id tracker_id project_id
22 1 218
In the second table .trackers:
id name
1 Error
Is it possible to make one Select request to receive back the result with names of trackers instead of/with their ids? I need it to simplify the Java Request.
I want to get smth like this:
id (tracker_id) project_id tracker_name
22 (1) 218 Error
I tried this, but I know it doesn't make much sense:
Statement stmt1 = conn.createStatement();
ResultSet ticketsRows = stmt1.executeQuery("SELECT * FROM issues
WHERE tracker_id IN (SELECT id FROM trackers)");
Join the trackers table and select the name column from that table instead the tracker_id
SELECT i.id, t.name as tracker_name, i.project_id
FROM issues i
JOIN trackers t on i.tracker_id = t.id
Change your query to :
SELECT i.id, i.tracker_id, i.project_id, t.name AS tracker_name
FROM issues i
JOIN trackers t on i.tracker_id = t.id
this would give you exact answer to your question --
SELECT IS.ID, IS.tracker_id ,IS.project_id , TR.tracker_name
FROM issues IS , tracker TR
where
IS.tracker_id = TR.id

How to use sql query with IN for a select statement in Java

I need to us a sql query in java that has an IN clause in it. I have seen suggestions for doing this when the IN clause is a predetermined set, such as numbers which won't work in this case because the set will be dynamic. Here is what I am trying to do:
select c.cust_name
from cust_acct c, has h
where c.cust_id=h.cust_id
and h.sh_add in (select a.sh_add from address a where sh_st='VA')
I do not get any faults. That is, the code runs I just get no results for this particular query
Have you checked that you are getting the right intermediate results from select a.sh_add from address a where sh_st='VA'?
You could just rewrite the query as:
select c.cust_name
from cust_acct c
inner join has h
on c.cust_id=h.cust_id
inner join address a
on h.sh_add = a.sh_add
where a.sh_st = 'VA'

Categories

Resources