Order by in subselect
Hello,
when using a ORDER BY clause in a subquery, i get the following error:
Error -
Auto Commit: On, SQL Mode: Internal, Isolation Level: Committed
General error;-5016 POS(286) Missing delimiter: )
SELECT *
FROM DOCPROTOCOL D
WHERE D.DOCID = (
SELECT DP.DOCID
FROM DOCPROTOCOL DP
WHERE DP.DOCID = 5
AND DP.ACTIONSYM=10
ORDER BY DP.XTIME ASC
LIMIT 1
The "ORDER"-Keyword is marked.
Trying the same request without the order-by part works fine, but naturally returns something different - which is not the result I want.
(I know, that the above sample is kind of useless - the "real" statement is more complex, so I tried to reduce it to a minimum.)
Are ORDER BY's not supported in subqueries? If so, are there any alternatives? Or did I overlook something?
Thanks in advance.
nobodyknows wrote:>
> Hello,
>
> when using a ORDER BY clause in a subquery, i get the following error:
>
> -
Error -
> Auto Commit: On, SQL Mode: Internal, Isolation Level: Committed
> General error;-5016 POS(286) Missing delimiter: )
> SELECT *
> FROM DOCPROTOCOL D
> WHERE D.DOCID = (
> SELECT DP.DOCID
> FROM DOCPROTOCOL DP
> WHERE DP.DOCID = 5
> AND DP.ACTIONSYM=10
> ORDER BY DP.XTIME ASC
> LIMIT 1
> )
>
> The "ORDER"-Keyword is marked.
> Trying the same request without the order-by part works fine, but naturally returns something different - which is not the result I want.
> (I know, that the above sample is kind of useless - the "real" statement is more complex, so I tried to reduce it to a minimum.)
>
> Are ORDER BY's not supported in subqueries? If so, are there any alternatives? Or did I overlook something?
No ORDER BY in subqueries aren't supported.
From what I see in your example something like this should work even better:
SELECT *
FROM DOCPROTOCOL D
WHERE D.DOCID = (
SELECT max(DP.DOCID)
FROM DOCPROTOCOL DP
WHERE DP.DOCID = 5
AND DP.ACTIONSYM=10
This is better because:
- it makes clear what you want to do
- it prevents useless sort operations
- it enables max/min optimizations if the DOCID is a indexed field or key column
regards,
Lars
Similar Messages
-
Top N query giving error for oracle 8.0.6
Dear All,
We are executing this query SELECT XBLNR, WERKS, MATNR, MDV01, BACKFLQUANT, STATUS, SAPTIMESTAMP, PITSTIMESTAMP, PMTIMESTAMP, BATCH FROM (SELECT XBLNR, WERKS, MATNR, MDV01, BACKFLQUANT, STATUS, SAPTIMESTAMP, PITSTIMESTAMP, PMTIMESTAMP, BATCH FROM PMBPITS.PITS_UNITY WHERE STATUS = '01' ORDER BY PMTIMESTAMP) WHERE ROWNUM < 20
on oracle 8.0.6 but this is giving the following error
ora - 00907 missing right parenthesis error
1. Is it that in the inner select we cannot use order by and where clause together.
2. We also found that if we remove order by from inner select then the query is not giving error
pls help . points will be awardedHi,
what ever the Aman said is correct. You check this is supported in 8.1.5, SQL allows you to embed the ORDER BY clause in a subquery and place the ROWNUM condition in the top-level query;
'Top-N query' is a ORACLE 8i feature which is supported in SQL. However,
Bug:855720 states the following:
"PL/SQL does not support top-N queries (ORDER BY in SUBSELECT/SUBQUERY
or VIEW. Since this feature is available in SQL, but not in PL/SQL,
it has been logged as a Bug that will be fixed in 8.1.6."
- Pavan Kumar N -
HI all
desc table rg_types
LIDER number(5),
SERIAL_N number(8),
CODE_T number(3)
TYPE_I number (1),
SHIPMENT_NUM number(4)
no p.k on table or unique index.
TYPE_I can contain values range [0-5].
for every lider can be sug_i [0-5] ,
select * from rg_type order by SHIPMENR_NUM,TYPE_I,LIDER
LIDER SERIAL_N CODE_T TYPE_I SHIPMENT_NUM
11 956307 1 0 564
11 956308 1 0 564
11 956309 1 1 564
400 956310 3 0 564
500 956311 1 2 564
600 956312 1 5 564
600 956313 1 5 564
200 956314 1 0 565
400 956315 3 4 565
700 956316 1 5 565
notice that not all the lider appears in every shipment_num , and also every lider doe'nt have to has all the type_i every shipment_num (could be, but doesn't have to be)
i need to perform a query that will show for all the lider how
many types from each type (0-5)
spomething like that
TYPE_I
LIDER 0 1 2 4 5
11 2 1
200 1
400 1 1
500 1
600 2
700 1
please help
thanks in advanced
NaamaMissing expression because the column expression before from was (sorry, my mistake) terminated by a comma
select lider,
max(decode(type_i,0,the_count)) count_0,
max(decode(type_i,1,the_count)) count_1,
max(decode(type_i,2,the_count)) count_2,
max(decode(type_i,3,the_count)) count_3,
max(decode(type_i,4,the_count)) count_4,
max(decode(type_i,5,the_count)) count_5 /* a comma was left here by mistake */
from (select lider,type_i,count(*) the_count
from rg_type
group by lider,type_i
group by lider
order by liderthe subselect gets the counts you are looking for and your problem might be already solved
but
you want each lider's type_i count appear as a column of that lider's row (bringing rows to columns is called pivoting)
to achieve it you first
select lider,
decode(type_i,0,the_count) count_0,
decode(type_i,1,the_count) count_1,
decode(type_i,2,the_count) count_2,
decode(type_i,3,the_count) count_3,
decode(type_i,4,the_count) count_4,
decode(type_i,5,the_count) count_5
from (select lider,type_i,count(*) the_count
from rg_type
group by lider,type_i
order by liderand then use max to get rid of all those null values obtaining a single row for each lider value
Regards
Etbin
p.s. I don't have database access to provide error free detailed examples -
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 -
Order BY with ROWNUM in Select List
Hi,
I have query like bellow for Oracle 11g:
create table em_c (vl_s_ec int,id_em_em int,id_empcre_ecr int, id_em_ec int);
create table em (id_em_em int)
Select e.id_em_em
, NVL(
(SELECT * FROM(SELECT a.vl_s_ec FROM em_c a WHERE a.id_em_em = ec.id_em_em
AND a.id_empcre_ecr < ec.id_em_ec order by a.id_em_ec DESC) WHERE ROWNUM <= 1) ,0) AS col2
FROM em_c ec
INNER JOIN em e on ec.id_em_em = e.id_em_em
WHERE 1 = 1;
But inner subselect doesn't see table "em_c ec" from outer FROM clause. That's why following error occurs:
SQL Error: ORA-00904: "EC"."ID_EM_EC": invalid identifier
Please help me to make it work.
Edited by: user11290901 on Dec 17, 2010 2:11 AMCorrelation names only go one level deep. It's a bit tough to decipher your query so if you want a better response please post the following:
1. Oracle version (SELECT * FROM V$VERSION)
2. Sample data in the form of CREATE / INSERT statements.
3. Expected output
4. Explanation of expected output (A.K.A. "business logic")
5. Use \ tags for #2 and #3. See FAQ (Link on top right side) for details. -
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 -
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? -
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; -
URGENT HELP: SQL ordering
Hi there
I have a set of records that I want to order using SQL. I want to sort them ascendingly by a numeric field. The trouble is that I want them ascending excluding the fields equalling zero. The fields with a value of zero should be listed last.
Is there any way to achieve this?
Hope someone can help me soon. It is incredibly urgent!!
ComlinkHi there
I have a set of records that I want to order using
SQL. I want to sort them ascendingly by a numeric
field. The trouble is that I want them ascending
excluding the fields equalling zero. The fields with
a value of zero should be listed last.
Is there any way to achieve this?This is probably a hack, but I can't figure out the subselect.
SELECT field1, field2 FROM table ORDER BY field1 WHERE field1 != 0;
SELECT field1, field2 FROM table WHERE field1 = 0; -
Subselect cannot access columns of main-select?
Dear experts!
First I had a rather simple statement
SELECT FPS_Main.NACHNAME, FPS_Main.VORNAME, FPG_Main.NAME AS "Abteilung", FPS_Main.DK_ORGA_BEREICH AS "Orgabereich d. Pers.", FPG_Main.DK_ORGA_BEREICH AS "Orgabereich der Abt."
FROM FPS FPS_Main, FPG FPG_Main
WHERE FPS_Main.FK_FPG_NR = FPG_Main.PK_FPG_NR
AND FPS_Main.DK_ORGA_BEREICH = 'AL'
AND FPG_Main.DK_ORGA_BEREICH = 'MZ'
ORDER BY FPS_Main.NACHNAME
which produces a table like this:
first name*last name*department*organisational area of person*organisational area of department
Then I wanted to add two columns to this statement, the first listing the number of publications of each person for 2008, the second listing the number of publications of each person for 2009. I tried to do it like that
SELECT FPS_Main.NACHNAME, FPS_Main.VORNAME, FPG_Main.NAME AS "Abteilung", FPS_Main.DK_ORGA_BEREICH AS "Orgabereich d. Pers.", FPG_Main.DK_ORGA_BEREICH AS "Orgabereich der Abt.", Sub1.Anzahl_2008, Sub2.Anzahl_2009
FROM FPS FPS_Main, FPG FPG_Main, (
SELECT COUNT(FPB_Sub1.FK_FPR_NR) AS Anzahl_2008
FROM FPB FPB_Sub1, FPR FPR_Sub1, FPN FPN_Sub1, FPS FPS_Sub1
WHERE FPB_Sub1.FK_FPR_NR = FPR_Sub1.PK_FPR_NR
AND FPB_Sub1.FK_FPN_NR = FPN_Sub1.PK_FPN_NR
AND FPR_Sub1.FK_FPS_NR = FPS_Main.PK_FPS_NR
AND FPN_Sub1.PUBLIKATION_JAHR = 2008
) Sub1,
SELECT COUNT(FPB_Sub2.FK_FPR_NR) AS Anzahl_2009
FROM FPB FPB_Sub2, FPR FPR_Sub2, FPN FPN_Sub2, FPS FPS_Sub2
WHERE FPB_Sub2.FK_FPR_NR = FPR_Sub2.PK_FPR_NR
AND FPB_Sub2.FK_FPN_NR = FPN_Sub2.PK_FPN_NR
AND FPR_Sub2.FK_FPS_NR = FPS_Main.PK_FPS_NR
AND FPN_Sub2.PUBLIKATION_JAHR = 2009
) Sub2
WHERE FPS_Main.FK_FPG_NR = FPG_Main.PK_FPG_NR
AND FPS_Main.DK_ORGA_BEREICH = 'AL'
AND FPG_Main.DK_ORGA_BEREICH = 'MZ'
ORDER BY FPS_Main.NACHNAME
using two times the same subselect as tables, but with different aliases. But that is giving me an ORA-00904 - invalid identifier. What did I do wrong, and how can I correct it?
Next I tried it like this, with only one column first
SELECT FPS_Main.NACHNAME, FPS_Main.VORNAME, FPG_Main.NAME AS "Abteilung", FPS_Main.DK_ORGA_BEREICH AS "Orgabereich d. Pers.", FPG_Main.DK_ORGA_BEREICH AS "Orgabereich der Abt.", COUNT(
SELECT FPB_Sub1.FK_FPR_NR
FROM FPB FPB_Sub1, FPR FPR_Sub1, FPN FPN_Sub1
WHERE FPB_Sub1.FK_FPR_NR = FPR_Sub1.PK_FPR_NR
AND FPB_Sub1.FK_FPN_NR = FPN_Sub1.PK_FPN_NR
AND FPR_Sub1.FK_FPS_NR = FPS_Main.PK_FPS_NR
AND FPN_Sub1.PUBLIKATION_JAHR = 2008
FROM FPS FPS_Main, FPG FPG_Main
WHERE FPS_Main.FK_FPG_NR = FPG_Main.PK_FPG_NR
AND FPS_Main.DK_ORGA_BEREICH = 'AL'
AND FPG_Main.DK_ORGA_BEREICH = 'MZ'
ORDER BY FPS_Main.NACHNAME
giving me an ORA-00936 - missing expression. Myself I never tried to use a (sub)-SELECT within an aggregate function, but I saw it somewhere else and thought, that it could be a good idea for this case, but seems its not working - what did I do wrong here?
Thank You very much in advance!
With kind regards,
ChrissHi Urs (& Hi Frank)!
Thank You very much :-D
This advice was not only fully correct, but also it guided me to a big mistake in my statement. Now I use something like that
SELECT FPS_Main.NACHNAME AS "Nachname", FPS_Main.VORNAME AS "Vorname", FPG_Main.NAME AS "Abteilung", FPS_Main.DK_ORGA_BEREICH AS "Orgabereich d. Pers.", FPG_Main.DK_ORGA_BEREICH AS "Orgabereich der Abt.", (
SELECT COUNT(FPB_Sub1.FK_FPR_NR)
FROM FPB FPB_Sub1, FPR FPR_Sub1, FPN FPN_Sub1
WHERE FPB_Sub1.FK_FPR_NR = FPR_Sub1.PK_FPR_NR
AND FPB_Sub1.FK_FPN_NR = FPN_Sub1.PK_FPN_NR
AND FPR_Sub1.FK_FPS_NR = FPS_Main.PK_FPS_NR
AND FPN_Sub1.PUBLIKATION_JAHR = 2007
) AS "Publikationen 2007",
FROM FPS FPS_Main, FPG FPG_Main
WHERE FPS_Main.FK_FPG_NR = FPG_Main.PK_FPG_NR
AND FPS_Main.DK_ORGA_BEREICH = 'AL'
AND FPG_Main.DK_ORGA_BEREICH = 'MZ'
ORDER BY FPS_Main.NACHNAME
and it is working perfectly - with a scalar subselect...
With kind regards,
Chriss -
Performance issue with ORDER BY
The following query runs in 60 milli sec with no order by clause. With order by it takes about 3 sec. The end result of the query is 25 records. One thing we noticed is order by is taking place after the union irrespective where we place the order by clause. Not sure how to make oracle perform order by on end result. Any thoughts truly appreciated.
SELECT *
FROM
(SELECT ROWNUM as R,
STATUS,
TASK_ID,
PRIORITY,
INSTALL_TYPE,
CUSTOMER_TYPE,
TYPE_ID,
QUEUE,
FALLOUTREASON,
START_DT,
NOTIF_CREATE_DT,
ACCT_NUM,
ADSLTN,
SDPID,
PERFORMER_DN,
SUPERVISOR_DN
FROM
(SELECT *
FROM
(SELECT *
FROM
(SELECT t.STATUS,
TASK_ID,
PRIORITY,
INSTALL_TYPE,
CUSTOMER_TYPE,
S.TYPE_ID,
QUEUE,
null FALLOUTREASON,
START_DT,
null NOTIF_CREATE_DT,
null ACCT_NUM,
ADSLTN,
null SDPID,
PERFORMER_DN,
SUPERVISOR_DN
FROM (select * from TASK_ACTIVITY where TYPE_ID='2' AND SUB_TYPE_ID='2' AND status not in ( 'Terminated','Complete') ) T,
SOEG_CRM S
WHERE t.activity_id= s.activity_id
UNION ALL
(SELECT t.STATUS,
TASK_ID,
PRIORITY,
null INSTALL_TYPE,
null CUSTOMER_TYPE,
TYPE_ID,
QUEUE,
'Review Letters for Printing' FALLOUT_REASON,
START_DT,
null NOTIF_CREATE_DT,
null ACCT_NUM,
null ADSLTN,
null SDPID,
PERFORMER_DN,
SUPERVISOR_DN
FROM (select * from TASK_ACTIVITY where TYPE_ID='3' AND status not in ( 'Terminated','Complete') ) T
UNION ALL
(SELECT t.STATUS,
TASK_ID,
PRIORITY,
INSTALL_TYPE,
CUSTOMER_TYPE,
op.TYPE_ID,
QUEUE,
FALLOUT_REASON,
START_DT,
NOTIF_CREATE_DT,
ACCT_NUM,
ADSLTN,
SDPID,
PERFORMER_DN,
SUPERVISOR_DN
FROM (select * from TASK_ACTIVITY where TYPE_ID='1' AND SUB_TYPE_ID='1' and status not in ( 'Terminated','Complete') ) T,
OPNOTES OP
WHERE t.activity_id= op.activity_id
UNION ALL
(SELECT t.STATUS,
TASK_ID,
PRIORITY,
INSTALL_TYPE,
CUSTOMER_TYPE,
a.TYPE_ID,
QUEUE,
null FALLOUTREASON,
START_DT,
null NOTIF_CREATE_DT,
null ACCT_NUM ,
ADSLTN,
null SDPID,
PERFORMER_DN,
SUPERVISOR_DN
FROM (select * from TASK_ACTIVITY where TYPE_ID='2' AND SUB_TYPE_ID='1' and status not in ( 'Terminated','Complete') ) T,
AELERA a
WHERE t.activity_id= a.activity_id
WHERE (PERFORMER_DN = 'cadmin1'
OR SUPERVISOR_DN in ('Address_Issues','PROCESSED','SG_EOSN','Test_Queue','Test_Queue_P','BUTST1','BUTST2','BU_UNAS','DATAR','PST','PST_ME','SVC','SVC_EX','SVC_ME','TEST_2_LETTER_QUEUE','TEST_2_OP_ESC_QUEUE','TEST_2_PROV_ESC_QUEUE','TEST_3','Test_4','Test_6','Test_8','test_5_LETTER_QUEUE','test_5_OP_ESC_QUEUE','test_5_PROV_ESC_QUEUE','cadmin1'))
AND ((STATUS = 'Pending'
AND PERFORMER_DN = QUEUE)
OR STATUS = 'Suspended'
OR STATUS = 'Acquired'
OR (STATUS = 'Pending'
AND PERFORMER_DN <> QUEUE))
AND (TYPE_ID = '2'
OR TYPE_ID = '3')
--order by priority
WHERE R >= 1
and R < 26
--order by priority
Here is the explain plan
19 SELECT STATEMENT
18 . VIEW
17 SORT [ORDER BY]
16 COUNT
15 . VIEW
14 UNION-ALL
4 NESTED LOOPS
1 APPL.TASK_ACTIVITY TABLE ACCESS [FULL]
3 APPL.SOEG_CRM TABLE ACCESS [BY INDEX ROWID]
2 APPL.XPK_SOEG_CRM INDEX [UNIQUE SCAN]
5 APPL.TASK_ACTIVITY TABLE ACCESS [FULL]
9 NESTED LOOPS
6 APPL.BSOMS_OPNOTES TABLE ACCESS [FULL]
8 APPL.TASK_ACTIVITY TABLE ACCESS [BY INDEX ROWID]
7 APPL.XPKTASK_ACTIVITY INDEX [UNIQUE SCAN]
13 NESTED LOOPS
10 APPL.TASK_ACTIVITY TABLE ACCESS [FULL]
12 APPL.AELERA TABLE ACCESS [BY INDEX ROWID]
11 APPL.XPK_AELERA INDEX [UNIQUE SCAN]Hi,
I must admit, that I didn't read your whole query, but did you try to use a subselect?
Select a.* from (your_query) a
order by a.priorityBTW: Don't use SELECT *
Dim -
Open Sales Orders Backlog Report
Hi All: I have been asked to create Backlog reports for Open Sales Orders and the revenue amounts for the next 15days, one month, 2 months, 3 months 6 months etc etc. Does anyone know of a standard report that covers that and the business content and cues that go with it?
Thanks in advance
BobbyI haven't seen any standard content for it, with my client, we actually developed the open order report from Item extractor and status extractor. And compare the created on date to the system data to see how long it has been opened for and put it in different buckets.
Hopes that help.
thanks.
Wond. -
Need a report on open order aging. The open order means not delivered or partially delivered. Further with days range since its open or not fully delievered i.e. > 15 days or 15-30 days and so on.
The standard transactions Va05 and VL10c can provide the list but dont provide any aging info.
Kindly help in this.
thanks
anuDear Anu
1. First in VA05 you can use Variants and get the report as desired by you,create one variant and use Filter along with greater then, less then (all are in selection option icon)
This way you can create three variants.
But limitation is dates has to be manually changed in variants each time.
2. Try this Tcodes
S_ALR_87014387 Display Document Flow
S_ALR_87014392 Display Document Flow
This reports will give you document flow run report with ticking checkbox for sales order,delivery and goods isse then after getting the list expand all (Shift+F12)
you will get the quantities for sales order, and what is delivered and what is issued.
3. As such if you want exactly the report you can take help of ABAP to create the ALV
4. Report or create Queries in SQVI , or else create MCSI report
Regards
Jitesh -
Necessary Fields For Creation of Service PO of Order Type Relaese Order.
Dear Guru,
I have encountered an issue which i am trying to resolve...
My this requirment will seem little okward the way i am asking but i have no way...
The issue is I have to create a Service PO of Order type Release order (RO) using BAPI Function Module .BAPI_PO_CREATE1.
The service PO should be of multiple Item and services for particular line item should be multiple.
When I am creating this using ME21 or ME21N i am facing no issue.
But when i am using BAPI Function Module .BAPI_PO_CREATE1
i am getting following errors ;; The error which i am getting as below
T ID NUM MESSAGE
E BAPI 001 No instance of object type PurchaseOrder has been created. External reference:
E MEPO 000 Purchase order still contains faulty items
E SE 029 Please maintain services or limits
E SE 140 Service HIRING OF LCD: please specify unit of measure
But I am failing to findout in which field services or limits and unit of measurement have to maintain.
What are the necessary fields have to pass in Bapi import parameter and the table i am unable to findout.
Please show some way how to resolve this or give me some guideline to resolve this
Dear Moderator request your kind intervane to move this qurry into correct forum if i have asked this in wrong forum
Thanks and regards
saifur rahamanHi Saifur
Can you please elaborate how did you resolve the issue we are also facing same problem when we are trying to create the PO for service items through SRM it is throwing same error while creating the PO in SAP.
Email Id : [email protected]
Thank you in advance!!
Regards
Deepika
Maybe you are looking for
-
How do I transfer purchased apps from my iPhone 5s to iTunes library?sse
I'm trying to update my 5s to OS 8. I've gotten a pop up window that states I have purchased apps that are not stored in my iTunes library. How do I transfer apps from my iPhone to my iTunes library?
-
Photoshop display issue with Windows 8
When running Photoshop elements 12 on my laptop running Windows 8 with high resolution, the menu items, etc. is so small I am unable to use the program. Will upgrading to 13 resolve the problem? Lenovo laptop with 3200 x 1800 resolution.
-
A new Macbook Air only has 2 hours battery??
I bought my 13" macbook air for less than two weeks. When I plug in the charger and have the battery charged to 100%, I found that there're only 2:xx remaining time. How can I fix the problem? Or do i need to have my battery charged for longer period
-
I did something and now my scans comes up with the following: File conversion - Scan_20150125.jpg Text encoded - select the Encoding that makes document readable I was scanning to PDF and after I changed to JPEG the problem started I have a HP Office
-
Easy setup for Digibeta?
I realize this is probably really simple, but: I need to get my sequence out to Digi and every time I try the edit to tape i'm told i have "Incompatible compression settings"? I'm on FCP 6 going to a sony DVW-M2000P through a AJA IO (SD) and i'm usin