Using WITH clause in Jasper report (JRXML file) - jasper-reports

I am trying to use the WITH clause in my jasper report query, but it is giving me the error: -
java.sql.SQLSyntaxErrorException: ORA-00928: missing SELECT keyword
and net.sf.jasperreports.engine.JRException: No input source supplied to the exporter.
The same query works perfectly fine in the Oracle DB client.
Please let me know if there is a problem in using the WITH clause with Jasper report version 3.7.6.

If you are using WITH clause in the starting of select query then, you need to change in Jasper report configuration where you need to specify query start with either SELECT or WITH.
If you are using WITH clause in the middle of select query, you might forget to select records from the dataset generated from WITH clause. See below example...
select * from (
WITH temp as (
select data from any_table
)
)
Instead, use below...
select * from (
WITH temp as (
select data from any_table
)
select * from temp
)
OR
select * from (
select * from
(
WITH temp as (
select data from any_table
)
)
)
Keep Querying!

Related

Loaded multiple files in table and now want to find Min and Max value is coming from which file

I have loaded multiple files in SQL Server.
Let's Say
table structure
Now
select COL_A, min(COL_C),max(COL_C)
from tbl
group by COL_A
I want to write SQL query to find which file has that Min(COL_C) and Max(COL_C) value. As you can see I am storing FILENAME in table.
So I want result like this
Here's one possible way you could get your results, using apply to correlate each aggregated value back to the same table to find the filename for that value, and using string_agg to produce a delimeted list if there are ties.
select * from (
select COL_A, Min(COL_C) MinColC,Max(COL_C) MaxColC
from T
group by COL_A
)x
outer apply (
select string_agg([filename], ', ') sMinFilename
from T
where T.COL_A=x.COL_A and T.COL_C=x.MinColC
)mn
outer apply (
select string_agg([filename], ', ') MinFilename
from T
where T.COL_A=x.COL_A and T.COL_C=x.MaxColC
)mx

"org.netezza.error.NzSQLexception:ERROR " >>Telend to NETEZZA group by / subquery error

I am trying to add component 'tNetezzainput' in Talend
with query like
select col1,col2 from
(select col1,col2 from tab2 )
when i run sub query independently it is allowing to get schema
but when i run above query it is showing error like
org.netezza.error.NzSQLexception:ERROR
(above query)
You need to add an alias after the parentheses (I randomly chose ‘x’):
select col1,col2 from
(select col1,col2 from tab2 ) x

Postgres JDBC Numbered Parameters

I have a table in a Postgres 9.3 database with a json column like this:
CREATE mytable (
mycolumn json
)
I would like to execute queries from a Java application that look like this:
SELECT
mycolumn->>'somefield',
count(*)
FROM
mytable
GROUP BY
mycolumn->>'somefield'
When I try to use a PreparedStatement like this:
SELECT
mycolumn->>?,
count(*)
FROM
mytable
GROUP BY
mycolumn->>?
I get the following error:
PSQLException: ERROR: column "mytable.mycolumn" must appear in the GROUP BY clause or be used in an aggregate function
It makes sense that this happens because Postgres cannot guarantee that the two positional parameters are the same.
Using psql, I can prepare a statement like this:
PREPARE mystatement AS
SELECT
mycolumn->>$1,
count(*)
FROM
mytable
GROUP BY
mycolumn->>$1
Is it possible to do this with JDBC?
No, this isn't possible. JDBC only has positional parameters, and therefor the PostgreSQL driver will render it as:
PREPARE mystatement AS
SELECT
mycolumn->>$1,
count(*)
FROM
mytable
GROUP BY
mycolumn->>$2
And as the value of $1 is not necessarily the same as $2, the parser of PostgreSQL will reject it as you are potentially not grouping on the same column.
The solution might be to do:
SELECT a.aColumnLabel, count(*)
FROM (
SELECT mycolumn->>? as aColumnLabel
FROM mytable
) a
GROUP BY a.aColumnLabel

Create a new table out of an existing one

I am having this table of words from multiple files. I want to count how many files each word shows. I can that with the piece of code below. But when I nest it with the CREATE TABLE statement, it won't work. The second piece of code below is the error code.
SELECT WORD, COUNT(*) FROM (select DISTINCT ABSTRACTID, WORD FROM NSFABSTRACTS)
GROUP BY WORD ORDER BY COUNT(*) DESC
CREATE TABLE DOC_FREQ (WORD, TOTALCOUNT) AS
(
SELECT WORD, COUNT(*) FROM (select DISTINCT ABSTRACTID, WORD FROM NSFABSTRACTS)
GROUP BY WORD ORDER BY COUNT(*));
Here is the error message:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Can anyone suggest how to create this table? Thanks.
You cannot use order by when you have the query enclosed in parentheses; at least if that clause is also within the parentheses:
create table t42 as (select * from dual order by dummy);
SQL Error: ORA-00907: missing right parenthesis
It is allowed outside:
create table t42 as (select * from dual) order by dummy;
table T42 created.
You can remove the parentheses as they aren't needed at all here:
create table t42 as select * from dual order by dummy;
table T42 created.
Or remove the order by, since an order by in the create statement usually makes little difference and doesn't affect how the data is retrieved:
create table t42 as (select * from dual);
table T42 created.
Or preferably for my tastes, both:
create table t42 as select * from dual;
table T42 created.

Excel parameter in t/sql SELECT TOP xxx without using SP

This is really only a short question, but I can't find the answer anywhere.
Is it possible to use an Excel parameter in a t/sql query as a SELECT TOP xxxx, where the parameter here defines how many rows are fetched, without using a stored procedure?
From sql 2005 and up you can write your query as:
Enter the following in your msquery:
{CALL sp_executesql (N'select top (#a) * from mytable', N'#a int', ?)}
I'm not sure about applying the Excel parameter, but to do this in T-SQL do:
select *
from (
select row_number() over (order by [Field]) as rowNum, *
from [myTable] ) s
where s.rowNum < #maxRows
If you are in Excel can you not use Concat to build up the sql query?
Concatenate("select top ", $D2, " from mytable where...")
and use the value of that cell as your tsql expression?
http://msdn.microsoft.com/en-us/library/aa188518(v=office.10).aspx