Sql Query Returns repeating rows
Oracle Version : 10g R2.
DDL & DML
CREATE TABLE T1
( CCODE VARCHAR2(4 BYTE),
PNUM VARCHAR2(16 BYTE),
CTYPCDE VARCHAR2(4 BYTE)
INSERT INTO T1 VALUES ('0001','003526892','0007');
INSERT INTO T1 VALUES ('0001','06767019','0006');
INSERT INTO T1 VALUES ('0001','14787-10-900','0007');
INSERT INTO T1 VALUES ('0002','003445803','0009');
INSERT INTO T1 VALUES ('0002','000000','0018');
INSERT INTO T1 VALUES ('0002','001645156','0008');
INSERT INTO T1 VALUES ('0002','001646283','0008');
INSERT INTO T1 VALUES ('0025','002587509','0008');
INSERT INTO T1 VALUES ('0025','02507462', '0008');
INSERT INTO T1 VALUES ('0025','02565229', '0008');
INSERT INTO T1 VALUES ('0025','000943965','0007');
CREATE TABLE XWLK
( CTYPCDE VARCHAR2(4 BYTE),
CODE VARCHAR2(85 BYTE)
INSERT INTO XWLK VALUES ( '0004','70');
INSERT INTO XWLK VALUES ( '0005','05');
INSERT INTO XWLK VALUES ( '0006','05');
INSERT INTO XWLK VALUES ( '0007','05');
INSERT INTO XWLK VALUES ( '0007','10');
INSERT INTO XWLK VALUES ( '0008','15');
INSERT INTO XWLK VALUES ( '0008','20');
INSERT INTO XWLK VALUES ( '0009','25');
INSERT INTO XWLK VALUES ( '0010','75');
INSERT INTO XWLK VALUES ( '0010','80');
INSERT INTO XWLK VALUES ( '0011','75');
INSERT INTO XWLK VALUES ( '0011','80');
INSERT INTO XWLK VALUES ( '0012','75');
INSERT INTO XWLK VALUES ( '0012','80');
INSERT INTO XWLK VALUES ( '0013','75');
INSERT INTO XWLK VALUES ( '0013','80');
INSERT INTO XWLK VALUES ( '0014','75');
INSERT INTO XWLK VALUES ( '0014','80');
INSERT INTO XWLK VALUES ( '0015','75');
INSERT INTO XWLK VALUES ( '0015','80');
INSERT INTO XWLK VALUES ( '0016','75');
INSERT INTO XWLK VALUES ( '0016','80');
INSERT INTO XWLK VALUES ( '0017','15');
INSERT INTO XWLK VALUES ( '0017','70');
INSERT INTO XWLK VALUES ( '0018','15');
INSERT INTO XWLK VALUES ( '0018','25');
INSERT INTO XWLK VALUES ( '0018','70');
INSERT INTO XWLK VALUES ( '0019','25');
INSERT INTO XWLK VALUES ( '0019','70');
INSERT INTO XWLK VALUES ( '0020','70');
INSERT INTO XWLK VALUES ( '0021','75');
INSERT INTO XWLK VALUES ( '0021','80');
INSERT INTO XWLK VALUES ( '0022','75');
INSERT INTO XWLK VALUES ( '0022','80');
INSERT INTO XWLK VALUES ( '0023','10');
INSERT INTO XWLK VALUES ( '0025','05');
INSERT INTO XWLK VALUES ( '0025','10');
INSERT INTO XWLK VALUES ( '0025','25');
INSERT INTO XWLK VALUES ( '0025','30');
INSERT INTO XWLK VALUES ( '0025','35');
INSERT INTO XWLK VALUES ( '0025','40');
INSERT INTO XWLK VALUES ( '0025','45');
INSERT INTO XWLK VALUES ( '0025','50');
INSERT INTO XWLK VALUES ( '0025','55');
INSERT INTO XWLK VALUES ( '0025','60');
INSERT INTO XWLK VALUES ( '0025','65');
INSERT INTO XWLK VALUES ( '0026','75');
INSERT INTO XWLK VALUES ( '0026','80');
INSERT INTO XWLK VALUES ( '0027','05');
INSERT INTO XWLK VALUES ( '0027','10');
INSERT INTO XWLK VALUES ( '0027','25');
INSERT INTO XWLK VALUES ( '0027','30');
INSERT INTO XWLK VALUES ( '0027','35');
INSERT INTO XWLK VALUES ( '0027','40');
INSERT INTO XWLK VALUES ( '0027','45');
INSERT INTO XWLK VALUES ( '0027','50');
INSERT INTO XWLK VALUES ( '0027','55');
INSERT INTO XWLK VALUES ( '0027','60');
INSERT INTO XWLK VALUES ( '0027','65');
INSERT INTO XWLK VALUES ( '0028','15');
INSERT INTO XWLK VALUES ( '0069','75');
CREATE TABLE PTB
( PNUM VARCHAR2(20 BYTE));
INSERT INTO PTB VALUES('003526892');
INSERT INTO PTB VALUES('06767019');
INSERT INTO PTB VALUES('14787-10-900');
INSERT INTO PTB VALUES('003445803');
INSERT INTO PTB VALUES('000000');
INSERT INTO PTB VALUES('001645156');
INSERT INTO PTB VALUES('001646283');
INSERT INTO PTB VALUES('002587509');
INSERT INTO PTB VALUES('02507462');
INSERT INTO PTB VALUES('02565229');
INSERT INTO PTB VALUES('000943965');
Query
SELECT
SAK.CCODE,
SAK.CTYPCDE,
CASE
WHEN SAK.CCODE = '0001' and SAK.CTYPCDE != '0088' THEN '75'
WHEN SAK.CCODE = '0002' and SAK.CTYPCDE != '0088' THEN '75'
WHEN SAK.CCODE = '0002' and SAK.CTYPCDE != '0088' THEN '80'
WHEN SAK.CCODE = '0003' and SAK.CTYPCDE != '0088' THEN '80'
ELSE XWLK.CODE
END AS PCD,
PTB.PNUM
FROM T1 SAK INNER JOIN XWLK ON SAK.CTYPCDE = XWLK.CTYPCDE
INNER JOIN PTB ON SAK.PNUM = PTB.PNUM
Order by 4
Output
CCODE CTYPCODE PCD PNUM
0002 0018 75 000000
0002 0018 75 000000
0002 0018 75 000000
0025 0007 05 000943965
0025 0007 10 000943965
0002 0008 75 001645156
0002 0008 75 001645156
0002 0008 75 001646283
0002 0008 75 001646283
0025 0008 15 002587509
0025 0008 20 002587509
0002 0009 75 003445803
0001 0007 75 003526892
0001 0007 75 003526892
0025 0008 20 02507462
0025 0008 15 02507462
0025 0008 15 02565229
0025 0008 20 02565229
0001 0006 75 06767019
0001 0007 75 14787-10-900
0001 0007 75 14787-10-900
But the Output is not the expected one if we see for the PNUM = 000000, the records are displayed
3 times it should be only ones displayed and also for PNUM = 001645156. My Query is why is it displaying
more than once, is there anyway that we can display it only once. Can some please advice me where am making
wrong. Thanks in advance.
Hi Frank,
Thanks for your responce. My earlier Insert scripts the sample data had few issues, Please find below the updated one....
Oracle Version : 10g R2.
DDL & DML
CREATE TABLE T1
( CCODE VARCHAR2(4 BYTE),
PNUM VARCHAR2(16 BYTE),
CTYPCDE VARCHAR2(4 BYTE)
INSERT INTO T1 VALUES ('0512', '000-06-5393-05', '0023');
INSERT INTO T1 VALUES ('0305', '000-06-5393-05', '0006');
INSERT INTO T1 VALUES ('0611', '0000', '0006');
INSERT INTO T1 VALUES ('0806', '0000', '0008');
INSERT INTO T1 VALUES ('0606', '0000', '0006');
INSERT INTO T1 VALUES ('0099', '0000', '0007');
INSERT INTO T1 VALUES ('0025', '000000324', '0008');
CREATE TABLE XWLK
( CTYPCDE VARCHAR2(4 BYTE),
CODE VARCHAR2(85 BYTE)
INSERT INTO XWLK VALUES ( '0004','70');
INSERT INTO XWLK VALUES ( '0005','05');
INSERT INTO XWLK VALUES ( '0006','05');
INSERT INTO XWLK VALUES ( '0007','05');
INSERT INTO XWLK VALUES ( '0007','10');
INSERT INTO XWLK VALUES ( '0008','15');
INSERT INTO XWLK VALUES ( '0008','20');
INSERT INTO XWLK VALUES ( '0009','25');
INSERT INTO XWLK VALUES ( '0010','75');
INSERT INTO XWLK VALUES ( '0010','80');
INSERT INTO XWLK VALUES ( '0011','75');
INSERT INTO XWLK VALUES ( '0011','80');
INSERT INTO XWLK VALUES ( '0012','75');
INSERT INTO XWLK VALUES ( '0012','80');
INSERT INTO XWLK VALUES ( '0013','75');
INSERT INTO XWLK VALUES ( '0013','80');
INSERT INTO XWLK VALUES ( '0014','75');
INSERT INTO XWLK VALUES ( '0014','80');
INSERT INTO XWLK VALUES ( '0015','75');
INSERT INTO XWLK VALUES ( '0015','80');
INSERT INTO XWLK VALUES ( '0016','75');
INSERT INTO XWLK VALUES ( '0016','80');
INSERT INTO XWLK VALUES ( '0017','15');
INSERT INTO XWLK VALUES ( '0017','70');
INSERT INTO XWLK VALUES ( '0018','15');
INSERT INTO XWLK VALUES ( '0018','25');
INSERT INTO XWLK VALUES ( '0018','70');
INSERT INTO XWLK VALUES ( '0019','25');
INSERT INTO XWLK VALUES ( '0019','70');
INSERT INTO XWLK VALUES ( '0020','70');
INSERT INTO XWLK VALUES ( '0021','75');
INSERT INTO XWLK VALUES ( '0021','80');
INSERT INTO XWLK VALUES ( '0022','75');
INSERT INTO XWLK VALUES ( '0022','80');
INSERT INTO XWLK VALUES ( '0023','10');
INSERT INTO XWLK VALUES ( '0025','05');
INSERT INTO XWLK VALUES ( '0025','10');
INSERT INTO XWLK VALUES ( '0025','25');
INSERT INTO XWLK VALUES ( '0025','30');
INSERT INTO XWLK VALUES ( '0025','35');
INSERT INTO XWLK VALUES ( '0025','40');
INSERT INTO XWLK VALUES ( '0025','45');
INSERT INTO XWLK VALUES ( '0025','50');
INSERT INTO XWLK VALUES ( '0025','55');
INSERT INTO XWLK VALUES ( '0025','60');
INSERT INTO XWLK VALUES ( '0025','65');
INSERT INTO XWLK VALUES ( '0026','75');
INSERT INTO XWLK VALUES ( '0026','80');
INSERT INTO XWLK VALUES ( '0027','05');
INSERT INTO XWLK VALUES ( '0027','10');
INSERT INTO XWLK VALUES ( '0027','25');
INSERT INTO XWLK VALUES ( '0027','30');
INSERT INTO XWLK VALUES ( '0027','35');
INSERT INTO XWLK VALUES ( '0027','40');
INSERT INTO XWLK VALUES ( '0027','45');
INSERT INTO XWLK VALUES ( '0027','50');
INSERT INTO XWLK VALUES ( '0027','55');
INSERT INTO XWLK VALUES ( '0027','60');
INSERT INTO XWLK VALUES ( '0027','65');
INSERT INTO XWLK VALUES ( '0028','15');
INSERT INTO XWLK VALUES ( '0069','75');
CREATE TABLE PTB
( PSAK Number(10,0),
PNUM VARCHAR2(20 BYTE),
Seq_num NUMBER(5,0));
INSERT INTO PTB VALUES('9462003', '000-06-5393-05', '1');
INSERT INTO PTB VALUES('9462004', '000-06-5393-05', '2');
INSERT INTO PTB VALUES('9462008', '0000', '1');
INSERT INTO PTB VALUES('9462009', '0000', '2');
INSERT INTO PTB VALUES('9462010', '0000', '3');
INSERT INTO PTB VALUES('9462017', '000000324', '1');
Query
SELECT
PTB.PSAK,
CASE
WHEN SAK.CCODE = '0001' and SAK.CTYPCDE != '0088' THEN '75'
WHEN SAK.CCODE = '0002' and SAK.CTYPCDE != '0088' THEN '75'
WHEN SAK.CCODE = '0002' and SAK.CTYPCDE != '0088' THEN '80'
WHEN SAK.CCODE = '0003' and SAK.CTYPCDE != '0088' THEN '80'
ELSE XWLK.CODE
END AS PCD,
PTB.PNUM,
PTB.Seq_num
FROM T1 SAK INNER JOIN XWLK ON SAK.CTYPCDE = XWLK.CTYPCDE
INNER JOIN PTB ON SAK.PNUM = PTB.PNUM
Order by 4,3
Query output without distinct
PSAK PCD PNUM SEQ_NUM
9462003 05 000-06-5393-05 1
9462003 10 000-06-5393-05 1
9462008 05 0000 1
9462008 10 0000 1
9462008 20 0000 1
9462008 15 0000 1
9462008 05 0000 1
9462008 05 0000 1
9462017 20 000000324 1
9462017 15 000000324 1
9462004 10 000-06-5393-05 2
9462004 05 000-06-5393-05 2
9462009 05 0000 2
9462009 20 0000 2
9462009 05 0000 2
9462009 15 0000 2
9462009 05 0000 2
9462009 10 0000 2
9462010 05 0000 3
9462010 05 0000 3
9462010 15 0000 3
9462010 10 0000 3
9462010 05 0000 3
9462010 20 0000 3
Query output with distinct
PSAK PCD PNUM SEQ_NUM
9462003 05 000-06-5393-05 1
9462003 10 000-06-5393-05 1
9462008 05 0000 1
9462008 10 0000 1
9462008 15 0000 1
9462008 20 0000 1
9462017 15 000000324 1
9462017 20 000000324 1
9462004 05 000-06-5393-05 2
9462004 10 000-06-5393-05 2
9462009 05 0000 2
9462009 10 0000 2
9462009 15 0000 2
9462009 20 0000 2
9462010 05 0000 3
9462010 10 0000 3
9462010 15 0000 3
9462010 20 0000 3
But both the output is wrong is there any way that i can get the result set as below, from the above query i can see duplicates
if the PNUM has more SEQ_NUM then it is again doing a cross join i think and hence the result set is coming wrong. The Below
is the expected output. Can some one help me out in getting this. Thanks in advance.
PSAK PCD PNUM SEQ_NUM
9462003 05 000-06-5393-05 1
9462004 10 000-06-5393-05 2
9462008 05 0000 1
9462009 05 0000 2
9462010 15 0000 3
9462010 20 0000 3
9462017 15 000000324 1
9462017 20 000000324 1
Similar Messages
-
Hi I joined two table and created the new table called FMW_ANALY. Which has some repeating rows. I used the following Query to list the top 10 customers by value
SELECT *
FROM
(SELECT CUSTOMER, OPP_VALUE, RANK() OVER (order by OPP_VALUE DESC) opp_val_rank
FROM FMW_ANALY)
WHERE opp_val_rank<=10;
I'm getting the following result
Customer Opp Value Opp Val Rank
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Department of Education and Training 8991776.99 1
Investments Commission 7447419.84 22
Bank Limited 4803000 23
Bank Limited 4803000 23
Bank Limited 4803000 23
Bank Limited 4803000 23
Bank Limited 4803000 23
Bank Limited 4803000 23
Bank Limited 4803000 23
Bank Limited 4803000 23
How can I Query without these repeating rows?
Thank you
VickExample which will help you understand Rank, Dense Rank
SQL> ed
Wrote file afiedt.buf
1 select deptno, ename,sal,
2 rank()
3 over (order by sal desc)rnk,
4 dense_rank()
5 over (order by sal desc)densernk
6 from emp2
7* order by sal desc
SQL> /
DEPTNO ENAME SAL RNK DENSERNK
10 KING2 5000 1 1
10 KING 5000 1 1
20 SCOTT2 3630 3 2
20 SCOTT 3630 3 2
20 JONES2 3272 5 3
20 JONES 3272 5 3
20 FORD2 3000 7 4
20 FORD 3000 7 4
30 BLAKE2 2850 9 5
30 BLAKE 2850 9 5
10 CLARK2 2450 11 6
10 CLARK 2450 11 6
30 ALLEN2 1600 13 7
30 ALLEN 1600 13 7
30 TURNER2 1500 15 8
30 TURNER 1500 15 8
10 MILLER2 1300 17 9
10 MILLER 1300 17 9
30 WARD2 1250 19 10
30 WARD 1250 19 10
30 MARTIN2 1250 19 10
30 MARTIN 1250 19 10
20 ADAMS2 1100 23 11
20 ADAMS 1100 23 11
30 JAMES2 950 25 12
30 JAMES 950 25 12
20 SMITH2 800 27 13
20 SMITH 800 27 13
28 rows selected.
SQL> ed
Wrote file afiedt.buf
1 SELECT * FROM
2 (
3 select deptno, ename,sal,
4 rank()
5 over (order by sal desc)rnk,
6 dense_rank()
7 over (order by sal desc)densernk
8 from emp2
9 order by sal desc
10 )
11* WHERE DENSERNK <=5
SQL> /
DEPTNO ENAME SAL RNK DENSERNK
10 KING2 5000 1 1
10 KING 5000 1 1
20 SCOTT2 3630 3 2
20 SCOTT 3630 3 2
20 JONES2 3272 5 3
20 JONES 3272 5 3
20 FORD2 3000 7 4
20 FORD 3000 7 4
30 BLAKE2 2850 9 5
30 BLAKE 2850 9 5
10 rows selected. -
SQL Query returning no rows, please help!!
I have a table that contains user audits for a particular procedures alongwith the date stamp. Now, I want to list all the procedures not accessed by the users within the last 6 months. Or, all the procedures that have not been used/accessed during the last 6 months.
This is what I am trying but is not returning any rows:
SELECT DISTINCT proc_name,
TRUNC (entry_date)
FROM log_web
WHERE proc_name NOT IN (SELECT proc_name
FROM log_web
WHERE TRUNC (entry_date) > TRUNC (SYSDATE - 180))
ORDER BY 2 DESC
Please advise.
Thank you in advance.Two possibilities leap to mind.
First, are you sure that there are any prodcedures not accessed in the last six months?
Second, is there a chance that there could be records with a null value in proc_name? If the sub-query used in a NOT IN predicate returns even one null value, the query will return no rows.
Does this return rows?
SELECT DISTINCT proc_name, TRUNC(entry_date)
FROM log_web
WHERE proc_name NOT IN (SELECT proc_name
FROM log_web
WHERE TRUNC (entry_date) > TRUNC (SYSDATE - 180) and
proc_name IS NOT NULL)
ORDER BY 2 DESCJohn -
SQL query returns few rows, but holds on with several Gbs of RAM memory used
If I perform the following query:
SELECT d.DocumentGUID, d.DocumentID
FROM ImportDataBase.dbo.Document d
LEFT OUTER JOIN ContentDataBase.dbo.Document d2 ON (d.DocumentGUID = d2.DocumentGUID)
WHERE ( d2.DocumentGUID IS NULL ) -- new document in the ImportDB
OR ( d2.DocumentGUID IS NOT NULL AND d.QueryContent <> d2.QueryContent ) -- modified document in the ImportDB
It returns around 1000 rows and takes about 3 minutes to complete
It also raises up the RAM memory used from 2GB to 9GBs.
This memory used will remain used untill I restart the server. I have no need to make use of that memory, I already copied the returned rows of that query into a note pad for example. And any other reason of SQL has to to keep that memory stored by
that query, I dont want it.
Is there a way to release that memory I really dont need my SQL server to keep, without having to restart the SQL server?
and without topping the max memory the sql server uses, since I need my SQL server to use as much as it needs in other tasks(I dont think it needs to hold on to the above query memory used)
Thank You very much.Is there a way to release that memory I really dont need my SQL server to keep, without having to restart the SQL server?
and without topping the max memory the sql server uses, since I need my SQL server to use as much
A query can be resource intensive even if it returns a single row. Generally it is a good idea to set SQL Server MAX memory.
BOL: "Optimizing Server Performance Using Memory Configuration Options
The memory manager component of Microsoft SQL Server eliminates the need for manual management of the memory available to SQL Server. When SQL Server starts, it dynamically determines how much memory to allocate based on how much memory the operating system
and other applications are currently using. As the load on the computer and SQL Server changes, so does the memory allocated. For more information, see Memory Architecture.
The following server configuration options can be used to configure memory usage and affect server performance:
•min server memory
•max server memory
•max worker threads
•index create memory
•min memory per query
The min server memory server configuration option can be used to ensure that SQL Server does not release memory below the configured minimum server memory once that threshold is reached. This configuration option can be set to a specific value based on the
size and activity of your SQL Server. If you choose to set this value, set it to some reasonable value to ensure that the operating system does not request too much memory from SQL Server, which can affect SQL Server performance.
The max server memory server configuration option can be used to specify the maximum amount of memory SQL Server can allocate when it starts and while it runs. This configuration option can be set to a specific value if you know there are multiple applications
running at the same time as SQL Server and you want to guarantee that these applications have sufficient memory to run. If these other applications, such as Web or e-mail servers, request memory only as needed, then do not set the max server memory server
configuration option, because SQL Server releases memory to them as needed. However, applications often use whatever memory is available when they start and do not request more if needed. If an application that behaves in this manner runs on the same computer
at the same time as SQL Server, set the max server memory server configuration option to a value that guarantees that the memory required by the application is not allocated by SQL Server."
LINK:
http://technet.microsoft.com/en-us/library/ms177455(v=sql.105).aspx
Memory configuration:
http://www.sqlusa.com/bestpractices/memory-configuration/
Kalman Toth Database & OLAP Architect
SQL Server 2014 Database Design
New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014 -
XML attributes makes my query return no rows
Hello everyone,
I've an odd problem.
I'm querying some XML, but the attributes in one of the tags make my query return no rows; if I remove the attributes, then the query works as expected.
The XML is below; it's the attributes in the Report tag that cause the issues:
<result errorCode="0">
<return>
<Report
xsi:schemaLocation="Items_x0020_status_x0020_information http://******-****/ReportServer?%2FReports%2FContent%20Producer%20Reports%2FItems%20status%20information&rs%3AFormat=xml&rc%3ASchema=True"
Name="Items status information" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="Items_x0020_status_x0020_information">
<Tablix1>
<Details_Collection>
<Details ItemId="914P7" Username="test" user_role="IT"
first_name="Barry" last_name="Donovan" organisation=""
content_format="On_Screen" modified_date="26/05/2011 13:16:49"
item_status="Draft" status_date="" component_name="" demand="" />
</Details_Collection>
</Tablix1>
</Report>
</return>
</result>My query is:
select
a.item_id
,a.username
,a.user_role
,a.first_name
,a.last_name
,a.supplier_id
,a.format
,a.modified_date
,a.item_status
,a.completion_date
,a.component_code
from dual
,xmltable
('/result/return/Report/Tablix1/Details_Collection/Details'
passing p_xml
columns
item_id varchar2(1000) path '@ItemId'
,username varchar2(1000) path '@Username'
,user_role varchar2(1000) path '@user_role'
,first_name varchar2(1000) path '@first_name'
,last_name varchar2(1000) path '@last_name'
,supplier_id varchar2(1000) path '@organisation'
,format varchar2(1000) path '@content_format'
,modified_date varchar2(1000) path '@modified_date'
,item_status varchar2(1000) path '@item_status'
,completion_date varchar2(1000) path '@status_date'
,component_code varchar2(1000) path '@demand'
) a;I've tried stripping out the attributes in the tag, which does work, but some of the XML I'm expecting back may be quite large (many records), so that caused issues in itself. I'd rather deal with it and not mess with the XML itself if possible.
Any help would be hugely appreciated!
Thank you very much in advance.
Robin
Edited by: User_resU on Apr 12, 2012 2:50 PMExample:
SQL> ed
Wrote file afiedt.buf
1 with t as (select xmltype('<result errorCode="0">
2 <return>
3 <Report
4 xsi:schemaLocation="Items_x0020_status_x0020_information http://******-****/ReportServer?%2FReports%2FContent%20Producer%20Reports%2FItems%20status%20information&rs%3AFormat=xml&rc%3ASchema=True"
5 Name="Items status information" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 xmlns="Items_x0020_status_x0020_information">
7 <Tablix1>
8 <Details_Collection>
9 <Details ItemId="914P7" Username="test" user_role="IT"
10 first_name="Barry" last_name="Donovan" organisation=""
11 content_format="On_Screen" modified_date="26/05/2011 13:16:49"
12 item_status="Draft" status_date="" component_name="" demand="" />
13 </Details_Collection>
14 </Tablix1>
15 </Report>
16 </return>
17 </result>') as xml from dual)
18 --
19 -- end of test data
20 --
21 select
22 a.item_id
23 ,a.username
24 ,a.user_role
25 ,a.first_name
26 ,a.last_name
27 ,a.supplier_id
28 ,a.format
29 ,a.modified_date
30 ,a.item_status
31 ,a.completion_date
32 ,a.component_code
33 from t
34 ,xmltable
35 (xmlnamespaces('Items_x0020_status_x0020_information' as "x0"),
36 '//x0:Report/x0:Tablix1/x0:Details_Collection/x0:Details'
37 passing xml
38 columns
39 item_id varchar2(1000) path '@ItemId'
40 ,username varchar2(1000) path '@Username'
41 ,user_role varchar2(1000) path '@user_role'
42 ,first_name varchar2(1000) path '@first_name'
43 ,last_name varchar2(1000) path '@last_name'
44 ,supplier_id varchar2(1000) path '@organisation'
45 ,format varchar2(1000) path '@content_format'
46 ,modified_date varchar2(1000) path '@modified_date'
47 ,item_status varchar2(1000) path '@item_status'
48 ,completion_date varchar2(1000) path '@status_date'
49 ,component_code varchar2(1000) path '@demand'
50* ) a
SQL> /
ITEM_ID
USERNAME
USER_ROLE
FIRST_NAME
LAST_NAME
SUPPLIER_ID
FORMAT
MODIFIED_DATE
ITEM_STATUS
COMPLETION_DATE
COMPONENT_CODE
914P7
test
IT
Barry
Donovan
On_Screen
26/05/2011 13:16:49
Draft -
Creating a directory via a jsp page sql query return
Is it possible to create a directory based on the results of a sql query?
i.e.
My sql query returns application number 1234 for a building application (BC for short).
Is it possible to create a directory /BC/1234
It is then proposed to use an iframe to look into the directory so users can place and access scanned documents which are captured during the approval process.
If so how?
An example would be great.
JasonSorry hope your still around I've been away for a while. We've created a samber share to the file server where the documents are stored and mounted it within the web file directory. It works ok if there is an existing directory but if there isn't we want it to be created when the page looks for the folder. I've done this with an asp page it basically did a directory exist test first then create folder if it didn't. The problem is can't find an example of how to do the directory exist test then directory create syntax so an example of both would be great
-
SQL query returns varchar - default value type!!!
On a field in my portal form there is a default value type of 'SQL query returns varchar', where can I put this sql to get a default value from a table?
Hi again
I have this code in 'additional pl/sql code' - '...before displaying the page'. But it comes up with an error when I run the form.
declare
prop_v varchar2(20);
group_v varchar2(3);
el1_v varchar2(3);
blk varchar2(10) := 'DEFAULT';
begin
prop_v := p_session.get_value_as_varchar2(p_block_name=>'DEFAULT', p_attribute_name => 'l_property_p');
group_v := p_session.get_value_as_varchar2(p_block_name=>'DEFAULT', p_attribute_name => 'l_group_p');
select element_code
into el1_v
from eh_risk_element_detail
where property_ref = prop_v
and group_code = group_v
and line_no = 1;
p_session.set_value(p_block_name => blk, p_attribute_name => 'L_ELEMENT1_P', p_value => el1_v);
end;
Rich -
[Excel] Running a SQL Query to delete rows
Hello Experts,
Background: I am attempting to use a dba of my companies time keeping system and implement it with Power BI tools. Given the file size restrictions within Power Bi itself I need to lower my file size by removing all time logs from
inactive employees.
I have a question regarding whether or not you can use a sql query to delete rows in excel. I have roughly 200,000+ rows in my excel spreadsheet. I am attempting to delete all rows where an employee equals inactive. I have attempted to
delete these rows by sorting them and doing a bulk delete and clear contents, but it seems to crash my excel every time. My thought process is that using a query that does a timed delete might put less of a burden on deleting the massive amount of data.
I would like to use this: DELETE * FROM [Table_(...)_Actual$] WHERE [Current] = "Inactive" (Will add more once I know it is possible to use sql queries in Excel.
Any information on whether or not this is possible would be appreciated.
Regards,
LinkRunning SQL Query in Excel is possible, however, the delete query is not supported in Excel.
You are more restricted in deleting Excel data than data from a relational data source. In a relational database, "row" has no meaning or existence apart from "record"; in an Excel worksheet, this is not true. You can delete values
in fields (cells). Please see:http://support.microsoft.com/kb/257819/en-us
One workaround : Use update query to set the rows as null, then use select query.
e.g.
SQL = "update [sheet2$A1:B5] set name=null,age=null where name='andy'"
cnn.Execute SQL
SQL = "select name,age from [sheet2$A1:B5] where name is not null"
Wind Zhang
TechNet Community Support -
I just wanted to note this on the forum..
I'm using Apex 3.0.1.00.08
My DEV environment has just been upgraded from 10g version 10.2.0.2.0 to 10.2.0.4.0.
I created the following process to select some values into some items on my Form page.
select MAX(STAT_YEAR+1)
, pcd.pct_id
, pcd.stat_type_id
, pcd.stat_period_type_id
, pcd.measurement_id
into :P11_STAT_YEAR
, :P11_PCT_ID
, :P11_STAT_TYPE_ID
, :P11_STAT_PERIOD_TYPE_ID
, :P11_MEASUREMENT_ID
from plant_commodity_data pcd
, stat_type stt
where pcd.STAT_TYPE_ID = stt.STAT_TYPE_ID
and pcd.pct_id = :P0_PCT_ID
and stt.stat_type = 'PRD'
group by pcd.pct_id
, pcd.stat_type_id
, pcd.stat_period_type_id
, pcd.measurement_id;The process should run conditionally if there was at least one row to select...
So I copied the SQL into the Process condition, removed the "into" section and set the condition type to be "Exists (SQL query retruns at least one row) ...
select MAX(STAT_YEAR+1)
, pcd.pct_id
, pcd.stat_type_id
, pcd.stat_period_type_id
, pcd.measurement_id
from plant_commodity_data pcd
, stat_type stt
where pcd.STAT_TYPE_ID = stt.STAT_TYPE_ID
and pcd.pct_id = :P0_PCT_ID
and stt.stat_type = 'PRD'
group by pcd.pct_id
, pcd.stat_type_id
, pcd.stat_period_type_id
, pcd.measurement_id;This worked perfectly until the DEV environment was upgraded from 10g version 10.2.0.2.0 to 10.2.0.4.0.
The condition would fire even if there were no rows returning.
I pasted the condition code into SQL Developer connected to the DEV (10.2.0.4.0) environment and it returned no rows.
To solve the problem, I removed the MAX.
You can test this using this code if you have access to the two versions... select MAX(1)
, sysdate
, 'Gus'
from dual
where 1 = 2
group by sysdate, 2Now I know the MAX isn't required in the condition as I'm just trying to find out if any rows exist, but it was there as I copied the code in... I was just wondering why this happened between 10.2.0.2.0 and 10.2.0.4.0?
Gus..Hi Gus,
try to execute
select count(*) from dual where exists (select MAX(1)
, sysdate
, 'Gus'
from dual
where 1 = 2
group by sysdate, 2)in SQL Developer. The above statement is generated by APEX for an "Exists (SQL query retruns at least one row)". Can't test it, because I don't have a 10.2.0.4.0 at hand.
Does SQL Developer now show the same behavior?
Patrick
My APEX Blog: http://www.inside-oracle-apex.com
The APEX Builder Plugin: http://builderplugin.oracleapex.info/
The ApexLib Framework: http://apexlib.sourceforge.net/ -
Repeating frame not visible when the query returns no rows
I've developed a report whose output looks like this:
Subinventory | Part Code |Part Description |Ordered Qty | Received Qty
Mentone St | BATT | non serialised item | |
Mentone St | SONY | spare parts MIN MAX | 30| 0
In the above report
subinventory, Part Code, Part description are in one repeating frame and Ordered and received qty are in other repeating frame.
for a perticular part code there may not be ordered or received quantities. Means the seond query fetches no rows for the perticulat partcode. In that case report is showing null(blank) but I want to print ZERO there.
If I use NVL in the query it'll effect only when the query fetches some rows.
I've tried with formula columns. for example in the formula column
IF :ordered_qty IS NULL THEN
v_ordered_qty :=0;
ELSE
v_ordered_qty := :ordered_qty;
END IF;
return(v_ordered_qty);
I've assigned this formula column as source to the Ordered Qty Filed. Then also its not working.
Any help in this regards is highly apprecialted
regards,
Vijmay be you can modify your code like below:
SELECT i.subinventory, i.part_code, i.part_description, i.min_qty, i.max_qty,
NVL (j.quantity, 0) ordered_qty,
NVL (j.quantity_delivered, 0) received_qty
FROM (SELECT DISTINCT c.secondary_inventory subinventory,
b.segment1 part_code, b.description part_description,
c.min_minmax_quantity min_qty,
c.max_minmax_quantity max_qty, b.inventory_item_id,
b.organization_id
FROM mtl_system_items_b b,
mtl_item_sub_inventories_all_v c
WHERE b.inventory_item_id = c.inventory_item_id
AND b.organization_id = c.organization_id
AND UPPER (c.secondary_inventory) =
NVL (UPPER (DECODE (:p_sub_inv,
'ALL', '',
:p_sub_inv
UPPER (c.secondary_inventory)
)) i,
(SELECT mtrl.inventory_item_id, mtrl.organization_id,
mtrl.to_subinventory_code,
NVL (SUM (mtrl.quantity), 0) quantity,
NVL (SUM (mtrl.quantity_delivered), 0) quantity_delivered
FROM mtl_txn_request_lines mtrl, mtl_system_items_b msi
WHERE mtrl.inventory_item_id = msi.inventory_item_id
AND mtrl.organization_id = msi.organization_id
AND mtrl.reference_type_code = 2
AND UPPER (mtrl.to_subinventory_code) =
NVL (UPPER (DECODE (:p_sub_inv, 'ALL', '', :p_sub_inv)),
UPPER (mtrl.to_subinventory_code)
AND TRUNC (mtrl.creation_date)
BETWEEN NVL (TRUNC (TO_DATE (:p_from_date,
'yyyy/mm/dd hh24:mi:ss'
TRUNC (mtrl.creation_date)
AND NVL (TRUNC (TO_DATE (:p_to_date,
'yyyy/mm/dd hh24:mi:ss'
TRUNC (mtrl.creation_date)
GROUP BY mtrl.inventory_item_id,
mtrl.organization_id,
mtrl.to_subinventory_code) j
WHERE i.inventory_item_id = j.inventory_item_id(+)
AND i.organization_id = j.organization_id(+) -
Query return no rows in Answers but retrun rows in sql
Hi all,
I have the following query which return 3 rows in SQL promple but return no row in Answer and Execute direct request what is the problem any idea?
select abc_date,abc_asset_desc,sum(abc_market_val_lcy+abc_int_accr_lcy) "Stock"
from abc
group by abc_date,abc_asset_descRegardsHi,
i really appriciate your reply
pls tell me briefly from where i can set log-level > 2 ?? and from where check the physical log of SQL ?
Followig message is arise at server log
ORA-01455 converting column overflow integer datatype at OCI call OCIStmtFetch,Bulk fetched failed
Message was edited by:
53637 -
Silly old fogey (me) cannot figure out why this query returns 1 row
Hi all,
In reference to {thread:id=2456973}, why does
select sum(count(decode(job, 'CLERK', 1, null))) CLERKS
, sum(count(decode(job, 'SALESMAN', 1, null))) SALESMANS
from emp group by job;only return 1 row and not 1 for each job? I actually had to test it myself to believe it.
It returns data as if the query were
select sum(CLERKS), sum(SALESMANS)
from (select count(decode(job, 'CLERK', 1, null)) CLERKS, count(decode(job, 'SALESMAN', 1, null)) SALESMANS
from emp group by job)Using only a single aggregate (either count or sum) returns 1 row per job, as expectedJohn Stegeman wrote:
It returns data as if the query were
select sum(CLERKS), sum(SALESMANS)
from (select count(decode(job, 'CLERK', 1, null)) CLERKS, count(decode(job, 'SALESMAN', 1, null)) SALESMANS
from emp group by job)
Exactly the point ;-)
Seems like Oracle actually can do a "double group by" in the same operation.
Witness the explain plans in this example:
SQL> select count(decode(job, 'CLERK', 1, null)) CLERKS
2 , count(decode(job, 'SALESMAN', 1, null)) SALESMANS
3 from scott.emp group by job;
CLERKS SALESMANS
0 0
0 0
0 0
0 4
4 0
Execution Plan
Plan hash value: 1697595674
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 5 | 40 | 4 (25)| 00:00:01 |
| 1 | HASH GROUP BY | | 5 | 40 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 112 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------And compare it to this one with the double aggregates:
SQL> select sum(count(decode(job, 'CLERK', 1, null))) CLERKS
2 , sum(count(decode(job, 'SALESMAN', 1, null))) SALESMANS
3 from scott.emp group by job;
CLERKS SALESMANS
4 4
Execution Plan
Plan hash value: 417468012
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 8 | 4 (25)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 8 | 4 (25)| 00:00:01 |
| 2 | HASH GROUP BY | | 1 | 8 | 4 (25)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMP | 14 | 112 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------There is both HASH GROUP BY and SORT AGGREGATE.
It does not really make sense to do an aggregate on an aggregate - if both aggregates are used "on the same group-by level".
The sum() aggregates are used upon an already aggregated value, so it does look like Oracle actually treats that as "first do the inner aggregate using the specified group by and then do the outer aggregate on the result with no group by."
Look at this example where I combine "double" aggregates with "single" aggregates:
SQL> select sum(count(decode(job, 'CLERK', 1, null))) CLERKS
2 , sum(count(decode(job, 'SALESMAN', 1, null))) SALESMANS
3 , count(decode(job, 'SALESMAN', 1, null)) SALESMANS2
4 , count(*) COUNTS
5 from scott.emp group by job;
CLERKS SALESMANS SALESMANS2 COUNTS
4 4 1 5
Execution Plan
Plan hash value: 417468012
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 8 | 4 (25)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 8 | 4 (25)| 00:00:01 |
| 2 | HASH GROUP BY | | 1 | 8 | 4 (25)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMP | 14 | 112 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------When mixing "double" and "single" aggregates, Oracle decides that single aggregates belong in the "outer" aggregation.
SALESMAN2 is doing a count on the aggregated job column that is the result of the "inner" group by - therefore only 1.
The count(*) also counts the result of the "inner" aggregation.
I am not sure if this is documented or if it is a "sideeffect" of either the internal code used for GROUPING SETS or the internal code used for allowing analytic functions like this:
SQL> select count(decode(job, 'CLERK', 1, null)) CLERKS
2 , count(decode(job, 'SALESMAN', 1, null)) SALESMANS
3 , sum(count(decode(job, 'CLERK', 1, null))) over () CLERKS2
4 , sum(count(decode(job, 'SALESMAN', 1, null))) over () SALESMANS2
5 from scott.emp group by job;
CLERKS SALESMANS CLERKS2 SALESMANS2
0 0 4 4
4 0 4 4
0 0 4 4
0 0 4 4
0 4 4 4
Execution Plan
Plan hash value: 4115955660
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 5 | 40 | 4 (25)| 00:00:01 |
| 1 | WINDOW BUFFER | | 5 | 40 | 4 (25)| 00:00:01 |
| 2 | SORT GROUP BY | | 5 | 40 | 4 (25)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMP | 14 | 112 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------Personally I think I would have preferred if Oracle raised an error on this "double aggregation" and thus require me to write it this way (if that is the result I desired):
select sum(CLERKS), sum(SALESMANS)
from (select count(decode(job, 'CLERK', 1, null)) CLERKS, count(decode(job, 'SALESMAN', 1, null)) SALESMANS
from emp group by job)I can not really think of good use-cases for the "double aggregation" - but rather that it could give you unnoticed bugs in your code if you happen to do double aggregation without noticing it.
Interesting thing to know ;-) -
APEX,PDF's, BI Publisher and SQL Query returning SQL code..
I don't know if I should be posting this in this Forum or the BI Publisher forum, so I am posting in BOTH forums..
I love APEX, let me say that first.. And appreciate the support offered here by the group, but am running int a confusing issue when BI Publisher tries to build a report from the above type APEX report..
Here is my dilemma:
I have a number of reports that are part of a Oracle package. They return an SQL Query back to a reports region on a page. I am having to deal with the column names returned are col01, col02..
The issue I have is, when building the Application Level query to download the XML sample from in building RTF layouts in Word, you can not use this code, you MUST use a standard SQL Select.
I have taken the sql from the function returning sql, and copied into the application query, supplying the required data values for bind variables being used in the query.
An XML file is produced, and I use this to build the RTF format file that I load back into APEX and try to use it for the PDF rendering of the report. I can view the output as a PDF in the Word add on, but when I try using it with the report, it is returning an empty PDF file.
Can anyone tell me what error log files on the bi publisher side I can look at to see what error is happening?
Thank you,
Tony Miller
UTMB/EHN
Title adjusted to allow people to know what I am talking about...
Message was edited by:
Tony MillerTony,
You can find the log as follows:
- go to http://[yourserver]:[yourport]/em
- logon to OC4J EM: oc4jadmin/[yourpassword]
- click on "logs" at the bottom of the page
- in the hgrid/tree, expand OC4J->home->Application
xmlpserver
- click on view log icon
You can also observe what's going on in BI Publisher
by going to the command prompt from where you started
it.
Or, as a third option, you can locate the file on
your file system, depending on your setup, the path
would be something similar to this:
\oracle\product\10.2.0\bip\j2ee\home\application-deplo
yments\xmlpserver\application.log
With that said though, I don't expect you'll find
much in there that would help with your particular
problem. I suspect you either get no rows in your XML
at runtime, due to some session state issues, or your
XML structure does in fact not match your RTF
template.
I'm not quite following your problem description,
i.e. when did you do what and are you associating
your report layout with a report query or report
region. So just some general notes, your query needs
to be parseable at design-time, when exporting the
XML, so that you get the XML file with the proper
column names derived from your query. If you want to
use your RTF template with a standard report region,
you must export the XML file first using the advanced
XML structure option. And of course the column names
in your report query need to match the column names
in your report region.
Perhaps this helps you further diagnose what's going
on, if you have additional information that could
help, let me know. And if you could stage this on
apex.oracle.com, I'd be happy to take a look.
Regards,
MarcMarc,
Thanks for looking at this issue. Below find my remarks to your questions..
Re: your query needs
to be parseable at design-time, when exporting the
XML, so that you get the XML file with the proper
column names derived from your query.At the start of this process, the query code was a function in a package. The function was returning an SQL select statement, for a report region on a page. I took the select statement, built an application query to build a sample of the xml for BI Publisher desktop (Add-on for Word). The code was producing the usual Col01, Col02.. since at design time that is were the column names.
When I then took the xml from this and built the rtf for loading into my APEX application.
When testing the Application Query with this RTF report layout, I am getting PDF's. When using it with the report region sending an xml feed to BI Publisher I am getting nothing back.
I have since taken the sql code and moved it back into the report region, and set the region to have a type of straight SQL Query. I have even tried to hard-code the parameters I was getting from the page to limit data returned.
Is it possible to see the xml being produced by the APEX page?
Re: Stage this on apex.oracle.com.. I would love to, but we would have HIPPA issues if I posted the data on a public website.
Can I send you the RTF file and the xml file that the application query is creating to see if there something weird about them?
Thank you,
Tony Miller
UTMB/EHN -
Need help in SQL Query: Update a row in a table & insert the same row into another table
I want to update a row in a table say Table A and the updated row should be inserted into another table say Table B. I need to do it in a single SQL query and i don't want to do it in PL/SQL with triggers. And i tried with MERGE statement but its working with this scenario. (Note: I'm using Oracle Database 10g Enterprise Edition Release 10.2.0.1.0).
Thanks in Advance.Using Sven's code as an example, you could save the updated row in a sql plus variable. (also untested):
SQL> var v_id number
update tableA
set colB='ABC'
where colC='XYZ'
returning id into :v_id;
insert into table A_History (ID, colA, colB, ColC)
select id, ColA, ColB, ColC
from tableA
where id = :v_id; -
Query returns more row than expected
1. select * from view_name where col1 = 'value1' returns 12 rows
2. select * from (view script) where col1 = 'value1' returns 24 rows
i have a view called view_name. If i use view_name directly in the query, it returns 12 rows. But if i use the select script directly in from clause, it returns more rows. I am not able to find out why it is happening so. Any pointers will be helpful.Are you saying that the SQL for view_name and view_script are identical? Can you post them?
Maybe you are looking for
-
I'm fed up with my local (Huntington Beach, CA) cable provider "improving my experience" by moving channels to their "digital lineup". As retribution, I'm seriously considering switching to a Verizon FIOS bundle (phone, internet, TV). I've some quest
-
I can't install itunes update 11.1.4 for Windows 7 (64-bit).
I have already tried this: http://support.apple.com/kb/TS5376 but it doesn't work
-
Filtering the rows of a JTable based on JCombobox selection
Hi,,, I write an application where I have a combobox contains the JTable first column data when i click on the item it should give me the rows, which contains the selected item as their first column data. I am enable to get the list like that, please
-
Find all Package and their related Procedure Names using a specifc Table
I have 25 Packages Each Package holds 30-35 Procedures I need to find out all Packages and Procedures Each Procedure handles 5 - 20 Tables as Per The Need of Business Rule. I need All Package and Related Procedure Names Where a Specific Table Name Ap
-
Read color/shortcut keys for Actions in Actions Palette?
Is it possible to use Javascript to read the color and shortcut key assigned to an action using Javascript? I'm looping through and building a list of actionsets/actions into an XML file and would like to include the color and shortcut as well. Than