Union & order by
hi
i have this SQL:
SELECT 1,A,date
FROM TEST
where A='X'
UNION
SELECT 2,A,date
FROM TEST
where A='Y'
I can not order by date!(i do this sql in report builder).
help me.
thanks
this is my query:
SELECT 1, U.CLINT_IP, U.CLIENT_USER_NAME, U.LOG_DATE,
U.LOG_TIME, U.PROCESSING_TIME,
(U.BYTE_SENT/1024) dis_bs,
(U.BYTE_RECIEVED/1024) dis_br, U.OBJECT_NAME
FROM USER_LOG_FILES U
where EXISTS (select 1 from compare_url C where upper(U.Object_Name) like upper('%'|| C.Url ||'%') )
and ( upper(U.CLIENT_USER_NAME) like '%'||upper(:user_id)||'%' or :user_id is null)
and LOG_DATE between :login_date1 and :login_date2
UNION
SELECT 2, U.CLINT_IP, U.CLIENT_USER_NAME, U.LOG_DATE,
U.LOG_TIME, U.PROCESSING_TIME,
(U.BYTE_SENT/1024) dis_bs,
(U.BYTE_RECIEVED/1024) dis_br, U.OBJECT_NAME
FROM USER_LOG_FILES U
where NOT EXISTS (select 1 from compare_url C where upper(U.Object_Name) like upper('%'|| C.Url ||'%') )
and ( upper(U.CLIENT_USER_NAME) like '%'||upper(:user_id)||'%' or :user_id is null)
and LOG_DATE between :login_date1 and :login_date2
order by 3
and this is my table :
SQL> desc user_log_files
Name Null? Type
CLINT_IP VARCHAR2(4000)
CLIENT_USER_NAME VARCHAR2(4000)
LOG_DATE DATE
LOG_TIME VARCHAR2(4000)
PROCESSING_TIME NUMBER(10)
BYTE_SENT NUMBER(10)
BYTE_RECIEVED NUMBER(10)
OBJECT_NAME VARCHAR2(4000)
thanks.
Similar Messages
-
Union Order by causing issue in SetWhereClause
Hi,
I am extending a CO and adding new query to the VO query.
I added where clause to the VO query. Also added Union. The Union is the same VO query with another where condition. VO query default had a Order by clause.
Now the total query looks like this.
select * from
(select a, b
from x, y) QRSLT
where ...
UNION
select * from
(select a, b
from x, y) QRSLT1
where ...
order by upper(a)
"Order by" is getting added since it is there in the original VO query.
Now I am getting the error as "java.sql.SQLException: ORA-01785: ORDER BY item must be the number of a SELECT-list expression"
The above query without the order by is working good. What is the best way to fix this?
Thanks,
HCThe problem is that you're using a UNION. The ORDER BY clause has to refer to a column in the query as a whole rather than just the second part of the UNION. In that situation, Oracle expects either a column position or the name of one of the selected columns.
Thanks
--Anil
http://oracleanil.blogspot.com/ -
Windows 8.1 - safe to delete "Browser Choice"?
Windows 8.1
The European Union ordered Microsoft to include a program in the Windows OS sold in Europe. This program makes a pop up
to invite PC users to use browsers different than Internet explorer.
This is not a malware or adware issue.
This obligation has expired.
The main program is in C:Windows/system32 directory
While the obligation was in force, it was believed that deleting the file would cause problems with future windows updates.
Is t safe to delete the program now?
The most reassuring would be to get a reply from Microsoft staff clearly saying whether deleting all the "browser choice" files would not cause any problems including for future updates etc, or even that an uninstaller has been prepared for this
case.Kindly go through the workarounds as suggested in the following thread of discussions:
http://answers.microsoft.com/en-us/windows/forum/windows_8-winapps/unable-to-remove-browser-choice-screen/a07924fa-b0f1-4c75-a111-cb665f5af3b2
S.Sengupta, Windows Entertainment and Connected Home MVP -
select a.address address,
s.hash_value hash_value,
s.piece piece,
s.sql_text sql_text,
u.username parsing_user_id,
c.username parsing_schema_id
from v$sqlarea a,
v$sqltext_with_newlines s,
dba_users u,
dba_users c
where a.address=s.address
and a.hash_value=s.hash_value
and a.parsing_user_id=u.user_id
and a.parsing_schema_id=c.user_id
and exists (select 'x'
from v$sqltext_with_newlines x
where x.address=a.address
and x.hash_value=a.hash_value
and upper(x.sql_text) like '%UNION%')
order by 1,2,3
On executing the above statement the server display list of sqltext present in sga. We know that the server first search for the record in sga and if it is not
present in SGA it searches in data files.
My question is ,adding all the sql statements in sga manually is advisable? will that improve the performance?Vinodh2 wrote:
In production the users uses some sql statement frequently. So those statements should be in the sga for better performance. The dbms package said by you takes object as input. What is object here?
Did you check the last line I pasted from document?
The value for this identifier is the concatenation of the address and hash_value columns from the v$sqlarea view.Each SQL in SQL AREA has a hash_value to identify it. You can use this hash value to decide which SQL want to keep in shared pool.
Having Dynamic sql statements(records fetched based on varying filter condtion) called from interfaces will not help.
But having static sql statements helps.
In that case how to include those sql.Like said, you are introducing some more problems instead of any real gain by trying to micro manage how shared pool work. You should let Oracle take care it with it's own algorithm. Unless some rare cases, that you have limited shared pool and super busy system you want to pin some SQL in the shared pool with provide package. -
DOWNLOAD CHEQUE FORMAT INTO A FLAT FILE
hi,
i have a requirement to download cheque format into a flat file.
as shown below. can any one guide me in this.
Check No. Date Payment Amt
1200175681 16/05/07 229.26 CAD
Pay TWO HUNDRED TWENTY NINE & 26/100*************************** CANADIAN D
To The MINISTER OF FINANCE
Order BARRIE SMALL CLAIMS COURT
Of 114 WORSLEY STREET
BARRIE ON L4M 1M1 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
BARRIESMCC 1200175681 16/05/07
Payee
MINISTER OF FINANCE
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
14/05/07 BARRIESMCC02MAY 0.00 CAD 229.26
Check No. Date Payment Amt
1200175682 16/05/07 149.96 CAD
Pay ONE HUNDRED FORTY NINE & 96/100**************************** CANADIAN D
To The BELLEVILLE ONTARIO HRDC
Order REMITTANCES
Of PO BOX 6767
MATANE QC G4W 4T1 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
BELLEVONHR 1200175682 16/05/07
Payee
BELLEVILLE ONTARIO HRDC
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
14/05/07 HRDCQC09MAY07 0.00 CAD 149.96
Check No. Date Payment Amt
1200175683 16/05/07 1,590.00 CAD
Pay ONE THOUSAND FIVE HUNDRED NINETY & 00/100****************** CANADIAN D
To The CANADIAN ENERGY EFFICIENCY
Order ALLIANCE
Of 2800 SKYMARK AVENUE
MISSISSAUGA ON L4W 5A6 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
CANENERGEF 02 1200175683 16/05/07
Payee
CANADIAN ENERGY EFFICIENCY
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
11/05/07 2117 0.00 CAD 1,590.00
Check No. Date Payment Amt
1200175684 16/05/07 8,701.83 CAD
Pay EIGHT THOUSAND SEVEN HUNDRED ONE & 83/100****************** CANADIAN D
To The CITY OF HAMILTON
Order ACCOUNTS RECEIVABLE
Of 120 KING ST WEST
SUITE 900, STANDARD LIFE BLDG ______________________________
HAMILTON ON L8P 4V2
Authorized Signature
Vendor ID Personnel ID Check No. Date
CITYHAMIL 01 1200175684 16/05/07
Payee
CITY OF HAMILTON
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
12/04/07 112949 0.00 CAD 8,701.83
Check No. Date Payment Amt
1200175685 16/05/07 23,104.38 CAD
Pay TWENTY THREE THOUSAND ONE HUNDRED FOUR & 38/100************ CANADIAN D
To The MINISTRY OF COMMUNITY AND
Order SOCIAL SERVICES, DIRECTOR
Of FAMILY RESPONSIBILITY OFFICE
PO BOX 2204 STATION P ______________________________
TORONTO ON M5S 3E9
Authorized Signature
Vendor ID Personnel ID Check No. Date
FAMILYREOF 1200175685 16/05/07
Payee
MINISTRY OF COMMUNITY AND
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
14/05/07 FRO02MAY07 0.00 CAD 23,104.38
Check No. Date Payment Amt
1200175686 16/05/07 553.85 CAD
Pay FIVE HUNDRED FIFTY THREE & 85/100************************** CANADIAN D
To The HAROLD MCQUAKER ENTERPRISES
Order LTD
Of BOX 538
EMO ON P0W 1E0 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
HAROLDMCEN 1200175686 16/05/07
Payee
HAROLD MCQUAKER ENTERPRISES
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
17/03/07 121 0.00 CAD 553.85
Check No. Date Payment Amt
1200175687 16/05/07 428,684.44 CAD
Pay **********428,684.44************************************* CANADIAN D
To The MERIDIAN CREDIT UNION
Order STE 160 S
Of 483 BAY ST
TORONTO ON M5G 2E1 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
HEPCOEDRUN 12 1200175687 16/05/07
Payee
MERIDIAN CREDIT UNION
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
14/05/07 MERIDIEN02MAY07 0.00 CAD 428,684.44
Check No. Date Payment Amt
1200175688 16/05/07 7,319.15 CAD
Pay SEVEN THOUSAND THREE HUNDRED NINETEEN & 15/100************* CANADIAN D
To The HYDRO ONE EMPLOYEES AND
Order PENSIONERS CHARITY TRUST
Of 483 BAY STREET 14TH FL
NORTH TOWER ______________________________
TORONTO ON M5G 2P5
Authorized Signature
Vendor ID Personnel ID Check No. Date
HYDROONEEP 1200175688 16/05/07
Payee
HYDRO ONE EMPLOYEES AND
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
14/05/07 CHARITY02MAY07 0.00 CAD 7,319.15
Check No. Date Payment Amt
1200175689 16/05/07 357.97 CAD
Pay THREE HUNDRED FIFTY SEVEN & 97/100************************* CANADIAN D
To The MINISTER OF FINANCE
Order KENORA SMALL CLAIMS COURT
Of 216 WATER STREET
KENORA ON P9N 1S4 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
KENORASMCC 1200175689 16/05/07
Payee
MINISTER OF FINANCE
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
14/05/07 KENORASMCC09MAY 0.00 CAD 357.97
Check No. Date Payment Amt
1200175690 16/05/07 692.41 CAD
Pay SIX HUNDRED NINETY TWO & 41/100**************************** CANADIAN D
To The MURRAY, WALTER
Order .
Of 1721 CEDAR LANE
BRACEBRIDGE ON P1L 1W9 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
MURRAYWALT 1200175690 16/05/07
Payee
MURRAY, WALTER
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
09/05/07 EXS2007710 0.00 CAD 692.41
Check No. Date Payment Amt
1200175691 16/05/07 2,437.44 CAD
Pay TWO THOUSAND FOUR HUNDRED THIRTY SEVEN & 44/100************ CANADIAN D
To The PACE, ROBERT
Order .
Of 5121 SACKVILLE ST, 7TH FLOOR
HALIFAX NS B3J 1K1 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
PACEROBERT 1200175691 16/05/07
Payee
PACE, ROBERT
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
09/05/07 EXS200711 0.00 CAD 2,437.44
Check No. Date Payment Amt
1200175692 16/05/07 2,718.35 CAD
Pay TWO THOUSAND SEVEN HUNDRED EIGHTEEN & 35/100*************** CANADIAN D
To The RECEIVER GENERAL FOR CANADA
Order CANADA CUSTOMS REVENUE AGENCY
Of 875 HERON RD
OTTAWA ON K1A 1B1 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
RECEIVEGEN 1200175692 16/05/07
Payee
RECEIVER GENERAL FOR CANADA
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
14/05/07 CRABNW02MAY07 0.00 CAD 2,718.35
Check No. Date Payment Amt
1200175693 16/05/07 7,688.00 CAD
Pay SEVEN THOUSAND SIX HUNDRED EIGHTY EIGHT & 00/100*********** CANADIAN D
To The RECEIVER GENERAL FOR CANADA
Order INDIAN & NORTHERN AFFAIRS
Of 100 ANEMKI DRIVE
RR #4 ______________________________
THUNDER BAY ON P7J 1A5
Authorized Signature
Vendor ID Personnel ID Check No. Date
RECEIVGECA 77 1200175693 16/05/07
Payee
RECEIVER GENERAL FOR CANADA
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
14/05/07 P2107132007 0.00 CAD 7,688.00
Check No. Date Payment Amt
1200175694 16/05/07 4,155.20 CAD
Pay FOUR THOUSAND ONE HUNDRED FIFTY FIVE & 20/100************** CANADIAN D
To The REUTERS CANADA LIMITED
Order CANADA
Of PO BOX 1519, STATION A
TORONTO ON M5W 3N9 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
REUTERINSV 1200175694 16/05/07
Payee
REUTERS CANADA LIMITED
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
01/04/07 ICA807040667 0.00 CAD 4,155.20
Check No. Date Payment Amt
1200175695 16/05/07 4,155.20 CAD
Pay FOUR THOUSAND ONE HUNDRED FIFTY FIVE & 20/100************** CANADIAN D
To The REUTERS CANADA LIMITED
Order CANADA
Of PO BOX 1519, STATION A
TORONTO ON M5W 3N9 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
REUTERINSV 1200175695 16/05/07
Payee
REUTERS CANADA LIMITED
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
01/05/07 ICA807050645 0.00 CAD 4,155.20
Check No. Date Payment Amt
1200175696 16/05/07 10,000.00 CAD
Pay TEN THOUSAND & 00/100************************************** CANADIAN D
To The WATERLOO REGIONAL CHILDRENS
Order MUSEUM
Of 10 KING ST W
KITCHENER ON N2G 1A3 ______________________________
Authorized Signature
Vendor ID Personnel ID Check No. Date
WATERLRECM 1200175696 16/05/07
Payee
WATERLOO REGIONAL CHILDRENS
Date Invoice No. PO/Cntrct Rel Discount/Wthld Cur Payment Amount
29/11/06 2006050034 0.00 CAD 10,000.00Raj:
You could create a 'Before Header' page process to create this 'flat file'. The code for the page process will be something similar to declare
v_file_name VARCHAR2 (2000) := 'fixedFieldsFile.txt';
--- Client Record Format
f1 char(20);
f2 char(20);
f3 char(20);
---- End format
begin
OWA_UTIL.mime_header ('application/txt', FALSE);
htp.p('Content-Disposition:attachment;filename="'|| v_file_name|| '"');
OWA_UTIL.http_header_close;
for c1 in (select * from emp) loop ----- The report query
f1:= c1.empno;f2:=c1.ename;f3:=c1.sal;
htp.p(f1||f2||f3);
end loop;
apex_application.g_unrecoverable_error:=true;
exception when others then
null;
end; Varad -
Hi there,
i've got a question to the refreshment of materialized views: when updating or inserting / deleting data of some specific tables, my materialized view should be updated.
First of all, i though to make that happen by using triggers on the specific table and the DBMS_MVIEW.REFRESH() mechanism. But as i have read pretty often, it isn't recommended doing that with triggers.
But how can I get what I want :-)
Thank a lot for your answers,
Martin
Edited by: 949697 on Mar 4, 2013 1:11 AMThanks for your answers,
the case with FAST REFRESH doesn't work. Our materialized are very complex and are using e.g subselect-statments, analytic functions,UNION, Order, Group by.... According to the documentation, we can't use the FAST REFRESH.
Using the REFRESH ON COMMIT could be possible. Having a look at the documentation, there is one interesting sentence: A REFRESH ON COMMIT materialized view is refreshed automatically when a transaction that does DML to one of the materialized view's detail tables commits
What are the materialized view's detail tables? Are that all tables in the FROM-Part of the query?
Martin -
Join, union, and order
I've been looking at some of the other posts on this and have not seen my exact problem (granted, I have not looked at all of the posts, as there are quite a few, so please forgive me if this has been covered before).
I have two tables, A & B, that I want to join. They have different data. They both contain fields 1 & 2 which I want to sort by. There are records in A that are not in B, and vice versa.
I've tried all sorts of things. The closest I've come to what I'm looking for looks like this:
select * from A, B
where A1 = B1 (+)
union
select * from A, B
where A1 (+) = B1
order by A2, A1
This gives me all the records I'm looking for, but not sorted in the right order. The problem is with records that are in B, but not in A. When sorting by A2, A1 the fields for records in B and not in A are null and appear at the end.
And yes, I've tried using numbers instead of field names in the order by. Didn't work either. I also tried using NVL to tell it to use B2 if A2 is null.
Right, left, and outer join do not work on the version I'm using.
Any ideas??here is the example
SQL> desc t
Name Null? Type
COL VARCHAR2(30 CHAR)
ID NUMBER
SQL> desc t_n
Name Null? Type
N NUMBER(38)
SQL> select * from t
2 union
select * from t_n;
select * from t
ERROR at line 1:
ORA-01789: query block has incorrect number of result columns
Note: above error coz no of columns are not same
SQL> ed
Wrote file afiedt.buf
1 select col,id from t
2 union
3* select n,null id from t_n
SQL> /
select col,id from t
ERROR at line 1:
ORA-01790: expression must have same datatype as corresponding expression
NOTE: got above error as the data type of the column are not same
SQL> ed
Wrote file afiedt.buf
1 select col,id from t
2 union
3* select null , n from t_n
SQL> /
COL ID
1#@#2#@#3 1
4#@#5#@#6 2
1
123
SQL> ed
Wrote file afiedt.buf
1 select col,id from t
2 union
3 select null , n from t_n
4* order by id
SQL> /
COL ID
1#@#2#@#3 1
1
4#@#5#@#6 2
123 -
Hi,
I wanted to know if this is possible with order by clause and union. some how union messes up the ordering of the left part of the union statement.
assume the data is as below in table tab1
c1 c2
4 1
4 2
5 1
5 2
7 1
7 2
8 3
9 4
the expected output must be for all c1 < 7 ordering must be on c1 and for all c1 > 7 ordering must be on c2.
so the query i tried was
select c1,c2 from tab1 where c1 <7 order by c1
union
select c1,c2 from tab1 where c1 >=7 order by c2
it is giving the message 'Sql command not properly ended'.
Suggestions are welcomeSQL>
SQL> With t As
2 (
3 Select 4 c1, 1 c2 From dual Union All
4 Select 4 ,2 From dual Union All
5 Select 5 ,1 From dual Union All
6 Select 5 ,2 From dual Union All
7 Select 7 ,1 From dual Union All
8 Select 7 ,2 From dual Union All
9 Select 8 ,3 From dual Union All
10 Select 9 ,4 From dual
11 ), t1 As (Select c1,c2 From t Where c1 <7 Order By c1)
12 ,t2 As (select c1,c2 from t where c1 >=7 Order By c2)
13 Select * From t1 Union All
14 Select * From t2;
C1 C2
4 1
4 2
5 1
5 2
7 1
7 2
8 3
9 4
8 rows selected
SQL> /*-- Union All
2 select c1,c2 from t where c1 >=7
3 Order By c1,c2;
4 */
5
with union clause order by must be placed in the end,,
select c1 from dual union all
select c2 from dual
order by c1 -
Query in Order By clause used with UNION
When tryna execute the below qurey I get
ORA-01784 - ORDER BY item must be the number of a SELECT-list expression
Please help me resolve this asap.
SELECT cm_workqueue.WQUEUEID_REF,cm_workqueue.WQUEUEID_CODE,cm_workqueue.mstr_queue_encode,cm_workqueue.desc_text
FROM 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
FROM 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 EXISTS (
SELECT 1
FROM 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 BY CASE
WHEN cm_workqueue.wqueueid_ref < 999 THEN 1
ELSE cm_workqueue.mstr_queue_encode
END,
LOWER (cm_workqueue.desc_text)To be correct, the column names in the order by must be the name of the column alias or the number of the selected column. If you don't give a column alias, the column name of the first select is taken.
SQL> create table t1 (a varchar2(1));
Table created.
SQL> create table t2 (aa varchar2(1));
Table created.
SQL> select a aa from t1
2 union
3 select aa from t2
4 order by aa;
no rows selectedDid you see how we nicely could order by the column name of the second select? NOT! It's because of the column alias. Look at the next select
SQL> select a aa from t1
2 union
3 select aa from t2
4 order by a;
order by a
ERROR at line 4:
ORA-00904: "A": invalid identifierDid you see, that the column name of the first select is not valid as there is a column alias?
That's why the best is, to use result set numbers when ordering in unions
SQL> select a aa from t1
2 union
3 select aa from t2
4 order by 1;
no rows selected
SQL> Edited by: Leo Mannhart on Mar 6, 2009 10:37 AM
And that's why the columns in the order by MUST be in the select list. Another error in the original post -
ORDER BY for the result of a UNION?
I have two SELECT statements which are merged by a UNION ALL.
Ok, I could add an ORDER BY to each of the SELECTs but that would sort only the partial result lists.
How can I sort the whole result table (after the merge)?
Peterputting the order by at the end of the last query.
SQL> select ename, sal from emp where sal>2000
2 union all
3 select ename, sal from emp where deptno=10;
ENAME SAL
JONES 2975
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
FORD 3000
CLARK 2450
KING 5000
MILLER 1300
9 rows selected.
SQL> select ename, sal from emp where sal>2000
2 union all
3 select ename, sal from emp where deptno=10
4 order by 2;
ENAME SAL
MILLER 1300
CLARK 2450
CLARK 2450
BLAKE 2850
JONES 2975
SCOTT 3000
FORD 3000
KING 5000
KING 5000
9 rows selected.Max
[My Italian Oracle blog| http://oracleitalia.wordpress.com/2010/02/07/aggiornare-una-tabella-con-listruzione-merge/] -
Bit Ordering for Structures in a Union
Hello. I seem to be having a problem with interpreting data placed into a char array that is overlayed with a structure. It seems to be related to bit ordering. The overall scope of my application is to take a data packet, send it over a serial communication port, then another application decodes the data packet, and places the data into a table. I built two seperate LabWindows applications that perform the tasks on both sides of the serial port, and they work properly. Each application makes use of the same header file containing structures defining the data packets. When I remove the LabWindows application on one side and replace it with my actual hardware, I start running into problems decoding the data sent over the serial port with the remaining LabWindows application.
The structure is as follows:
typedef struct
unsigned int lcu_status_id:4;
unsigned int fan_id: 1;
unsigned int dimv_id: 1;
unsigned int thunderstorm_id: 1;
unsigned int nvis_mode_id: 1;
unsigned int temp_id: 1;
unsigned int power_id: 1;
unsigned int fan_status_id: 1;
unsigned int lcu_fault: 1;
unsigned int undefined_bits: 4;
} LCU_STATUS_DEF;
typedef union
LCU_STATUS_DEF lcu_status_data;
unsigned char lcu_status_str[2];
When receiving data from the serial port (COM1), I use the following commands:
read_status = ComRd(1, buf, 1)
packets.lcu_status.lcu_status_str[0] = buf[0]; //packets is another structure not mentioned in above code containing LCU_STATUS and others
... Later in the code I fill data tables by assigning each table the value that is stored in the packets.lcu_status.lcu_status_data.lcu_status_id (or whichever of the variables in the structure I need)
I'm finding that the actual hardware sends the data across the serial port correctly, so that packets.lcu_status.lcu_status_str[0] (and subsequent words) is filled correctly. For example, if I send across a word containing 27 (decimal) then packets.lcu_status.lcu_status_str[0] is filled with 27 when I read it in debug mode. The next word I send is 16 and packets.lcu_status.lcu_status_str[1] is filled with 16. However,
I would expect the structure to then be filled like so:
lcu_status_id = 3
fan_id = 1
dimv_id = 0
thunderstorm_id = 1
nvis_mode_id = 1
temp_id = 0
power_id = 0
fan_status_id = 0
lcu_fault = 1
undefined_bits = 0
But instead, I get:
lcu_status_id = 0
fan_id = 1
dimv_id = 0
thunderstorm_id = 0
nvis_mode_id = 0
temp_id = 1
power_id = 1
fan_status_id = 0
lcu_fault = 1
undefined_bits = 3
I do not understand why this bit order occurs. Would anyone be able to explain?
Thank you,
JasonJason,
Seems to me that your hardware communicates in "big endian" (as many embedded systems do). This means that you will need to rearrange the bytes in the numeric data you receive before using the values. The same will apply to any numeric data you send.
Have a look at Roberto's reply in this thread. Also, here is an informative article discussing endianness.
Cheers,
Colin. -
TSQL verify sort order / UNION ALL
CREATE PROCEDURE Test
AS
BEGIN
SELECT * FROM (
SELECT 1 AS a,'test1' as b, 'query1' as c
UNION ALL
SELECT 2 AS a,'test22' as b, 'query22' as c
UNION ALL
SELECT 2 AS a,'test2' as b, 'query2' as c
UNION ALL
SELECT 3 AS a,'test3' as b, 'query3' as c
UNION ALL
SELECT 4 AS a,'test4' as b, 'query4' as c
) As sample
FOR XML RAW
END
Can we guarantee that the stored procedure returns results in given order?
Normally it says when we insert these select query to temporary table we can't guarantee its inserting order. So we have to use order by clause. But most of time it gives same order. Can we enforce to give it some different order? Is this related with clustered
and non clustered indices.
In second case can we enforce inserting order by adding Identity column?
Explain more on behind logic as well. I'm checking whether I need to add ORDER BY in old Stored Procedures or not.Can we guarantee that the stored procedure returns results in given order?
No. The only way to get a guaranteed order is to use an ORDER BY clause.
Normally it says when we insert these select query to temporary table we can't guarantee its inserting order. So we have to use order by clause. But most of time it gives same order. Can we enforce to give it some different order? Is this related with
clustered and non clustered indices.
The only way to get a guaranteed order is to use an ORDER BY clause. Anything else is happenstance.
In second case can we enforce inserting order by adding Identity column? Explain more on behind logic as well. I'm checking whether I need to add ORDER BY in old Stored Procedures or not.
Again: the only way to get a guaranteed order is to use an ORDER BY clause.
Erland Sommarskog, SQL Server MVP, [email protected] -
Hi all, I have a pair of statements connected by a UNION, where I need to sort the final result according to columns not appearing in any column list:
SELECT REF(d) FROM DOCUMENT_TABLE d
WHERE d.header$.parentKey = ?
UNION
SELECT REF(d) FROM DOCUMENT_TABLE d, CROSS_TABLE k
WHERE k.doc = d.key$ AND k.folder = ?
ORDER BY d.header$.name
this fails saying that d.header$.name is an invalid identifier (ORA-904), like being out of scope.
Both union branches succeed when run alone (select + order by).
How can I sort by colums without having them to be returned ?
In all examples I found columns are also retrieved and referenced by either alias or by position.
Thanks.Hi,
In a UNION query, you can only ORDER BY columns in the SELECT clause.
You can do the UNION in a sub-query, then do the ORDER BY in the main query, where you can ORDER BY anything:
WITH union_results AS
SELECT REF (d) AS ref_d
, name
FROM DOCUMENT_TABLE d
WHERE d.header$.parentKey = ?
UNION
SELECT REF (d) AS ref_d
, name
FROM DOCUMENT_TABLE d
, CROSS_TABLE k
WHERE k.doc = d.key$
AND k.folder = ?
SELECT REF_D
FROM union_results
ORDER BY name
;Your front end may be able to hide specified columns, so you could do this without a sub-query.
For example, in SQL*Plus you can say:
COLUMN name NOPRINT
SELECT REF (d) AS ref_d
, name
FROM DOCUMENT_TABLE d
WHERE d.header$.parentKey = ?
UNION
SELECT REF (d) AS ref_d
, name
FROM DOCUMENT_TABLE d
, CROSS_TABLE k
WHERE k.doc = d.key$
AND k.folder = ?
ORDER BY name
;The result set actually contains two columns, but SQL*Plus will only display one. -
Order By Parameter UNION Issue!!
Hi All,
i have a query in which i have to order by a parameter.
but my problem is order by works only for non-union query if i add union to the query it gives error.
please see following..
SQL> SELECT H.BOL_NUMBER BOL_NUMBER,
2 H.PICKUP_NUMBER,
3 to_char(H.PICKUP_DATE,'DD-MON-YYYY') PICKUP_DATE,
4 RAC.CUSTOMER_NAME CUSTOMER_NAME,
5 L.SALES_ORDER,
6 to_char(MAX(PH.DATE_CONFIRMED),'DD-MON-YYYY HH24:MI:SS') DATE_CONFIRMED
7 FROM APPS.SO_PICKING_HEADERS_ALL PH,
8 SMCOE.SMCOE_BOL_HEADERS H,
9 SMCOE.SMCOE_BOL_LINES L,
10 SO_HEADERS_ALL SHA,
11 SO_ORDER_TYPES_ALL SOTA,
12 APPS.RA_CUSTOMERS RAC,
13 APPS.SO_PICKING_LINES_ALL PL,
14 APPS.SO_PICKING_LINE_DETAILS PLD
15 WHERE H.BOL_HEADER_ID = L.BOL_HEADER_ID
16 AND L.SO_HEADER_ID = SHA.HEADER_ID
17 AND SHA.ORDER_TYPE_ID = SOTA.ORDER_TYPE_ID
18 AND L.CUSTOMER_ID = RAC.CUSTOMER_ID
19 AND H.PICKUP_DATE >= TO_DATE('3-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
20 AND H.PICKUP_DATE < TO_DATE('4-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
21 AND DECODE(NULL,NULL,'X',
22 RAC.CUSTOMER_NAME) = DECODE(NULL,NULL,'X',
23 NULL)
24 AND DECODE(634307,NULL,1,
25 H.BOL_NUMBER) = DECODE(634307,NULL,1,
26 634307)
27 AND decode('Only SO','Only SO','Z',RAC.CUSTOMER_NAME)=decode('Only SO','Only SO','Z','XXX')
28 AND PH.DATE_CONFIRMED IS NOT NULL
29 AND PH.PICKING_HEADER_ID=PL.PICKING_HEADER_ID
30 AND PL.PICKING_LINE_ID=PLD.PICKING_LINE_ID
31 AND PLD.PICKING_LINE_DETAIL_ID=L.PICKING_LINE_DETAIL_ID
32 AND NOT EXISTS (SELECT 1
33 FROM SO_PICKING_LINE_DETAILS SPLD,WSH_DELIVERIES WD
34 WHERE SPLD.DELIVERY_ID=WD.DELIVERY_ID
35 AND SPLD.PICKING_LINE_DETAIL_ID =L.PICKING_LINE_DETAIL_ID
36 AND WD.STATUS_CODE='OP'
37 )
38 GROUP BY H.BOL_NUMBER ,
39 H.PICKUP_NUMBER,
40 H.PICKUP_DATE ,
41 RAC.CUSTOMER_NAME ,
42 L.SALES_ORDER,
43 L.LINE_NUMBER,
44 L.GROSS_WEIGHT,
45 L.NET_WEIGHT,
46 L.TARE_WEIGHT
47 ORDER BY decode('CUSTOMER','DATE',PICKUP_DATE,'CUSTOMER',CUSTOMER_NAME,'PN',PICKUP_NUMBER,SALES_ORDER);
BOL_NUMBER PICKUP_N PICKUP_DATE CUSTOMER_NAME
SALES_ORDER
DATE_CONFIRMED
634307 SC22862 03-NOV-2006 A M CASTLE & CO
300014511
03-NOV-2006 19:05:01
634307 SC22862 03-NOV-2006 A M CASTLE & CO
300014542
03-NOV-2006 19:04:55
634307 SC22862 03-NOV-2006 A M CASTLE & CO
300014599
03-NOV-2006 19:04:56
634307 SC22862 03-NOV-2006 A M CASTLE & CO
300014637
03-NOV-2006 19:05:04
634307 SC22862 03-NOV-2006 CORROSION MATERIALS INC
14 rows selected.
SQL> ed
Wrote file afiedt.buf
line 72 truncated.
1 SELECT H.BOL_NUMBER BOL_NUMBER,
2 H.PICKUP_NUMBER,
3 to_char(H.PICKUP_DATE,'DD-MON-YYYY') PICKUP_DATE,
4 RAC.CUSTOMER_NAME CUSTOMER_NAME,
5 L.SALES_ORDER,
6 ' ' DATE_CONFIRMED
7 FROM SMCOE.SMCOE_BOL_HEADERS H,
8 SMCOE.SMCOE_BOL_LINES L,
9 APPS.RA_CUSTOMERS RAC
10 WHERE H.BOL_HEADER_ID = L.BOL_HEADER_ID
11 AND L.CUSTOMER_ID = RAC.CUSTOMER_ID
12 AND H.PICKUP_DATE >= TO_DATE('3-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
13 AND H.PICKUP_DATE < TO_DATE('4-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
14 AND DECODE(NULL,NULL,'X',
15 RAC.CUSTOMER_NAME) = DECODE(NULL,NULL,'X',
16 NULL)
17 AND DECODE(NULL,NULL,1,
18 H.BOL_NUMBER) = DECODE(NULL,NULL,1,
19 NULL)
20 AND SALES_ORDER NOT LIKE '4%'
21 AND SALES_ORDER NOT LIKE 'PO%'
22 AND SALES_ORDER <> '1'
23 AND L.CUSTOMER_NAME NOT LIKE 'REC%'
24 AND decode('Only SO','Product','Y',RAC.CUSTOMER_NAME)=decode('Only SO','Product','Y','XXX')
25 UNION
26 SELECT H.BOL_NUMBER BOL_NUMBER,
27 H.PICKUP_NUMBER,
28 to_char(H.PICKUP_DATE,'DD-MON-YYYY') PICKUP_DATE,
29 RAC.CUSTOMER_NAME CUSTOMER_NAME,
30 L.SALES_ORDER,
31 to_char(MAX(PH.DATE_CONFIRMED),'DD-MON-YYYY HH24:MI:SS') DATE_CONFIRMED
32 FROM APPS.SO_PICKING_HEADERS_ALL PH,
33 SMCOE.SMCOE_BOL_HEADERS H,
34 SMCOE.SMCOE_BOL_LINES L,
35 SO_HEADERS_ALL SHA,
36 SO_ORDER_TYPES_ALL SOTA,
37 APPS.RA_CUSTOMERS RAC,
38 APPS.SO_PICKING_LINES_ALL PL,
39 APPS.SO_PICKING_LINE_DETAILS PLD
40 WHERE H.BOL_HEADER_ID = L.BOL_HEADER_ID
41 AND L.SO_HEADER_ID = SHA.HEADER_ID
42 AND SHA.ORDER_TYPE_ID = SOTA.ORDER_TYPE_ID
43 AND L.CUSTOMER_ID = RAC.CUSTOMER_ID
44 AND H.PICKUP_DATE >= TO_DATE('3-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
45 AND H.PICKUP_DATE < TO_DATE('4-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
46 AND DECODE(NULL,NULL,'X',
47 RAC.CUSTOMER_NAME) = DECODE(NULL,NULL,'X',
48 NULL)
49 AND DECODE(634307,NULL,1,
50 H.BOL_NUMBER) = DECODE(634307,NULL,1,
51 634307)
52 AND decode('Only SO','Only SO','Z',RAC.CUSTOMER_NAME)=decode('Only SO','Only SO','Z','XXX')
53 AND PH.DATE_CONFIRMED IS NOT NULL
54 AND PH.PICKING_HEADER_ID=PL.PICKING_HEADER_ID
55 AND PL.PICKING_LINE_ID=PLD.PICKING_LINE_ID
56 AND PLD.PICKING_LINE_DETAIL_ID=L.PICKING_LINE_DETAIL_ID
57 AND NOT EXISTS (SELECT 1
58 FROM SO_PICKING_LINE_DETAILS SPLD,WSH_DELIVERIES WD
59 WHERE SPLD.DELIVERY_ID=WD.DELIVERY_ID
60 AND SPLD.PICKING_LINE_DETAIL_ID =L.PICKING_LINE_DETAIL_ID
61 AND WD.STATUS_CODE='OP'
62 )
63 GROUP BY H.BOL_NUMBER ,
64 H.PICKUP_NUMBER,
65 H.PICKUP_DATE ,
66 RAC.CUSTOMER_NAME ,
67 L.SALES_ORDER,
68 L.LINE_NUMBER,
69 L.GROSS_WEIGHT,
70 L.NET_WEIGHT,
71 L.TARE_WEIGHT
72* ORDER BY decode('CUSTOMER','DATE',PICKUP_DATE,'CUSTOMER',CUSTOMER_NAME,'PN',PICKUP_NUMBER,SALES_ORDER)
SQL> /
OERROR at line 72:
ORA-01785: ORDER BY item must be the number of a SELECT-list expression
SQL> spool off;Maybe you could try this:
select ...
from
select ...
from ..
UNION
select ...
from ...
order by ... -
Using order by with the UNION ALL operator
Hi,
I have 2 queries and i'm using UNION ALL to join both of them.
And i want to sort the final result based on a column.
When i try to do that, its not allowing me to use the ORDER BY clause.
Any suggestions??
Example
select * from xxx where job='Manager'
order by ename
union all
select * from yyy where job='Engineer'
order by ename
Thanks in advance
--KumarSQL> select * from test_emp where deptno = 10 order by ename
2 union all
3 select * from test_emp where deptno = 20 order by ename;
union all
ERROR at line 2:
ORA-00933: SQL command not properly ended
SQL> select * from test_emp where deptno = 10 --order by ename
2 union all
3 select * from test_emp where deptno = 20 order by ename;
select * from test_emp where deptno = 20 order by ename
ERROR at line 3:
ORA-00904: "ENAME": invalid identifier
SQL> select * from test_emp where deptno = 10 --order by ename
2 union all
3 select * from test_emp where deptno = 20 --order by ename;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698 BLAKE MANAGER 7839 01-MAY-81 3141 1 10
7782 CLARK MANAGER 7839 09-JUN-81 2700 10
7839 KING PRESIDENT 17-NOV-81 5512 10
7934 MILLER CLERK 7782 23-JAN-82 1433 10
7369 SMITH CLERK 7902 10-OCT-06 882 123 20
7566 JONES MANAGER 7839 10-OCT-06 3279 123 20
7788 SCOTT ANALYST 7566 11-OCT-06 3307 123 20
7876 ADAMS CLERK 7788 10-OCT-06 1212 123 20
7902 FORD ANALYST 7566 10-OCT-06 3307 123 20
9 rows selected.
SQL> select * from
2 (select * from test_emp where deptno = 10 union all
3 select * from test_emp where deptno = 20)
4 order by ename;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7876 ADAMS CLERK 7788 10-OCT-06 1212 123 20
7698 BLAKE MANAGER 7839 01-MAY-81 3141 1 10
7782 CLARK MANAGER 7839 09-JUN-81 2700 10
7902 FORD ANALYST 7566 10-OCT-06 3307 123 20
7566 JONES MANAGER 7839 10-OCT-06 3279 123 20
7839 KING PRESIDENT 17-NOV-81 5512 10
7934 MILLER CLERK 7782 23-JAN-82 1433 10
7788 SCOTT ANALYST 7566 11-OCT-06 3307 123 20
7369 SMITH CLERK 7902 10-OCT-06 882 123 20
9 rows selected.
SQL>
Maybe you are looking for
-
IS the problem in my PSU?
Hi guys, i am kinda new on this forum, but ive got a problem that is driving me crazy I have just build a new comp> AMD Athlon XP 2800+ (barton box), KT6V mobo, Apacer 512 MB DDR400 CL 2.5, FX 5200( i know is kinda crap but i am from very poor countr
-
Hi All, I am working in XI3.0. When I am executing a process with BPM, I am getting an error message like this. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> - <!-- Call Adapter --> - <SAP:Error xmlns:SAP="http://sap.com/xi/XI/Message/3
-
XML load limited to 20 child nodes? Why?
I use the attached code to load the attached xml file, but it will only load the first 20 nodes. I have moved the 20th to the 21st and the 21st up to the 20th and it will still load the first 20, so it isn't a formatting problem.
-
Automatically trim a varchar field in an update SQL
We have an ETL application that does an update with a varchar field. When the field contains spaces, we see that with an update these spaces are trimmed automatically. We wonder whether there is a HANA setting that can be set to disable this trim....
-
When opening a Reader doc, receive error asking to go to webpage and install Flash. Even though lastest version of flash is installed.