I've got a query that works fine when I run it in SQL Developer, but returns an empty result set when run as a prepared statement. I'm not sure if my query is formatted incorrectly, or if it's something else (which I'll leave for another question entirely).
So here is my query. I've stripped stuff out in order to capture the format of it, and not the business logic. The table has three columns: type, key, and value.
SELECT a.key id, a.value name
FROM
(SELECT * FROM sometable WHERE type='A') a,
(SELECT * FROM sometable WHERE type='B') b,
(SELECT * FROM sometable WHERE type='C') c,
(SELECT * FROM sometable WHERE type='D') d
WHERE a.value = b.key
AND a.value = c.key
AND a.value = d.key
Essentially, should this execute correctly in a prepared statement?
Are you seeing any errors?
The query as is can be run as a Statement since it is a static SQL.
Related
I try to formulate the following PostgreSQL Query into JOOQ:
SELECT * FROM
(SELECT *, row_number() OVER (PARTITION BY propertyid ORDER BY validfrom DESC)
AS rownum FROM propertyvalue WHERE validfrom <= '1978-01-01T00:00:00Z') X
WHERE rownum = 1;
So I want to write a query with a nested query which uses window functions and start with the inner query first. I came that far:
ctx.select(PROPERTYALUE.fields(), DSL.rowNumber().over().
partitionBy(PROPERTYVALUE.propertyid).orderBy(PROPERTYVALUE.validfrom).???)
After the orderBy command, I don't have a DESC option to be used anymore in JOOQ. There are just commands like rangeFollowing or rowsCurrentRow and an inherited command like desc() which returns a SortField-object on which I can't call my required subsequent commands anymore.
So is there any way in JOOQ to formulate my desired SQL query with the DESC order in a window function?
Write this:
DSL.rowNumber()
.over()
.partitionBy(PROPERTYVALUE.propertyid)
.orderBy(PROPERTYVALUE.validfrom.desc()))
Observe the fact that I called desc() on the column validfrom in order to pass the resulting SortField to the orderBy() clause of the window function call.
What you tried to do would have been to order by the window function expression, e.g. in an ORDER BY clause of the SELECT statement. E.g.:
SELECT *
FROM propertyvalue
WHERE validfrom <= '1978-01-01T00:00:00Z'
ORDER BY row_number() OVER (PARTITION BY propertyid ORDER BY validfrom) DESC
We primarily use SQL Server for our queries, but it seems that when we use Java it runs in an Oracle engine. In this question I asked for the SQL Server method of counting records from multiple tables since most of the time SQL Server runs the same as Oracle. I was able to get data returned from this query:
declare #count table (count1 int, count2 int, count3 int)
INSERT INTO #count
select (SELECT COUNT(*) FROM Bike),
(SELECT COUNT(*) FROM Car),
(SELECT COUNT(*) FROM Truck)
select * from #count;
However when putting this in Java I get an error that no records were returned. Any idea what I can do here to fix this up?
In Oracle, you need to use the dual table:
select (SELECT COUNT(*) FROM Bike),
(SELECT COUNT(*) FROM Car),
(SELECT COUNT(*) FROM Truck)
from dual;
If you need to return these values in a single row, then the following should work in either database:
select b.bcnt, c.ccnt, t.tcnt
from (SELECT COUNT(*) as bcnt FROM Bike) b cross join
(SELECT COUNT(*) as ccnt FROM Car) c cross join
(SELECT COUNT(*) as tcnt FROM Truck) t;
Using Java JDBC, I want to collect information returned from SQL Select query.
If I fire below SQL query:
SELECT col1 AS 'Field1', col2 AS 'Field2' FROM Table;
Then, using resultSetMetaData.getColumnName(1), I get 'col1' as result, which is the expected result.
Now, the problem is, when I join 2 SQL tables (Since, MySQL does not provide Full Outer Join, hence, I fired the following query)
SELECT Table1.Col1 AS 'Field1', Table1.Col3 AS 'Field2',
Table2.Col5 AS 'Field3',Table2.Col4 AS 'Field4' FROM Table1
LEFT JOIN Table2 ON Table1.id = Table2.id
UNION
SELECT Table1.Col1 AS 'Field1', Table1.Col3 AS 'Field2',
Table2.Col5 AS 'Field3',Table2.Col4 AS 'Field4' FROM Table1
RIGHT JOIN Table2 ON Table1.id = Table2.id;
Now, using resultSetMetaData.getColumnName(1), I get 'Field1' as result, where as, I expected 'col1'.
I tried resultSetMetaData.getColumnLabel(1) also, but it still returned 'Field1'.
I want 'col1' as the result, which I could not get by any of the methods of resultSetMetaData.
Any help on this will be appreciable.
You are seeing those results because it is a UNION query. It is entirely possible that such a query could do something like
SELECT Col1 AS Field1 FROM Table1
UNION
SELECT Col2 AS Field1 FROM Table2
In that case there is no single "correct" answer if getColumnName was to try and return the name of the underlying column in the result: Should it return 'Col1' or 'Col2'?
Since any column in the result set of a UNION query can be derived from more than one underlying column, getColumnName can only return the effective name of that column, which is Field1 in the example above.
I'm trying to join two tables but can't get the SQL right. My SQL statement is far more complex than the below SQL but the below SQL will do to show the principle. I get an invalid identifier exception when trying to refer to t or p. How come I cannot refer to these? I'm using Oracle DB.
SQL:
SELECT * FROM ((SELECT * FROM transactions t) FULL JOIN (SELECT * FROM payments p) ON (t.id = p.trans_id));
Exception:
Caused by: java.sql.SQLException: ORA-00904: "P"."TRANS_ID": invalid identifier
You are not really giving your querys an alias, you should do:
SELECT *
FROM (SELECT * FROM transactions) t
FULL JOIN (SELECT * FROM payments) p
ON (t.id = p.trans_id);
Of course, assuming that you are using real derived tables instead of those simple SELECTs, otherwise you should just use the tables directly.
The aliases are in the wrong place. The alias for a derived table should be just after the closing parenthesis:
SELECT *
FROM
(SELECT * FROM transactions) t
FULL JOIN
(SELECT * FROM payments) p
ON t.id = p.trans_id
Note that it makes no sense to use derived tables with such a simple query. I assume that you do actually need the derived tables and that you just simplified your query for the question.
I have the following code
Criteria criteria = this.getCriteriaForClass(DeviceListItem.class);
Projection rowCountProjection = Projections.countDistinct("color");
criteria.setProjection(rowCountProjection);
int rowCount = ((Long) criteria.uniqueResult()).intValue();
return rowCount;
, whose purpose is to find out the number of rows with different values for the field named "color". The problem is that
Projections.countDistinct("color");
returns the same number of results as
Projections.count("color");
even though there are multiple rows with same color in the database view. When converting the Criteria object to SQL, I see that the SQL produced by Hibernate is
select count(this_.COLOR) as y0_ from DEVICESLIST_VIEW this_ where 1=1
when I would expect it to be
select count(distinct this_.COLOR) as y0_ from DEVICESLIST_VIEW this_ where 1=1
Why doesn't it work like expected and is there some remedy? Unfortunately I have no option to use HQL in this case.
It's a bug, fixed in 3.5.2: HHH-4957.