Subselect in ReportQuery
Hi,
(first time post, please move if appropriate)
I can find lots of information in the Toplink docs and elsewhere about using sub queries in the selection criteria of a ReportQuery, but not as an attribute. Here's what I'm trying to do:
I have two tables, SUPPLIER and DOCUMENT. I'm trying to get various values from the SUPPLIER table, and a count from the DOCUMENT table, for a given SUPPLIER ID. That is, some data about the supplier, and the number of documents he/she has. In SQL I would do:
SELECT COLUMNA, COLUMNB, (SELECT COUNT(*) FROM DOCUMENT WHERE DOCUMENT.SUPPLIER_ID = 123) FROM SUPPLIER WHERE SUPPLIER_ID = 123
which seems to work. But, as a ReportQuery, I'm having trouble, here's what I'm doing:
ExpressionBuilder exp = new ExpressionBuilder();
ReportQuery query = new ReportQuery(exp);
ExpressionBuilder equalOppsBuilder = new ExpressionBuilder(Document.class);
ReportQuery equalOppsQuery = new ReportQuery(equalOppsBuilder);
equalOppsQuery.setReferenceClass(Document.class);
equalOppsQuery.addCount("count", equalOppsBuilder.get("SUPPLIER_ID"));
equalOppsQuery.setSelectionCriteria(equalOppsBuilder.get("SUPPLIER_ID").equal(getSupplierId()));
query.addAttribute("equalOppsCount", expBuilder.subQuery(equalOppsQuery));
With the code between the ...'s above commented out, my query works correctly. With it in, I get a java.lang.NumberFormatException.
Digging a little deeper, I see that the query being sent to the database (according to the logs) doesn't actually include the sub select at all. It seems that once the query is returned, Toplink is expecting the result, and the columns following the position of the subselect are being put in the wrong place.
The following 5 columns are numbers, the 6th being text. It seems that the NumberFormatException is caused by Toplink trying to parse the text value as a number. Commenting out that line causes the same problem further down, with another text value that follows a number. Further, removing that line (and, similarly, putting the subselect as the last attribute in the query), gives me an oracle.toplink.exceptions.QueryException:
Exception Description: ReportQuery result size mismatch. Expecting [51], but retrieved [51]
I've tried various different combinations of the code above, doing the subselect, such as just using equalOppsQuery.addCount() without parameters, using addMaximum() instead, and using query.addItem() instead of addAttribute(), none of which has made a difference.
Perhaps there's something obvious that I'm getting wrong...? Any pointers gratefully appreciated.
TopLink ReportQuery does not support sub-selects in the from SELECT clause. If you require this you will need to use custom SQL.
You may be able to define the same query using a group-by however.
(you may need to group-by each field your select, but should still get correct result).
For the NumberFormatException and the QueryException could you include the stack trace.
-- James : http://www.eclipselink.org
Similar Messages
-
Problem with a group by in a subselect - ORA-00979
Hi,
I have some difficulties with my query, I have every time an error ORA-00979 not a group by expression. Does anybody could help me, please ?
Thanks.
Here my query :
select
count(RES."ID") as "NbrRes",
RES.PATIENT_LASTNAME as "lastname",
RES.PATIENT_MAIDEN_NAME as "maiden name",
RES.PATIENT_FIRSTNAME as "firstname",
RES.EXTERNAL_NUMBER as "numero externe",
TRUNC(MONTHS_BETWEEN(RES.SAMPLE_SAMPLING_DATE,RES.PATIENT_BIRTHDATE)/12)+1 as "Age",
(select BOX_FULLNAME from HCQ_STORAGE_CONTAINER where BOX_ID=RES.STORAGE_BOX_ID) as "Box",
count(RES."ID")*RES.QUANTITY as "QtDispo",
(select SHORT_NAME from PRIMARY_UNIT where ID=RES.UNIT_ID) as "Unit"
from
HCQ_RESOURCE RES
group by
RES.PATIENT_LASTNAME,
RES.PATIENT_MAIDEN_NAME,
RES.PATIENT_FIRSTNAME,
RES.EXTERNAL_NUMBER,
RES.QUANTITY,
TRUNC(MONTHS_BETWEEN(RES.SAMPLE_SAMPLING_DATE,RES.PATIENT_BIRTHDATE)/12)+1
order by
TRUNC(MONTHS_BETWEEN(RES.SAMPLE_SAMPLING_DATE,RES.PATIENT_BIRTHDATE)/12)+1user539451 wrote:
Hi,
I have some difficulties with my query, I have every time an error ORA-00979 not a group by expression. Does anybody could help me, please ?
Thanks.
Here my query :
select
count(RES."ID") as "NbrRes",
RES.PATIENT_LASTNAME as "lastname",
RES.PATIENT_MAIDEN_NAME as "maiden name",
RES.PATIENT_FIRSTNAME as "firstname",
RES.EXTERNAL_NUMBER as "numero externe",
TRUNC(MONTHS_BETWEEN(RES.SAMPLE_SAMPLING_DATE,RES.PATIENT_BIRTHDATE)/12)+1 as "Age",
(select BOX_FULLNAME from HCQ_STORAGE_CONTAINER where BOX_ID=RES.STORAGE_BOX_ID) as "Box",
count(RES."ID")*RES.QUANTITY as "QtDispo",
(select SHORT_NAME from PRIMARY_UNIT where ID=RES.UNIT_ID) as "Unit"
from
HCQ_RESOURCE RES
group by
RES.PATIENT_LASTNAME,
RES.PATIENT_MAIDEN_NAME,
RES.PATIENT_FIRSTNAME,
RES.EXTERNAL_NUMBER,
RES.QUANTITY,
TRUNC(MONTHS_BETWEEN(RES.SAMPLE_SAMPLING_DATE,RES.PATIENT_BIRTHDATE)/12)+1
order by
TRUNC(MONTHS_BETWEEN(RES.SAMPLE_SAMPLING_DATE,RES.PATIENT_BIRTHDATE)/12)+1Hi,
Tested with other sample data, it works fine.. You need to include subselect column in the group by clause.
Rewrite your code as
select
count(RES."ID") as "NbrRes",
RES.PATIENT_LASTNAME as "lastname",
RES.PATIENT_MAIDEN_NAME as "maiden name",
RES.PATIENT_FIRSTNAME as "firstname",
RES.EXTERNAL_NUMBER as "numero externe",
TRUNC(MONTHS_BETWEEN(RES.SAMPLE_SAMPLING_DATE,RES.PATIENT_BIRTHDATE)/12)+1 as "Age",
(select BOX_FULLNAME from HCQ_STORAGE_CONTAINER where BOX_ID=RES.STORAGE_BOX_ID) as "Box",
count(RES."ID")*RES.QUANTITY as "QtDispo",
(select SHORT_NAME from PRIMARY_UNIT where ID=RES.UNIT_ID) as "Unit"
from
HCQ_RESOURCE RES
group by
RES.PATIENT_LASTNAME,
RES.PATIENT_MAIDEN_NAME,
RES.PATIENT_FIRSTNAME,
RES.EXTERNAL_NUMBER,
RES.QUANTITY,
TRUNC(MONTHS_BETWEEN(RES.SAMPLE_SAMPLING_DATE,RES.PATIENT_BIRTHDATE)/12)+1 ,
Age,
Box,
Unit
order by 6Hope this helps.
Regards,
Achyut -
Using a subselect in a procedure
In my procedure, I want to use a subselect to get some of the values, but i want to hard code some of the others. This is what I mean
This would be part of my insert statement for example. I want to set project_associate_oid to project_associate_oid.NEXTVAL and hard code project_type_oid and project_oid. Then I want to use a select to populate the short_title, title, address, city, state, zip. How would I do that? I know how to use the subselect, put how do you use it with hard coded values?
insert into project_associate
project_associate_oid, project_type_oid, project_oid,
short_title, title, address, city, state, zip
Select title1, title2, address, city, state, zip from table
End;
Thanksinsert into project_associate (project_associate_oid, project_type_oid, project_oid, short_title, title, address, city, state, zip)
Select project_associate_oid.NEXTVA, 'hard1','hard2', title1, title2, address, city, state, zip from table -
Using the result of a subselect in another part of the select
Does anyone know if 9i can use the results of a subselect in a select in another part of the select. For example:
select x, (select y from sometable) as ssvar, z, (select abc from someothertable where somecolumn=ssvar) from yetanothertable
Anyone? We have a bunch of Sybase queries we are porting over and they use this extensively. It looks like the 9i docs say this may not be possible, but I really wanted to be sure before I rewrite the queries.
-LouFrom your example, an inline view seems most appropriate to me...
select max_table.table_name, column_name
from user_tab_columns, (
select max(table_name) as table_name
from user_tables) max_table
where max_table.table_name = user_tab_columns.table_name
However, if your actual usage is more complicated, you might want to look at 'subquery factoring' (new in 9i). It doesn't really do much good for this example but here's the syntax...
with max_table as (
select max(table_name) as table_name
from user_tables)
select max_table.table_name, column_name
from max_table, user_tab_columns
where user_tab_columns.table_name = max_table.table_name
Richard -
Group by with a subselect?
I have a problem when trying to group by the results of a subselect. I keep getting the error ORA-00979: not a GROUP BY expression, though I can't see the problem with the SQL.
I want to count the number of rows in a table having / not having at least one row in another table.
The query is equivalent to:
select c1, count(*) from
(select case when exists
(select * from dual t2
where t1.dummy=t2.dummy)
then 'Y' else 'N' end c1
from dual t1) t
group by c1
This fails on every version of Oracle I've tried, and I get the same error with:
select c1, count(*) from
(select case when t1.dummy in
(select t2.dummy from dual t2)
then 'Y' else 'N' end c1
from dual t1) t
group by c1
Is this a fault with the SQL, or with Oracle?
Thanks in advance,
RexOn 9.2.0.5:
ora9205>select c1, count(*)
2 from (select case
3 when exists
4 (select * from dual t2
5 where t1.dummy = t2.dummy)
6 then 'Y' else 'N'
7 end c1
8 from dual t1) t
9 group by c1;
C COUNT(*)
Y 1
1 row selected.
-- Two approaches with the DEPT and EMP tables
ora9205>select c1, count(*)
2 from (select case
3 when exists
4 (select null
5 from emp e
6 where e.deptno = d.deptno)
7 then 'Y' else 'N'
8 end c1
9 from dept d)
10 group by c1;
C COUNT(*)
N 2
Y 3
2 rows selected.
ora9205>select 'Y', count(*)
2 from dept d
3 where exists (select null
4 from emp e
5 where e.deptno = d.deptno)
6 union all
7 select 'N', count(*)
8 from dept d
9 where not exists (select null
10 from emp e
11 where e.deptno = d.deptno);
' COUNT(*)
Y 3
N 2
2 rows selected. -
Compilation problems using NVL function in Pro*C subselect
I have come across a weird oracle problem. When I execute the following query in SQLPlus it works but when
I include it in Pro*C code in a EXEC SQL statement it gives syntax errors and fails to compile. Any idea what I am doing wrong.
SELECT DISTINCT
A.ID_PERSON,
C.ID_STAGE_PERSON_LINK ,
A.NM_PERSON_FULL,
A.NBR_PERSON_AGE,
A.ADDR_PERSON_ST_LN_1,
A.ADDR_PERSON_CITY,
A.ADDR_PERSON_ZIP,
A.CD_PERSON_STATE,
A.CD_PERSON_COUNTY,
A.NBR_PERSON_PHONE,
C.CD_STAGE_PERS_REL_INT
FROM
PERSON A,
STAGE_PERSON_LINK C
WHERE
C.ID_CASE = 88776721
AND A.IND_INVALID_PERS IS NULL
AND C.CD_STAGE_PERS_TYPE = 'PRN'
AND C.ID_PERSON = A.ID_PERSON
AND C.ID_STAGE_PERSON_LINK =
NVL (
(SELECT MAX(F.ID_STAGE_PERSON_LINK)
FROM STAGE_PERSON_LINK F
WHERE F.ID_PERSON=C.ID_PERSON
AND F.ID_CASE = C.ID_CASE
AND F.CD_STAGE_PERS_TYPE = 'PRN'
AND F.CD_STAGE_PERS_REL_INT IS NOT NULL)
(SELECT MAX(G.ID_STAGE_PERSON_LINK)
FROM STAGE_PERSON_LINK G
WHERE G.ID_PERSON=C.ID_PERSON
AND G.ID_CASE = C.ID_CASE
AND G.CD_STAGE_PERS_TYPE = 'PRN')
AND A.ID_PERSON NOT IN
SELECT S.ID_PERSON
FROM STAGE_PERSON_LINK S,STAGE T
WHERE S.ID_CASE = C.ID_CASE
AND S.ID_CASE = T.ID_CASE
AND S.ID_STAGE = T.ID_STAGE
AND T.CD_STAGE <> 'INT'
MINUS
SELECT H.ID_PERSON
FROM STAGE_PERSON_LINK H, STAGE F
WHERE H.ID_CASE = F.ID_CASE
AND H.ID_STAGE = F.ID_STAGE
AND H.ID_CASE = C.ID_CASE
AND H.CD_STAGE_PERS_ROLE <> 'XE'
AND F.CD_STAGE <> 'INT'
This query returns data when run in sqlplus.When used in a Pro*C C program and compiled the precompiler complains with syntax errors when used with EXEC SQL DECLARE CLSS82D_CURSOR CURSOR FOR < query above >.However removing the NVL function and retaining the subselect clause compiles but that is not what I want to do.
Syntax error at line 262, column 20, file clss82d.pc:
Error at line 262, column 20 in file clss82d.pc
SELECT MAX( F.ID_STAGE_PERSON_LINK )
...................1
PCC-S-02201, Encountered the symbol "MAX" when expecting one of the following:
( ) * + - / . @ | at, day, hour, minute, month, second, year,
The symbol "(" was substituted for "MAX" to continue.
Syntax error at line 263, column 10, file clss82d.pc:
Error at line 263, column 10 in file clss82d.pc
FROM STAGE_PERSON_LINK F
.........1
PCC-S-02201, Encountered the symbol "FROM" when expecting one of the following:
, ) * + - / | at, day, hour, minute, month, second, year,Pro*C works bit differently tha sqlplus. try removing the blank line after
SELECT MAX(G.ID_STAGE_PERSON_LINK)
FROM STAGE_PERSON_LINK G
WHERE G.ID_PERSON=C.ID_PERSON
AND G.ID_CASE = C.ID_CASE
AND G.CD_STAGE_PERS_TYPE = 'PRN')
good luck,
Gauranga -
Using the result of a function, inside a subselect
Hi!
I´m wondering if it´s possible to use the result of a function inside a subselect. Let me give you an example of what I´m trying to do here:
select * from t_node where node_pk in (get_node_parents_pk(22345));
The function get_node_parents_pk stands in for the following SELECT-statment:
select node_pk from t_node_child where parent_node_pk = 12345
The statement above would return something like this: 12435,23423,23453,23452
These values represent the node_pk value for the parent nodes.
I want the get_node_parents_pk function to return a result set similar to this so that I might call it inside the IN ( ) statement.
Any clue? =)I created a collection type in the database:
CREATE OR REPLACE TYPE nodes_pk_arr IS TABLE OF INTEGER;
The function get_node_parents_pk () is made to return the collection type above. However, this does not work. I get the following error message:
SELECT *
FROM t_node
WHERE node_pk IN
(SELECT * FROM TABLE (get_node_parents_pk (22345)));
ORA-22905: cannot access rows from a non-nested table item
However, if I insert a nodes_pk_arr collection directly into the SQL-statement, like I do below, it works:
SELECT *
FROM t_node
WHERE node_pk IN
(SELECT * FROM TABLE (nodes_pk_arr(24564,23545,34523));
So, when returning the collection from the function I´m told that the collection is not a nested table, when in fact it is. What gives?
Also, is there no way to return a result set directly from the get_node_parents_pk() function, making it possible to write the statement like that shown below?
SELECT *
FROM t_node
WHERE node_pk IN (get_node_parents_pk (22345));
Your reply is much appreciated!
Kind regards
Robert -
Hello,
i want to write an sql-update statement where a field through a part of another field will be supplemented.
example:
update tabelle set targetDirectiory = ' export/home/VALUE_FROM_FIELD x' where blablabla
This should work as a subselect ?but I do not know how i can merge the strings
Thanks a lot.
LutzIt could have been better if you would have provided some supporting data. Anyway
You can do it like
UPDATE emp e
SET dept_name = 'DEPT-'||(SELECT d.dept_name
FROM dept d
WHERE d.dept_id = e.dept_id
WHERE blahblahblah;Regards
Arun -
SQL Optimization with join and in subselect
Hello,
I am having problems finding a way to optimize a query that has a join from a fact table to several dimension tables (star schema) and a constraint defined as an in (select ....). I am hoping that this constraint will filter the fact table then perform the joins but I am seeing just the opposite with the optimizer joining first and then filtering at the very end. I am using the cost optimizer and saw that it does in subselects last in the predicate order. I tried the push_subq hint with no success.
Does anyone have any other suggestions?
Thanks in advance,
David
example sql:
select ....
from fact, dim1, dim2, .... dim <n>
where
fact.dim1_fk in ( select pf from dim1 where code = '10' )
and fact.dim1_fk = dim1.pk
and fact.dim2_fk = dim2.pk
and fact.dim<n>_fk = dim<n>.pkThe original query probably shouldn't use the IN clause because in this example it is not necessary. There is no limit on the values returned if a sub-select is used, the limit is only an issue with hard coded literals like
.. in (1, 2, 3, 4 ...)Something like this is okay even in 8.1.7
SQL> select count(*) from all_objects
2 where object_id in
3 (select object_id from all_objects);
COUNT(*)
32378The IN clause has its uses and performs better than EXISTS in some conditions. Blanket statements to avoid IN and use EXISTS instead are just nonsense.
Martin -
SQL subselect rounds unintentional in SAP B1
Hey everyone,
if I am executing a select-statement with a subselect in sap b1, the result of the subselect is rounded - why? If I run a query without other tables, I get the exact value.
SELECT T0.[TransId], T0.[RefDate],
(SELECT T2.Rate FROM ORTT T2 WHERE T0.RefDate = T2.RateDate AND T2.Currency = 'USD')
FROM OJDT T0 INNER JOIN JDT1 T1 ON T0.TransId = T1.TransId WHERE T1.[ShortName] = 'T0001'
T2.Rate is rounded unintentional to two decimals and so further arithmetic operations are senseless because not exact. If I run the same query with Managment Studio, I get the exact value with four decimals. Several tries with CONVERT and CAST did not solve the problem.
Any suggestions? Thank you!
Best Regards
SebastianHi,
thanks for your reply. I have tried it out and you were right.. but the problem is, that I want to export the results into excel, but there the rounded values are exported.
I have solved the problem by extending the where-clause and putting the T2.Rate into the normal select-clause.. then the values keep their format.
Best Regards
Sebastian -
How to improve 'select query with subselects'?
Oracle Database 11g Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0Hi there,
I'm a C# developer so I really have troubles with understanding PL/SQL. I use a lot of subselects in my queries because I find it easy to understand, however, I need to learn to make use of the power of queries. I made this little library sample to illustrate the problem I have in a smaller scale. I have multiple subselects looking in the same table, running through the same amount of columns to obtain data. This is a waste of performance and resources.
Idea of the query:
Information about a book
- Location of this book
- Number of unique readers of this book
- Number of days it was rent
- Number of reservation of this book
Test data
with
book as (
select 1 as id, 'reference0001' as reference, 'titel0001' as title, 'description0001' as description, 'author0001' as author, 1 as locid from dual union all
select 2 as id, 'reference0002' as reference, 'titel0002' as title, 'description0002' as description, 'author0002' as author, 1 as locid from dual union all
select 3 as id, 'reference0003' as reference, 'titel0003' as title, 'description0003' as description, 'author0003' as author, 1 as locid from dual union all
select 4 as id, 'reference0004' as reference, 'titel0004' as title, 'description0004' as description, 'author0004' as author, 2 as locid from dual union all
select 5 as id, 'reference0005' as reference, 'titel0005' as title, 'description0005' as description, 'author0005' as author, 2 as locid from dual union all
select 6 as id, 'reference0006' as reference, 'titel0006' as title, 'description0006' as description, 'author0006' as author, 2 as locid from dual union all
select 7 as id, 'reference0007' as reference, 'titel0007' as title, 'description0007' as description, 'author0007' as author, 3 as locid from dual union all
select 8 as id, 'reference0008' as reference, 'titel0008' as title, 'description0008' as description, 'author0008' as author, 3 as locid from dual
location as (
select 1 as id, '100.1' as roomnumber from dual union all
select 2 as id, '100.2' as roomnumber from dual union all
select 3 as id, '100.3' as roomnumber from dual
client as (
select 1 as id, 'client001' as name from dual union all
select 2 as id, 'client002' as name from dual union all
select 3 as id, 'client003' as name from dual
book_history as (
select 1 as bookid, 1 as clientid, 10 as days, to_date('06-06-2012') as rentdate from dual union all
select 3 as bookid, 1 as clientid, 5 as days, to_date('06-06-2012') as rentdate from dual union all
select 2 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012') as rentdate from dual union all
select 1 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012') as rentdate from dual union all
select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012') as rentdate from dual union all
select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012') as rentdate from dual union all
select 3 as bookid, 1 as clientid, 3 as days, to_date('09-06-2012') as rentdate from dual
reservation_history as (
select 1 as bookid, 1 as clientid, to_date('04-06-2012') as reservationdate from dual union all
select 3 as bookid, 1 as clientid, to_date('04-06-2012') as reservationdate from dual union all
select 2 as bookid, 2 as clientid, to_date('05-06-2012') as reservationdate from dual union all
select 1 as bookid, 2 as clientid, to_date('05-06-2012') as reservationdate from dual union all
select 2 as bookid, 1 as clientid, to_date('06-06-2012') as reservationdate from dual union all
select 2 as bookid, 1 as clientid, to_date('06-06-2012') as reservationdate from dual union all
select 3 as bookid, 1 as clientid, to_date('07-06-2012') as reservationdate from dual
)Normally my queries look like this
select b.id,
b.reference,
b.title,
b.description,
b.author,
l.roomnumber,
select count(distinct bh.clientid)
from book_history bh
where bh.bookid = b.id
and bh.rentdate >= to_date('01-01-2012')
and bh.rentdate < to_date('01-01-2013')
) as different_readers,
nvl((
select sum(bh.days)
from book_history bh
where bh.bookid = b.id
and bh.rentdate >= to_date('01-01-2012')
and bh.rentdate < to_date('01-01-2013')
),0) as days_of_rent,
select count(1)
from reservation_history rh
where rh.bookid = b.id
and rh.reservationdate >= to_date('01-01-2012')
and rh.reservationdate < to_date('01-01-2013')
) as times_reserved
from book b
join location l
on b.locid = l.id I tried to improve it to this
select b.id,
b.reference,
b.title,
b.description,
b.author,
l.roomnumber,
nvl(t1.clients, 0) as different_readers,
nvl(t1.days, 0) as days_of_rent,
select count(1)
from reservation_history rh
where rh.bookid = b.id
and rh.reservationdate >= to_date('01-01-2012')
and rh.reservationdate < to_date('01-01-2013')
) as times_reserved
from book b
left join location l
on b.locid = l.id
left join (select bh.bookid as bookid,
count(distinct bh.clientid) as clients,
sum(bh.days) as days
from book_history bh
where bh.rentdate >= to_date('01-01-2012')
and bh.rentdate < to_date('01-01-2013')
group by bh.bookid
) t1
on t1.bookid = b.id I am told that I should try to move out the group by from the last left join or try to use partitioning.
Now this is a bit too much for my understanding. When I use partitioning
select distinct
b.id,
b.reference,
b.title,
b.description,
b.author,
l.roomnumber,
nvl(count(distinct bh.clientid) over (partition by bh.bookid), 0) as different_readers,
nvl(sum(bh.days) over (partition by bh.bookid),0) as days_of_rent,
select count(1)
from reservation_history rh
where rh.bookid = b.id
and rh.reservationdate >= to_date('01-01-2012')
and rh.reservationdate < to_date('01-01-2013')
) as times_reserved
from book b
left join location l
on b.locid = l.id
left join book_history bh
on b.id = bh.bookid
and bh.rentdate >= to_date('01-01-2012')
and bh.rentdate < to_date('01-01-2013')I suppressed same result rows by using distinct, however I am told to avoid using distinct as this slows down a lot.
Is there any other solution I can use to achieve the same?
I hope my question is clear enough to be answered! Thanks in advance for your time!
Kind regards,
Metroickha
Edited by: 858378 on 6-feb-2013 2:37Thanks for providing the data in a usable format, that really helps.
858378 wrote:
I am told that I should try to move out the group by from the last left join or try to use partitioning.
Now this is a bit too much for my understanding. When I use partitioning
select b.id,
b.reference,
b.title,
b.description,
b.author,
l.roomnumber,
count(distinct bh.clientid) over (partition by bh.bookid) as different_readers,
nvl(sum(bh.days) over (partition by bh.bookid),0) as days_of_rent
from book b
left join location l
on b.locid = l.id
left join book_history bh
on b.id = bh.bookid
and bh.rentdate >= '01-01-2012'
and bh.rentdate < '01-01-2013'I get multiple rows with the same values (as expected). So what do I need to add to remove the multiple same rows?Use a DISTINCT...
SQL> ed
Wrote file afiedt.buf
1 with book as (
2 select 1 as id, 'reference0001' as reference, 'titel0001' as title, 'description0001' as description, 'author0001' as author, 1 as locid from dual union all
3 select 2 as id, 'reference0002' as reference, 'titel0002' as title, 'description0002' as description, 'author0002' as author, 1 as locid from dual union all
4 select 3 as id, 'reference0003' as reference, 'titel0003' as title, 'description0003' as description, 'author0003' as author, 1 as locid from dual union all
5 select 4 as id, 'reference0004' as reference, 'titel0004' as title, 'description0004' as description, 'author0004' as author, 2 as locid from dual union all
6 select 5 as id, 'reference0005' as reference, 'titel0005' as title, 'description0005' as description, 'author0005' as author, 2 as locid from dual union all
7 select 6 as id, 'reference0006' as reference, 'titel0006' as title, 'description0006' as description, 'author0006' as author, 2 as locid from dual union all
8 select 7 as id, 'reference0007' as reference, 'titel0007' as title, 'description0007' as description, 'author0007' as author, 3 as locid from dual union all
9 select 8 as id, 'reference0008' as reference, 'titel0008' as title, 'description0008' as description, 'author0008' as author, 3 as locid from dual
10 ),
11 location as (
12 select 1 as id, '100.1' as roomnumber from dual union all
13 select 2 as id, '100.2' as roomnumber from dual union all
14 select 3 as id, '100.3' as roomnumber from dual
15 ),
16 client as (
17 select 1 as id, 'client001' as name from dual union all
18 select 2 as id, 'client002' as name from dual union all
19 select 3 as id, 'client003' as name from dual
20 ),
21 book_history as (
22 select 1 as bookid, 1 as clientid, 10 as days, to_date('06-06-2012','DD-MM-YYYY') as rentdate from dual union all
23 select 3 as bookid, 1 as clientid, 5 as days, to_date('06-06-2012','DD-MM-YYYY') as rentdate from dual union all
24 select 2 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012','DD-MM-YYYY') as rentdate from dual union all
25 select 1 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012','DD-MM-YYYY') as rentdate from dual union all
26 select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012','DD-MM-YYYY') as rentdate from dual union all
27 select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012','DD-MM-YYYY') as rentdate from dual union all
28 select 3 as bookid, 1 as clientid, 3 as days, to_date('09-06-2012','DD-MM-YYYY') as rentdate from dual
29 )
30 select distinct
31 b.id
32 ,b.reference
33 ,b.title
34 ,b.description
35 ,b.author
36 ,l.roomnumber
37 ,count(distinct bh.clientid) over (partition by b.id) as different_readers
38 ,nvl(sum(bh.days) over (partition by b.id),0) as days_of_rent
39 from book b
40 join location l on (b.locid = l.id)
41 left outer join book_history bh on ( bh.bookid = b.id
42 and bh.rentdate >= to_date('01-01-2012','DD-MM-YYYY')
43 and bh.rentdate < to_date('01-01-2013','DD-MM-YYYY')
44 )
45* order by 1
SQL> /
ID REFERENCE TITLE DESCRIPTION AUTHOR ROOMN DIFFERENT_READERS DAYS_OF_RENT
1 reference0001 titel0001 description0001 author0001 100.1 2 15
2 reference0002 titel0002 description0002 author0002 100.1 2 15
3 reference0003 titel0003 description0003 author0003 100.1 1 8
4 reference0004 titel0004 description0004 author0004 100.2 0 0
5 reference0005 titel0005 description0005 author0005 100.2 0 0
6 reference0006 titel0006 description0006 author0006 100.2 0 0
7 reference0007 titel0007 description0007 author0007 100.3 0 0
8 reference0008 titel0008 description0008 author0008 100.3 0 0
8 rows selected.
SQL>also ensure you treat dates as dates and specify a format mask when using TO_DATE. -
Hi,
what's the best way to do a query like this
UPDATE
table
WHERE id = (
SELECT
id
FROM
table
WHERE
<condition>
ORDER BY id
LIMIT 1
)in EJB 2.1 while taking into consideration that subselects are not transaction safe? If you have any idea, please tell me :-)
Thanks
-DannyHm... looks, better, right! :)
But I guess this can't be done using EJBQL. Is is race condition safe to
write a finder with the join part like this:
SELECT OBJECT(a) FROM tableA AS a, tableB as B
WHERE a.id = b.id AND a.reservedForID = 0 AND b.<col> = <condition>and then do a
TableAValue aVal= aLocal.findBy...();
aVal.setReservedForID(id);
aLocal.update(aVal);without havind a second thread finding the exact same entry? Is it correct,
that the container has to lock the entry that is returned? -
Hi,
i got an error that the format of the statement isn't correct. How can i solve this problem using dynamic select AND subselect?
data: column_stmt type string,
from_stmt type string,
where_stmt type string.
column_stmt = 'ko~aufnr ko~plnbez po~psmng po~amein po~wemng ko~gstri ko~gsuzi ko~gstrs ko~gltrs kt~maktx'.
from_stmt = 'afko AS ko'
& ' INNER JOIN afpo AS po ON ko~aufnr = po~aufnr'
& ' INNER JOIN aufk AS fk ON ko~aufnr = fk~aufnr'
& ' INNER JOIN makt AS kt ON ko~plnbez = kt~matnr'.
where_stmt = 'kt~spras = sy-langu'
& 'AND po~posnr = ('
& ' SELECT MIN( posnr ) FROM afpo'
& ' WHERE aufnr = ko~aufnr'
SELECT (column_stmt)
FROM (from_stmt)
INTO CORRESPONDING FIELDS OF TABLE re_results
WHERE (where_stmt)
Thanks for help,
O. Cramerdata: column_stmt type string,
from_stmt type string,
where_stmt type string.
column_stmt = 'ko~aufnr ko~plnbez po~psmng po~amein po~wemng ko~gstri ko~gsuzi ko~gstrs ko~gltrs kt~maktx'.
from_stmt = 'afko AS ko'
& ' INNER JOIN afpo AS po ON ko~aufnr = po~aufnr'
& ' INNER JOIN aufk AS fk ON ko~aufnr = fk~aufnr'
& ' INNER JOIN makt AS kt ON ko~plnbez = kt~matnr'.
where_stmt = 'kt~spras = sy-langu'
& *' AND* po~posnr = *( *'
& ' SELECT MIN( posnr ) FROM afpo'
& ' WHERE aufnr = ko~aufnr'
SELECT (column_stmt)
FROM (from_stmt)
INTO CORRESPONDING FIELDS OF TABLE re_results
WHERE (where_stmt) -
NEED HELP STRUCTURING A SUBSELECT - INVALIS IDENTIFIER ERROR
This is my 1st post to this PL/SQL forum. I've used TSQL up to now; first job with PL/SQL and Oracle DB. I'm getting a "INVALID IDENTIFIER" error on the ORDER BY line (next to last line in code) in the following code. I haven't doen a lot of subselects and none with grouping. All I'm trying to do is get a count of states my customers are in... Any ideas what might be causing the problem, or a better way to structure this? (I tried the subselect in the SELECT statement as well with no luck there either)
-- count and list All states by state zipcode resolution
SELECT st.state, COUNT(*) AS count_by_state
FROM ADM.USR u
INNER JOIN
SELECT
CASE
WHEN zipcode BETWEEN 99501 AND 99950 THEN 'AK' -- Alaska
WHEN zipcode BETWEEN 35004 AND 36925 THEN 'AL' -- Alabama
WHEN zipcode BETWEEN 71601 AND 72959 THEN 'AR' -- Arkansas
WHEN zipcode BETWEEN 75502 AND 75502 THEN 'AR' -- Arkansas( Texarkana)
WHEN zipcode BETWEEN 85001 AND 86556 THEN 'AZ' -- Arizona
WHEN zipcode BETWEEN 82001 AND 83128 THEN 'WY' -- Wyoming
ELSE NULL
END AS state
FROM CUST.USR_PRFL
ORDER BY state) st ON u.usr_id = st.usr_id
GROUP BY st.state;
Edited by: 927929 on Apr 16, 2012 7:05 AM
Edited by: 927929 on Apr 16, 2012 7:07 AMtry
SELECT st.state, COUNT(*) AS count_by_state
FROM ADM.USR u
INNER JOIN CUST.USR_PRFL up ON u.usr_id = up.usr_id
INNER JOIN
SELECT
CASE
WHEN zipcode BETWEEN 99501 AND 99950 THEN 'AK' -- Alaska
WHEN zipcode BETWEEN 35004 AND 36925 THEN 'AL' -- Alabama
WHEN zipcode BETWEEN 71601 AND 72959 THEN 'AR' -- Arkansas
WHEN zipcode BETWEEN 75502 AND 75502 THEN 'AR' -- Arkansas( Texarkana)
WHEN zipcode BETWEEN 85001 AND 86556 THEN 'AZ' -- Arizona
WHEN zipcode BETWEEN 82001 AND 83128 THEN 'WY' -- Wyoming
ELSE NULL
END AS state
FROM CUST.USR_PRFL
--ORDER BY state
) st
-- any relation beetwen? like "ON u.usr_id = up.usr_id" above
GROUP BY st.state
ORDER BY state; -
Variable REPORTQUERY is undefined.
<cfstoredproc datasource="MyDSN" procedure = "MySP">
<cfprocparam value = "1" CFSQLTYPE = "cf_sql_numeric">
</cfstoredproc>
for my report query using a stored procedure need parameter.
I got Variable REPORTQUERY is undefined.
I notice that the default name for query report is ReportQuery, but CFSTOREDPROC doex not have name property.
I am not sure how I can assign the variable using a stored procedure.
Your help and information is great appreciated,
Regards,
Iccsi,it works if I use CFQUERY,
since CFQUERY can be named and can have execute stored procedure syntax,
Regards,
Iccsi,
Maybe you are looking for
-
What type of external hard drive should I get?
First, I'm not sure were I should have posted this question but here goes.... I am officially 100% an Apple home environment now. I have an iMac, a Macbook Pro, and an Airport Extreme. I want to add an external hard drive and I need suggestions for w
-
Why can't I use the mesh tool? - brand new illustrator user
I am trying to learn to use illustrator - I've been watching the online videos - which are very helpful, but there aren't any that I can find for the mesh tool. I want to add some shading to the body of this acrobat but every time I try to use the m
-
Setting width/height of a control with CSS
Hi, I am styling a button a button in CSS and set a background-image for the button in my stylesheet. The problem is, that the button should scale to the width/height of the image. I added: -fx-width: 32px; -fx-height: 32px; but it has no effect. I a
-
Intermedia Status Invalid after 10.1.0.4 patchset
I just installed the 10.1.0.4 patche set 2. At the end of the 7.4.2 Upgrade the Release 10.1 Database it shows Oracle interMedia's status is INVALID. How can I find out what's causing the INVALID status and more importantly how can I fix it? Thanks i
-
Thanks.