Order by with Union
select level as lvl --,1 as ordered from dual connect by level <=5 union all select rownum as lvl --,2 as ordered from dual connect by level <=5
How to order the query after the UNION ALL clause in descending order
My expected results
1
2
3
4
5
5
4
3
2
1
Select * from (select level as lvl-, 1 as ordered
of the double
connect by level<>
order by 1)
Union of all the
Select * from (select rownum as -lvl 2 as he ordered
of the double
connect by level<>
order by 1 desc)
Tags: Database
Similar Questions
-
order by with analytic function
Hi gurus
Need your help again.
I have the following data.
Examples of data
Select * from
(
As with a reference
(
Select ' 100 ', ' 25' grp lb, to_date('2012-03-31') ter_dt, 'ABC' package_name FROM DUAL union all
Select ' 100 ', ' 19', to_date ('2012-03-31'), 'AA' OF the whole union DOUBLE
Select ' 200 ', ' 25', to_date('2012-03-31'), 'CC' FROM DUAL union all
Select ' 300 ', ' 28', to_date('2012-03-31'), 'XX' from DUAL union all
Select ' 300 ', ' 28', to_date('4444-12-31'), 'XY' from DUAL
)
Select the grp, lb, ter_dt, Package_name
ROW_NUMBER() over (partition by order of grp by case when lb = '19' then 1)
When lb = '25' then 2
ro_nbr end)
Reference)
-where ro_nbr = 1
;
-----------
The query above returns the following result:
Existing query result
GRP LB TER_DT package_name RO_NBR
100 19 03/12/31 AA 1 100 25 03/12/31 ABC 2 200 25 03/12/31 CC 1 300 28 03/12/31 XX 1 300 28 44 12-31 XY 2 If you can see the data above then I use the order clause with function row_number analytic and prioritize data according to LB using the order by clause.
Now the problem is I need simple stored against each group so I write the following query:
Query
Select * from
(
As with a reference
(
Select ' 100 ', ' 25' grp lb, to_date('2012-03-31') ter_dt, 'ABC' package_name FROM DUAL union all
Select ' 100 ', ' 19', to_date ('2012-03-31'), 'AA' OF the whole union DOUBLE
Select ' 200 ', ' 25', to_date('2012-03-31'), 'CC' FROM DUAL union all
Select ' 300 ', ' 28', to_date('2012-03-31'), 'XX' from DUAL union all
Select ' 300 ', ' 28', to_date('4444-12-31'), 'XY' from DUAL
)
Select the grp, lb, ter_dt, Package_name
ROW_NUMBER() over (partition by order of grp by case when lb = '19' then 1)
When lb = '25' then 2
ro_nbr end)
Reference)
where ro_nbr = 1
;
The query result
GRP LB TER_DT RO_NBR
100 19 03/12/31 AA 1 200 25 03/12/31 CC 1 300 28 03/12/31 XX 1 My required result is that 300 GRP contains 2 folders and I need the record with the latest means of ter_dt and right now, I only get the latest.
My output required
GRP LB TER_DT RO_NBR
100 19 03/12/31 AA 1 200 25 03/12/31 CC 1 300 28 44 12-31 XY 1 Please guide. Thank you
Hello
The query you posted is the ro_nbr assignment based on nothing other than lb. When there are 2 or more lines that have an equal claim to get assigned ro_nbr = 1, then one of them is chosen arbitrarily. If, when a tie like that occurs, you want the number 1 to be assigned based on some sort, and add another expression of Analytics ORDER BY clause, like this:
WITH got_ro_nbr AS
(
SELECT the grp, lb, ter_dt, nom_package
ROW_NUMBER () OVER (PARTITION BY grp
ORDER OF CASES
WHEN lb = '19' THEN 1
WHEN lb = '25' THEN 2
END
, ter_dt DESC-* NEW *.
) AS ro_nbr
REFERENCE
)
SELECT the grp, lb, ter_dt, nom_package
OF got_ro_nbr
WHERE ro_nbr = 1
;
-
Dear all,
I have two tables
TABLE A (A_DATE, COLA1, COLA2)
TABLE B (B_DATE)
A_DATE = B_DATE
Examples of tables:
TABLE A (11 selected lines)
A_DATE COLA1 COLA2 03/01/2013 54 VAL76 04/01/2013 11 VAL78 04/01/2013 11 VAL22 04/01/2013 74 VAL22 04/02/2013 15 VAL45 04/02/2013 11 VAL22 04/03/2013 44 VAL22 04/03/2013 71 VAL73 04/04/2013 99 VAL13 04/05/2013 11 VAL22 04/05/2013 11 VAL23 TABLE B (01 April-30 April)
B_DATE 04/01/2013 04/02/2013 04/03/2013 04/04/2013 (condition 1: month: April)
AND
condition 2: CALA1-> 11
AND
condition 3: COLA2-> VAL22)
I need to display all records for the months April to TABLE A,.
If the documents are not present in TABLE A for every 30 days, I need to display null, by reading DATE information in TABLE B
Expected results (sample)
Date COLA1 COLA2 04/01/2013 11 VAL22 04/02/2013 11 VAL22 04/03/2013 04/04/2013 04/05/2013 11 VAL22 Currently I have the following query
Select A_DATE, COLA1, COLA2 from TABLEA
where COLA1 = 11 and COLA2 in ('VAL22') and A_DATE between ' 2013-04-01 00:00:00.0' AND ' 00:00:00.0' 2013-04-30
Union of all the
Select B_DATE, null, null from TABLEB if date between ' 2013 - 04 - 01 00:00:00.0' AND ' 00:00:00.0' 2013-04-30
But I'm the duplicate records for the values in TABLE a.
Output current (sample)
Date COLA1 COLA2 04/01/2013 11 VAL22 04/01/2013 04/02/2013 11 VAL22 04/02/2013 04/03/2013 04/04/2013 04/05/2013 11 VAL22 04/05/2013 I tried with UNION and UNION ALL, but getting the same result.
Hello
Try this:
Select b.B_DATE, a, COLA1, one, COLA2
from TABLEB b
the left join TABLEA a
On a.A_DATE = b.B_DATE
and COLA1 = 11
and COLA2 in ('VAL22')
where B_DATE between to_date('2013-04-01','yyyy-mm-dd') AND to_date('2013-04-30','yyyy-mm-dd')
----
Ramin Hashimzade
-
with the clause with union all?
Hello
I tried to use 'by' article with union all. .but it gives me error...
What harm am I doing here? Thank you very much!!with t1 as (select '1'col1 from dual union all select '2' col2 from dual)select * from t1 union with t2 as (select '3' col1 from dual union all select '4' col1 from dual ) select * from t2
Use the WITH clause, once for you all the subqueries, and then add the main request;
with t1 as (select '1' col1 from dual union all select '2' col2 from dual), t2 as (select '3' col1 from dual union all select '4' col1 from dual) select * from t1 union select * from t2;
-
Query in the Order byclause used with UNION
Tryna qurey below when I get
ORA-01784 - ORDER BY item must include the number of an expression in the SELECT list
Please help me to solve this problem as soon as possible.
SELECT cm_workqueue. WQUEUEID_REF, cm_workqueue. WQUEUEID_CODE, cm_workqueue.mstr_queue_encode, cm_workqueue.desc_text
OF cm_workqueue.
cm_wkqueue_role,
atrt_role
WHERE cm_workqueue. MSTR_QUEUE_ENCODE = 10000000
AND cm_workqueue. WQUEUEID_REF = cm_wkqueue_role. WQUEUEID_REF
AND atrt_role. ROLEID_REF = cm_wkqueue_role.roleid_ref
AND cm_wkqueue_role. REFERRAL_YORN_ENCODE = 10000000
AND cm_workqueue. STATUS_REF = 10000000
AND cm_wkqueue_role. STATUS_REF = 10000000
UNION
SELECT cm_workqueue. WQUEUEID_REF, cm_workqueue. WQUEUEID_CODE, cm_workqueue.mstr_queue_encode, cm_workqueue.desc_text
OF cm_workqueue.
cm_wkqueue_role,
atrt_role
WHERE cm_workqueue.mstr_queue_encode = 10000001
AND cm_workqueue.status_ref = 10000000
AND cm_wkqueue_role.wqurolid_ref = 1
AND THERE ARE)
SELECT 1
OF cm_filterset f.
cm_wkqueue_role cmr
WHERE f.wqueueid_ref = cm_workqueue.wqueueid_ref
AND f.status_ref = 10000000
AND cmr.roleid_ref = atrt_role.roleid_ref
AND cmr.wqueueid_ref = f.mstrquid_ref
AND cmr.referral_yorn_encode = 10000000
AND cmr.status_ref = 10000000)
ORDER OF CASES
WHEN cm_workqueue.wqueueid_ref < 999 THEN 1
Of OTHER cm_workqueue.mstr_queue_encode
END,
BASS (cm_workqueue.desc_text)No, you must put the case in the selection list
SQL> select ename, empno, deptno, case deptno when 10 then 2 when 20 then 1 else 0 end dno from emp where deptno = 10 2 union 3 select ename, empno, deptno, case deptno when 10 then 2 when 20 then 1 else 0 end dno from emp where deptno = 20 4 order by 4, ename 5 / ENAME EMPNO DEPTNO DNO ---------- ---------- ---------- ---------- ADAMS 7876 20 1 FORD 7902 20 1 JONES 7566 20 1 SCOTT 7788 20 1 SMITH 7369 20 1 CLARK 7782 10 2 KING 7839 10 2 MILLER 7934 10 2 8 rows selected. SQL>
-
With the help of SCORE on top of view with UNION
Hi guys,.
I explain what I'm trying to do it quickly:
2 tables: table1 and table2 with the same structure and have both a multi_column_datastore ctxsys.context.
1 view: View1
Select * from table1
Union
Select * from table2
If I run:
Select * from View1 WHERE contains (view1. Column1, '% textext %', 1) > 0;
It works fine, I get the correct result.
If I try to use the PARTITION function, I got an error:
Select * from View1 WHERE contains (view1. Column1, '% textext %', 1) > 0 ORDER by SCORE (1);
ORA-29921: auxiliary operator not supported with query to configure block
I understand the problem is in the UNION inside the view, is it possible to keep work, filtering the VIEW?
Thanks in advance
There is no score in the view, so you cannot reference the score when you query the view. In order to put the note in the view, you need a contains the clause, which requires a value. A method to do this is to use sys_context. Please see the reproduction of the problem and solution below.
Scott@orcl12c >-reproduction of the problem:
Scott@orcl12c > create the table1 table:
2 (column1 varchar2 (30))
3.
Table created.
Scott@orcl12c > insert into table1 values ('textext")
2.
1 line of creation.
Scott@orcl12c > create table table2
2 (column1 varchar2 (30))
3.
Table created.
Scott@orcl12c > insert into table2 values ('textext")
2.
1 line of creation.
Scott@orcl12c > start
2 ctx_ddl.create_preference ('test_ds', 'multi_column_datastore');
3 ctx_ddl.set_attribute ('test_ds', 'columns', "column1");
4 end;
5.
PL/SQL procedure successfully completed.
Scott@orcl12c > create index table1_idx on table1 (column1)
2 indextype is ctxsys.context
3 parameters ("test_ds of the data store")
4.
The index is created.
Scott@orcl12c > create index table2_idx on the table2 (column1)
2 indextype is ctxsys.context
3 parameters ("test_ds of the data store")
4.
The index is created.
Scott@orcl12c > create or replace view View1
2 as
3 select * from table1
4 union
5 select * from table2
6.
Created view.
Scott@orcl12c > select * from View1 where contains (view1.column1, '% textext %', 1) > 0
2.
COLUMN1
------------------------------
textext
1 selected line.
Scott@orcl12c > select * from View1 where contains > 0 (view1.column1,'%textext%',1) order by score (1)
2.
Select * from View1 where contains > 0 (view1.column1,'%textext%',1) order by score (1)
*
ERROR on line 1:
ORA-29921: auxiliary operator not supported with query to configure block
Scott@orcl12c >-solution:
Scott@orcl12c > create or replace view View1
2 as
3. Select the partition (1) score, table1.* from table1
4 where contains (table1. Column1, sys_context ('text_query', 'query_value'), 1) > 0
5 union
6 select score partition (1), table2.* from table2
7 where contains (table2.column1, sys_context ('text_query', 'query_value'), 1) > 0
8.
Created view.
Scott@orcl12c > create or replace context text_query using text_proc
2.
Context that is created.
Scott@orcl12c > create or replace procedure text_proc
2 (p_val in varchar2)
3 as
4 start
5 dbms_session.set_context ('text_query', "query_value", p_val);
6 end text_proc;
7.
Created procedure.
Scott@orcl12c > text_proc exec ('% textext %')
PL/SQL procedure successfully completed.
Scott@orcl12c > set autotrace on explain
Scott@orcl12c > select * from View1 by score
2.
MARK THE COLUMN1
---------- ------------------------------
3 textext
1 selected line.
Execution plan
----------------------------------------------------------
Hash value of plan: 4090246122
----------------------------------------------------------------------------------------------
| ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time |
----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2. 60. 8 (0) | 00:00:01 |
| 1. SORT ORDER BY | | 2. 60. 8 (0) | 00:00:01 |
| 2. VIEW | VIEW1. 2. 60. 8 (0) | 00:00:01 |
| 3. UNIQUE FATE | | 2. 58. 8 (50) | 00:00:01 |
| 4. UNION-ALL | | | | | |
| 5. TABLE ACCESS BY INDEX ROWID | TABLE1. 1. 29. 4 (0) | 00:00:01 |
|* 6 | DOMAIN INDEX | TABLE1_IDX | | | 4 (0) | 00:00:01 |
| 7. TABLE ACCESS BY INDEX ROWID | TABLE2. 1. 29. 4 (0) | 00:00:01 |
|* 8 | DOMAIN INDEX | TABLE2_IDX | | | 4 (0) | 00:00:01 |
----------------------------------------------------------------------------------------------
Information of predicates (identified by the operation identity card):
---------------------------------------------------
6 - access("CTXSYS".") CONTAINS "(" TABLE1".»)" Column1', SYS_CONTEXT ('text_query ',' query_v)
Alou '), 1) > 0)
8 - access("CTXSYS".") CONTAINS "(" TABLE2".»)" Column1', SYS_CONTEXT ('text_query ',' query_v)
Alou '), 1) > 0)
Note
-----
-the dynamic statistics used: dynamic sampling (level = 2)
Scott@orcl12c >
-
As I am not able to use IF-ELSE in Oracle for select statements, I use to get rid of a select set of query results and UNION to get the results of other queries set according to the value of the parameter.
Example of a normal query:
Select FNAME LNAME from EMP where Param.1 in ('NAME', 'NAME')
UNION
SELECT CITY, ADDRESS from EMP where Param.1 in ('LOCATION', 'ADDRESS')
When I give Param.1 as 'NAME', it gives me the results of the first query set while it ignores another query resultset will null coz Param.1 isn't in "Position" or "ADDRESS".
Same Wau if I give Param.1 'Address', this is for me the result set of a query resultset 1 2nd query and spread as it is null.
But the same approach doesnot work to query Oracle with Xquery that follows:
Select rtrim (XMLELEMENT ("lines", XMLAGG (RW.column_value)), ',') stopped
EMPTABLE UL, XMLTABLE (' sets of lines/lines/lines ' UL of PASSAGE. ") TEXT) AS RW
WHERE
[Param.1] IN ('NAME', 'NAME')
UNION
Select rtrim (XMLELEMENT ("lines", XMLAGG (RW.column_value)), ',') stopped
ADDRESSTABLE UL, XMLTABLE (' sets of lines/lines/lines ' UL of PASSAGE. ") TEXT) AS RW
WHERE
[Param.1] IN ('LOCATION', 'ADDRESS')
Here, if the Param.1 is "NAME" then the results in the form < all > < / lines > + out of another query (that thing I want)
How can I remove this < Rowset > < / lines > output?As I am not able to use IF-ELSE in Oracle for select statements
How about a CASE statement?
If every query is always supposed to return only a single grouped column (and of course a single line), you can use this:SELECT CASE WHEN param_1 IN ('VAL1', 'VAL2') THEN ( SELECT ... FROM emptable, XMLTable( ... ) ) WHEN param_1 IN ('VAL3', 'VAL4') THEN ( SELECT ... FROM addresstable, XMLTable( ... ) ) END AS Orders FROM dual;
If you prefer that the approach of the UNION, then:
Here, if the Param.1 is 'NAME' then results such as
+ out of another query (that thing I want) It is because of the aggregation XMLAgg function.
An aggregate without GROUP BY function always returns a line, even if there is no line in the original game.SQL> SELECT XMLElement("Rowset", XMLAgg(rw.column_value)) 2 FROM emptable ul 3 , XMLTable('/Rowsets/Rowset/Row' passing ul.text) as rw 4 WHERE 1 = 0 5 ; XMLELEMENT("ROWSET",XMLAGG(RW. --------------------------------------------------------------------------------
You can group into an empty list of column to force the required behavior:
SQL> SELECT XMLElement("Rowset", XMLAgg(rw.column_value)) 2 FROM emptable ul 3 , XMLTable('/Rowsets/Rowset/Row' passing ul.text) as rw 4 WHERE 1 = 0 5 GROUP BY () 6 ; XMLELEMENT("ROWSET",XMLAGG(RW. --------------------------------------------------------------------------------
You must also use UNION ALL instead of UNION whereas Oracle doesn't bother doing the a unnecessary sort operation.
What is the purpose of RTRIM in your examples? I see no reason to call it on an instance of XMLType, in addition, it performs a type conversion from implicit VARCHAR2 data that can potentially cause errors.
If you must serialize the resulting XMLType column and then use the methods getStringVal(), getClobVal(), or XMLSerialize (11g).Published by: odie_63 on 7 nov. 2012 23:24
-
order by clause union in date column
Hi gems... good evening...
Oracle version: 11.2.0.2 EL6 Linux server
I have a query as below:
Select col1, col2, col3 col4
tab1, tab2, tab3
where the conditions
Union
Select col1, col2, col3 col4
tab1, tab2, tab3
where the conditions
Union
Select col1, col2, col3 col4
tab1, tab2, tab3
where the conditions;
Now the col4 is a date column, and I need to order by on that entire result sets. I know that I can do (order of col4) (order by 4) or at the end of all of the query.
But the problem is that the output is coming to Mon-dd-yyyy (i.e. 31-Dec-2012).
I want each output in the format dd/mm/yyyy, so I need to use the function to_char.
But in this case, I can't use the order by clause, because in this case it's get organized by character IE by 1,2,3,4,5 like that.
How can I solve my problem... Please help... Thanks in advance.
Published by: gogol on 3 November 2012 08:05Maybe - when you order by date column, she did it with a date internal representation so on exit you can use any date format you think appropriate
select distinct col1,col2,col3,col4 from (select col1,col2,col3,col4 from tab1,tab2,tab3 where conditions_1 or conditions_2 or conditions_3 )
Concerning
Etbin
-
Hi all
I have two requests and I want to join this two query
The column of the report should be like this
1st requestitem_number WK_30 WE_31
2nd requestselect re.item_number, nvl(le.quantity,0) - nvl(re.quantity,0) WK_30 from BACKLOG_WEEK_WH_AFTR_ATP le, BACKLOG_ATP_GT_CW_IN re where le.item_number =re.item_number and to_number(substr(re.year_week,-2,2)) = to_number(to_char(sysdate,'IW'))+1
Thanks in advanceselect re.item_number, nvl(le.quantity,0) - nvl(re.quantity,0) WK_31 from BACKLOG_WEEK_WH_AFTR_ATP le, BACKLOG_ATP_GT_CW_IN re where le.item_number =re.item_number and to_number(substr(re.year_week,-2,2)) = to_number(to_char(sysdate,'IW'))+2
ConcerningHello
You are welcome.
I think that the best way to understand that is to show what data is generated at each stage...
1. we need to generate a list of lines that have every week, we are interested in swing on...
( SELECT to_number(to_char(sysdate,'IW')) + ROWNUM year_week_num FROM DUAL CONNECT BY LEVEL <= 5 ) row_gen YEAR_WEEK_NUM ------------- 30 31 32 33 34
2. must be added BACKLOG_WEEK_WH_AFTR_ATP lines in the calculations for the first week in the report
SELECT le.item_number, le.quantity, to_number(to_char(sysdate,'IW'))+1 year_week_num FROM BACKLOG_WEEK_WH_AFTR_ATP le UNION ALL SELECT re.item_number, -re.quantity, to_number(substr(re.year_week,-2,2)) year_week_num FROM BACKLOG_ATP_GT_CW_IN re / ITEM_NUMBE QUANTITY YEAR_WEEK_NUM ---------- ---------- ------------- ITEM_1 200 30 ITEM_2 600 30 ITEM_1 -200 30 ITEM_1 -100 33 ITEM_2 -300 30 ITEM_2 -200 32 ITEM_3 -800 30 ITEM_3 -400 34
3. we must now repeat weekly, generated by the request of row_gen against all of the item_numbers returned by the above query and match them if possible to get the amount. Where there is no entry in BACKLOG_WEEK_WH_AFTR_ATP or BACKLOG_ATP_GT_CW_IN for a number of the week, we should use 0 as the quantity. The Partition Outer Join does for us.
SQL> select 2 re.item_number, 3 row_gen.year_week_num, 4 NVL(re.quantity,0) qty 5 from 6 ( SELECT 7 to_number(to_char(sysdate,'IW')) + ROWNUM year_week_num 8 FROM 9 DUAL 10 CONNECT BY LEVEL <= 5 11 ) row_gen LEFT OUTER JOIN 12 ( SELECT 13 le.item_number, 14 le.quantity, 15 to_number(to_char(sysdate,'IW'))+1 year_week_num 16 FROM 17 BACKLOG_WEEK_WH_AFTR_ATP le 18 UNION ALL 19 SELECT 20 re.item_number, 21 -re.quantity, 22 to_number(substr(re.year_week,-2,2)) year_week_num 23 FROM 24 BACKLOG_ATP_GT_CW_IN re 25 ) re 26 PARTITION BY (re.item_number) 27 ON ( row_gen.year_week_num = re.year_week_num) 28 / ITEM_NUMBE YEAR_WEEK_NUM QTY ---------- ------------- ---------- ITEM_1 30 200 ITEM_1 30 -200 ITEM_1 31 0 ITEM_1 32 0 ITEM_1 33 -100 ITEM_1 34 0 ITEM_2 30 -300 ITEM_2 30 600 ITEM_2 31 0 ITEM_2 32 -200 ITEM_2 33 0 ITEM_2 34 0 ITEM_3 30 -800 ITEM_3 31 0 ITEM_3 32 0 ITEM_3 33 0 ITEM_3 34 -400 17 rows selected.
4. now, we have the base dataset that we need - she has all the amounts with the right directions so that we can simply add everything up as a sum of sumulative. That's what makes OVER(PARTITION BY re.item_number ORDER BY row_gen.year_week_num) SUM (NVL(re.quantity,0)) bit. He said baseically are all lines seen so far in the result set for each item number...
select re.item_number, row_gen.year_week_num, NVL(re.quantity,0) qty, SUM(NVL(re.quantity,0)) OVER(PARTITION BY re.item_number ORDER BY row_gen.year_week_num) quantity from ( SELECT to_number(to_char(sysdate,'IW')) + ROWNUM year_week_num FROM DUAL CONNECT BY LEVEL <= 5 ) row_gen LEFT OUTER JOIN ( SELECT le.item_number, le.quantity, to_number(to_char(sysdate,'IW'))+1 year_week_num FROM BACKLOG_WEEK_WH_AFTR_ATP le UNION ALL SELECT re.item_number, -re.quantity, to_number(substr(re.year_week,-2,2)) year_week_num FROM BACKLOG_ATP_GT_CW_IN re ) re PARTITION BY (re.item_number) ON ( row_gen.year_week_num = re.year_week_num) / ITEM_NUMBE YEAR_WEEK_NUM QTY QUANTITY ---------- ------------- ---------- ---------- ITEM_1 30 200 0 ITEM_1 30 -200 0 ITEM_1 31 0 0 ITEM_1 32 0 0 ITEM_1 33 -100 -100 ITEM_1 34 0 -100 ITEM_2 30 -300 300 ITEM_2 30 600 300 ITEM_2 31 0 300 ITEM_2 32 -200 100 ITEM_2 33 0 100 ITEM_2 34 0 100 ITEM_3 30 -800 -800 ITEM_3 31 0 -800 ITEM_3 32 0 -800 ITEM_3 33 0 -800 ITEM_3 34 -400 -1200
5. we can now rotate these data in columns - however, there is a small problem. For points 1 and 2 we have 2 lines for the week 30, running in the quantity column total is the same for both because the two lines appear at the same point in the game as defined by the order by clause. This means before rotate us, we must select one of the lines - or the other will do, they are both correct. If we do not have, the effect will be to double the cumulative sum (for point 1 is 0, so it does not appear) for this, we can use the ROW_NUMBER function...
select re.item_number, row_gen.year_week_num, NVL(re.quantity,0) qty, SUM(NVL(re.quantity,0)) OVER(PARTITION BY re.item_number ORDER BY row_gen.year_week_num) quantity, ROW_NUMBER() OVER(PARTITION BY re.item_number, row_gen.year_week_num ORDER BY NULL) rn from ( SELECT to_number(to_char(sysdate,'IW')) + ROWNUM year_week_num FROM DUAL CONNECT BY LEVEL <= 5 ) row_gen LEFT OUTER JOIN ( SELECT le.item_number, le.quantity, to_number(to_char(sysdate,'IW'))+1 year_week_num FROM BACKLOG_WEEK_WH_AFTR_ATP le UNION ALL SELECT re.item_number, -re.quantity, to_number(substr(re.year_week,-2,2)) year_week_num FROM BACKLOG_ATP_GT_CW_IN re ) re PARTITION BY (re.item_number) ON ( row_gen.year_week_num = re.year_week_num) / ITEM_NUMBE YEAR_WEEK_NUM QTY QUANTITY RN ---------- ------------- ---------- ---------- ---------- ITEM_1 30 200 0 1 ITEM_1 30 -200 0 2 ITEM_1 31 0 0 1 ITEM_1 32 0 0 1 ITEM_1 33 -100 -100 1 ITEM_1 34 0 -100 1 ITEM_2 30 -300 300 1 ITEM_2 30 600 300 2 ITEM_2 31 0 300 1 ITEM_2 32 -200 100 1 ITEM_2 33 0 100 1 ITEM_2 34 0 100 1 ITEM_3 30 -800 -800 1 ITEM_3 31 0 -800 1 ITEM_3 32 0 -800 1 ITEM_3 33 0 -800 1 ITEM_3 34 -400 -1200 1
6. so now that we have a way to select one of the duplicate lines, we can move forward a pivot data by ensuring that us choose only 'first' line for each element for each week...
SELECT item_number, SUM ( CASE WHEN year_week_num = to_number(to_char(sysdate,'IW'))+1 THEN quantity END ) plus_1, SUM ( CASE WHEN year_week_num = to_number(to_char(sysdate,'IW'))+2 THEN quantity END ) plus_2, SUM ( CASE WHEN year_week_num = to_number(to_char(sysdate,'IW'))+3 THEN quantity END ) plus_3, SUM ( CASE WHEN year_week_num = to_number(to_char(sysdate,'IW'))+4 THEN quantity END ) plus_4, SUM ( CASE WHEN year_week_num = to_number(to_char(sysdate,'IW'))+5 THEN quantity END ) plus_5 FROM ( select re.item_number, row_gen.year_week_num, SUM(NVL(re.quantity,0)) OVER(PARTITION BY re.item_number ORDER BY row_gen.year_week_num) quantity, ROW_NUMBER() OVER(PARTITION BY re.item_number, row_gen.year_week_num ORDER BY NULL) rn from ( SELECT to_number(to_char(sysdate,'IW')) + ROWNUM year_week_num FROM DUAL CONNECT BY LEVEL <= 5 ) row_gen LEFT OUTER JOIN ( SELECT le.item_number, le.quantity, to_number(to_char(sysdate,'IW'))+1 year_week_num FROM BACKLOG_WEEK_WH_AFTR_ATP le UNION ALL SELECT re.item_number, -re.quantity, to_number(substr(re.year_week,-2,2)) year_week_num FROM BACKLOG_ATP_GT_CW_IN re ) re PARTITION BY (re.item_number) ON ( row_gen.year_week_num = re.year_week_num) ) WHERE rn = 1 GROUP BY item_number
You need not add a join between BACKLOG_WEEK_WH_AFTR_ATP and BACKLOG_ATP_GT_CW_IN, because we are all summed up in the article number and all item numbers, we have selected two tables. The aggregation ensures that the quantities are added together against the item number.
HTH
David
-
ODI 11 g multiple table with union join
Hi all
I have a problem with joining 4 different tables and union all of them.
Here I want to generate this query;
Select * from
Table1, table2
Join table2 = table1
Union of all the
Select * from
Table 3, table 4
Join table3 = table 4
But I have not the same column that all the table, some are missing. When I write one - PL/SQL query I can easily add a false "NULL column1' column, but I can not how I do with ODI 11 g.
I tried not to use "Active Mapping", but it does not work.
I'm newbie ODI 11 g
Thank you
Myra
Hi, Myra,.
I'd even as in PL/SQL. In your table target add all the columns you need and overall data whatever you're missing some columns just put NULL in the mapping.
Thank you
Ajay
-
I can not activate a new order paid with a different email address
After the trial period months using a gmail account, I paid a new order using an account of hotmail of mine, the order number of the answer of the adobe (for hotmail) can not be activated: my account is fixed with the old (and allegedly expired) gmail. Also when you try to change my ID in hotmail instead of gmail, it is said: "(impossible de changer,,,)." This is already a month and still the same.
How can we get the money. Then I can make a new order using the gmail email account.
Maybe European people who post here can help you with this question.
-
How to upgrade an ordered list with a constraint
Hi friends,
I need to update a column for all records in the table with a unique constraint
ex - select rec_no in tablename;
9
8
7
6
5
now I have to update this as
Select rec_no in tablename;
8
7
6
5
4
but when I do update tablename set rec_no = rec_no-1
I get unique constraint error
Please help meDJ wrote:
but when I do update tablename set rec_no = rec_no-1
I get unique constraint errorNo, you don't. Show us.
Oracle ensures consistency of 'duress of education. " If you perform an update statement (which is what you say above), then the Oracle will not give you a constraint error.
Because if she did, then we are all would have a serious problem: whether or not the update succeeds then depend on the order in which Oracle (ie. the implementation plan) happens to be updated lines. And we cannot have such behavior "undeterministic" (similar to what we would have if Oracle has not provided us with the error table mutation...).
Just so try, or show us:
create table uk_test (i number not null unique) / insert into uk_test values(4); insert into uk_test values(3); insert into uk_test values(2); commit;
Now try these two:
update (select * from uk_Test order by i desc) set i=i-1; update (select * from uk_Test order by i) set i=i-1;
They both succeed. Oracle allows temporary constraint violations during at the execution of a statement inside. There is no need to define this constraint postponed here.
I'm talking about consistency of statement here also-level constraint:
http://rulegen.blogspot.nl/2012/01/statement-level-constraint-validation.html
-
Poblem with union/union all giving ORA-22950
I use a select statement with the xmlelement to create an xml file.
There the various unions end in 5, but I always bring a line through.
I do like that as then I can use this as a cursor in my plsql without having to create lots of cursors when I only need to use a.
That is to say.
Then, I use a cursor to execute it in a ftp file folder I selected passing ptype as a parameter.SELECT xmlelement("QUOTATION",xmlagg(xmlelement(Quotation, xmlelement(CUSTOMERNAME, customername), xmlelement(QUOTE,quoteid )))xmlfile From cust, quote where custid=quotecustid and ptype = 'Q' UNION SELECT xmlelement("SALES",xmlagg(xmlelement(SALES, xmlelement(CUSTOMERNAME, customername), xmlelement(QUOTE,salesid )))xmlfile From cust, sales where custid=salescustid and ptype = 'S'
If I use the union I get ORA-22950 - I have seen other people use UNION all around it.
If I use union all I'll get a value for the SALE of any line that is
Any ideas how I can get around this as do not want to continue to create cursorsThe use of different sliders is always the best approach IMO.
All things considered, it should be more effective than that appearing more "smart" one.And there is no need to be explicit cursors, it could just be SELECT INTOs wrapped in a PL/SQL CASE statement, with the appropriate exception handler (if necessary).
Anyway, if you want to stick with the single-cursor method, there are different ways to do this:
(1) adding a column selector:
SELECT xmlfile FROM ( SELECT 'Q' as selector , xmlelement("QUOTATION",xmlagg(xmlelement(Quotation, xmlelement(CUSTOMERNAME, customername), xmlelement(QUOTE,quoteid )))xmlfile From cust, quote where custid=quotecustid and ptype = :1 UNION ALL SELECT 'S' , xmlelement("SALES",xmlagg(xmlelement(SALES, xmlelement(CUSTOMERNAME, customername), xmlelement(QUOTE,salesid )))xmlfile From cust, sales where custid=salescustid and ptype = :1 ) WHERE selector = :1 ;
(2) adding a GROUP BY clause, so that XMLAgg returns nothing if no row is selected:
SELECT xmlelement("QUOTATION",xmlagg(xmlelement(Quotation, xmlelement(CUSTOMERNAME, customername), xmlelement(QUOTE,quoteid )))xmlfile From cust, quote where custid=quotecustid and ptype = :1 GROUP BY null UNION ALL SELECT xmlelement("SALES",xmlagg(xmlelement(SALES, xmlelement(CUSTOMERNAME, customername), xmlelement(QUOTE,salesid )))xmlfile From cust, sales where custid=salescustid and ptype = :1 GROUP BY null ;
(3) subqueries:
SELECT case :1 when 'Q' then ( SELECT ... ) when 'S' then ( SELECT ... ) end as xmlfile FROM dual ;
-
order by with an additional condition?
Hello
Oracle 11g r2.
I have a few items (let's say the dominoes of a domino game that are placed on one table in front of the other).
Sometimes, between the 2 items, I have a beam (piece of wood) which is an element too.
I have tables that store the entry and exit of each element and the distance accumulated between them.
A point is identified by a name of area (where it is placed), a class, a number (not really, just an identifier) and type of point (entry (E) or output (S)).
Here is an example of data:CREATE TABLE SPATIAL_POINTS ( SPAT_ID INTEGER, SPAT_AREA_NAME VARCHAR2(10), SPAT_CLASS VARCHAR2(15), SPAT_NUMBER VARCHAR2(15), SPAT_PT VARCHAR2(1), ... CONSTRAINT PK_SPAT_ID PRIMARY KEY(SPAT_ID), CONSTRAINT FK_SPAT_AREA FOREIGN KEY(SPAT_AREA_NAME) REFERENCES AREAS(AREA_NAME), CONSTRAINT CHK_SPAT_PT CHECK(SPAT_PT) IN ('E','S') ); CREATE TABLE ALL_COORDINATES ( ALL_ID INTEGER, ALL_SPAT_ID INTEGER, ALL_CUMUL NUMBER(15,9), ..., CONSTRAINT PK_ALL_ID PRIMARY KEY(ALL_ID), CONSTRAINT FK_ALL_SPAT_ID FOREIGN KEY(ALL_SPAT_ID) REFERENCES SPATIAL_POINTS(SPAT_ID), );
I want to display the points sorted by distance (as in the example), but I want to show at each point of exit right after the entry point, I don't want all the other points to apear beetween an entry and an exit point of the same element.select spat_area_name as "AREA", spat_class as "CLASS", spat_number as "NUMBER", spat_pt as "POINT", all_cumul as "CUMUL DIST" from spatial_points inner join all_coordinates on spat_id = all_spat_id where spat_area_name = 'P61' order by all_cumul, spat_class, spat_number, spat_pt AREA CLASS NUMBER POINT CUMUL DIST P61 POUTRE 01 E -0.07700 P61 CLSNA 0105 E 0.00000 P61 CLSNA 0105 S 0.00400 P61 CLSNB 0110 E 0.05600 P61 CLSNB 0110 S 0.12400 P61 CLBPE 0125 E 0.37240 P61 CLBPE 0125 S 0.37240 P61 CLSNC 0130 E 0.44500 P61 CLSNC 0130 S 0.45500 P61 CLSNC 0140 E 0.79500 P61 CLSNC 0140 S 0.80500 P61 CLSNC 0160 E 0.99500 P61 CLSNC 0160 S 1.00500 P61 CLMTV 0165 E 1.06100 P61 CLMTV 0165 S 1.14100 P61 CLBPE 0166 E 1.18440 P61 CLBPE 0166 S 1.18440 P61 CLSNC 0170 E 1.19500 P61 CLSNC 0170 S 1.20500 P61 POUTRE 01 S 1.32300 P61 POUTRE 02 E 1.36000 P61 CLSND 0210 E 1.37750 P61 CLSND 0210 S 1.42250 P61 CLPBU 0215 E 1.50700 P61 POUTRE 02 S 1.54200 ... ... ... ... ...
I want to order by cumulative distance of entry points (E) and then add the exit point (S) after each entry point regardless of the cumulative distance of the outlet. I hope I'm understandable!
Here is an example of what I want, according to the example above:
In this way 'E' and ' go for the points of the "BEAM", and we don't care about cumulative distance of ' point.AREA CLASS NUMBER POINT CUMUL DIST P61 POUTRE 01 E -0.07700 P61 POUTRE 01 S 1.32300 P61 CLSNA 0105 E 0.00000 P61 CLSNA 0105 S 0.00400 P61 CLSNB 0110 E 0.05600 P61 CLSNB 0110 S 0.12400 P61 CLBPE 0125 E 0.37240 P61 CLBPE 0125 S 0.37240 P61 CLSNC 0130 E 0.44500 P61 CLSNC 0130 S 0.45500 P61 CLSNC 0140 E 0.79500 P61 CLSNC 0140 S 0.80500 P61 CLSNC 0160 E 0.99500 P61 CLSNC 0160 S 1.00500 P61 CLMTV 0165 E 1.06100 P61 CLMTV 0165 S 1.14100 P61 CLBPE 0166 E 1.18440 P61 CLBPE 0166 S 1.18440 P61 CLSNC 0170 E 1.19500 P61 CLSNC 0170 S 1.20500 P61 POUTRE 02 E 1.36000 P61 POUTRE 02 S 1.54200 P61 CLSND 0210 E 1.37750 P61 CLSND 0210 S 1.42250 P61 CLPBU 0215 E 1.50700 ... ... ... ... ...
I tried some things with PARTITION BY, but without success.
Any idea on how I can achieve this? I need a query, no PL/SQL.
Thank you!
Yann.As...
select * from( select spat_area_name as "AREA", spat_class as "CLASS", spat_number as "NUMBER", spat_pt as "POINT", all_cumul as "CUMUL_DIST", min(CUMUL DIST) over(partition by spat_area_name) mn from spatial_points inner join all_coordinates on spat_id = all_spat_id where spat_area_name = 'P61' order by all_cumul, spat_class, spat_number, spat_pt) order by mn,AREA,CUMUL_DIST
tested with the EMP table. The query below will be stopped first by empno, then it will show all employees of the particular department...
SQL> select empno,deptno 2 from scott.emp 3 order by empno; EMPNO DEPTNO ---------- ---------- 7369 20 7499 30 7521 30 7566 20 7654 30 7698 30 7782 10 7788 20 7839 10 7844 30 7876 20 7900 30 7902 20 7934 10 14 rows selected. SQL> select * 2 from( 3 select empno,deptno,min(empno) over(partition by deptno) mn 4 from scott.emp 5 order by empno 6 ) 7 order by mn,deptno,empno; EMPNO DEPTNO MN ---------- ---------- ---------- 7369 20 7369 7566 20 7369 7788 20 7369 7876 20 7369 7902 20 7369 7499 30 7499 7521 30 7499 7654 30 7499 7698 30 7499 7844 30 7499 7900 30 7499 7782 10 7782 7839 10 7782 7934 10 7782 14 rows selected.
Published by: JAC on April 3, 2012 19:40
-
Hello
I have a request below which I need the merger of two of them to get the result in a single query result, here's the query
but the error me do is ORA-12704: incompatibility of character set, please let me know wat is the error in the query below
Select rpad ('Roster_num', 20, "") 'Roster_num '.
of the double
UNION ALL
SELECT
SEPARATE
RPAD (AP. EMPLOYEE_CODE, 20, "") 'Roster_num '.
AR_EMPLOYEE_PERSONAL AP,
AR_ADDRESS AA,
AR_V_ROSTER_EMPLOYEE_DEF AD,
AR_STATE_PROV_REGION ASP,
AR_DEFINITION_TERRITORY ADT
WHERE
BDM PROJECT = 81 AND
BPM EMPLOYEE_ID = AA. EMPLOYE_ID AND
BPM EMPLOYEE_ID = AD. EMPLOYEE_ID (+) AND
BAM STATE_PROV_REG_ID = ASP STATE_PROV_REG_ID AND
BDM DEFINITION_TERRITORY_ID = ADT. DEFINITION_TERRITORY_ID (+)
Thank you
SudhirExplanation with some examples:
SQL>desc t_test; Name Null Type ------------------------------ -------- ----------------- COL1 NUMBER COL2 NUMBER COL3 NUMBER COL4 NCHAR(30)
We see that COL4 in the table is of type NCHAR.
SQL>select * from t_test; COL1 COL2 COL3 COL4 ---------------------- ---------------------- ---------------------- ------------------------------ 1 123 3 Roster 2 3 4 RKött 3 4 2 9 10 12 11 23 43 SQL>select rpad('Roster_num',20,' ') "Roster_num" from dual union all select RPAD(col4,20,' ') "Roster_num" from t_test; Error starting at line 7 in command: select rpad('Roster_num',20,' ') "Roster_num" from dual union all select RPAD(col4,20,' ') "Roster_num" from t_test Error at Command Line:7 Column:7 Error report: SQL Error: ORA-12704: character set mismatch 12704. 00000 - "character set mismatch" *Cause: One of the following - The string operands(other than an nlsparams argument) to an operator or built-in function do not have the same character set. - An nlsparams operand is not in the database character set. - String data with character set other than the database character set is passed to a built-in function not expecting it. - The second argument to CHR() or CSCONVERT() is not CHAR_CS or NCHAR_CS. - A string expression in the VALUES clause of an INSERT statement, or the SET clause of an UPDATE statement, does not have the same character set as the column into which the value would be inserted. - A value provided in a DEFAULT clause when creating a table does not have the same character set as declared for the column. - An argument to a PL/SQL function does not conform to the character set requirements of the corresponding parameter. *Action:
Error returned for incompatibility of character as data types are not compatible.
SQL>select rpad('Roster_num',20,' ') "Roster_num" from dual union all select TO_CHAR(RPAD(col4,20,' ')) "Roster_num" from t_test; Roster_num ------------------------------------------ Roster_num Roster RKött 6 rows selected SQL>
After the converted to the TO_CHAR problem solved
Maybe you are looking for
-
Hello. I am a member on these forums and I always want to learn new things. What is the SMC reset? When is it used? It will erase all personal data files?
-
Recovery after that hibernation is too long on the Qosmio F30
Hello I use my Qosmio F30 laptop about 3 years and still have the same problem. Recovery of Windows after hibernation, it takes about 1 to 1.3 minutes long. Of course it is not too much, but I see how quickly rescind my other two books, with a lower
-
Upgrade 32-bit, XP to Windows 7 64-bit
Running XP Home Edition and XP Professional Edition, the two 32 bit. on two separate machines. Try to update either of them to Windows 7 Professional 64 bit. Processor is an AMD Athlon 64 x 2 Dual core and have enough disk space and RAM. If I try to
-
I was using IE 8 and was in Hotmail when the error message interrupted a part of the message: psdfilter.sys - ADDRESS F788E8E0 base at F788B000, Datestamp 467e1bf2
-
I just got a surround sound blueray/home cinema sound for Christmas. I wanted to find a way to play music on the home theater using the Droid X wireless. It seems that this is possible, but I'm running into a problem. I have the currently connec