Row_number() with distinct
How to use row_number() with distinct ?
I want all the selected in distinct display but it only can display (a) table. How can i add more table in select row_number() ??
Here is my query.
SELECT
ROW_NUMBER() OVER (ORDER BY a.student_number) as No
FROM
SELECT DISTINCT
TO_CHAR(TO_DATE(c.study_intake, 'yyyymm'), upper('mon yyyy')) AS Study_intake,
a.student_number,
a.name,
b.course_code,
b.academic_semester,
e.qualification,
e.school,
e.state,
f.school_town,
a.permanent_tel_no,
a.mobile_no
FROM
student_data a,
student_course_master b,
student_course_detail_subject c,
student_qualification e,
school_master f
WHERE
a.student_number = b.student_number
AND b.student_key = c.student_key
AND e.student_key = a.student_key
AND b.student_course_status IN ('Completed','Active')
AND e.school = f.school_name(+)
AND e.state = f.state(+)
)a
Did not get any help from them ;)
Regards,
Guru
Similar Messages
-
Picking records with distinct col values
Hi gurus,
I have a table with
id col1 col2 col3
1 a b c
2 e f g
3 a b c
4 a b c
what would be the query to pick records with distinct col1,col2,col3 values.i.e records with col values 1,a,b,c and 2,e,f,g only in the above table
Could you please help me on this?
Best Regards
SridhaSQL> with t as
2 (
3 select 1 COL1,'A' COL2,'B' COL3,'C' COL4 FROM DUAL UNION ALL
4 select 2,'D' COL1,'E' COL2,'F' COL3 FROM DUAL UNION ALL
5 select 3,'A' COL1,'B' COL2,'C' COL3 FROM DUAL UNION ALL
6 select 4,'G' COL1,'H' COL2,'I' COL3 FROM DUAL UNION ALL
7 select 5,'D' COL1,'E' COL2,'F' COL3 FROM DUAL
8 )
9 SELECT COL1, COL2, COL3, COL4
10 FROM T
11 /
COL1 C C C
1 A B C
2 D E F
3 A B C
4 G H I
5 D E F
SQL> with t as
2 (
3 select 1 COL1,'A' COL2,'B' COL3,'C' COL4 FROM DUAL UNION ALL
4 select 2,'D' COL1,'E' COL2,'F' COL3 FROM DUAL UNION ALL
5 select 3,'A' COL1,'B' COL2,'C' COL3 FROM DUAL UNION ALL
6 select 4,'G' COL1,'H' COL2,'I' COL3 FROM DUAL UNION ALL
7 select 5,'D' COL1,'E' COL2,'F' COL3 FROM DUAL
8 )
9 SELECT COL1, COL2, COL3, COL4
10 FROM
11 (
12 SELECT COL1, COL2, COL3, COL4, ROW_NUMBER () OVER (PARTITION BY COL2, COL3, COL3 ORDER BY COL1) AS RN
13 FROM T
14 )
15 WHERE RN=1
16 /
COL1 C C C
1 A B C
2 D E F
4 G H I
SQL> -
Problem in using sum with distinct in case
Hi all,
I want to use a sum function with distinct and also with case.
below is the format i am using.
Sum( distinct case when a.value='TOOL' then e.value else 0 end)
This is working but i don't get distinct records.
SO i tried using in this way
Sum( case when a.value='TOOL' then distinct e.value else 0 end)
But I am getting missing expression error.
Please help me to resolve this issue
Thanks
PriyaSTEP -2
SQL>
SQL> commit;
Commit complete.
SQL> ed
Wrote file afiedt.buf
1 Select Sum(Value)
2 From (
3 Select Distinct Case when value='TOOL'
4 then value
5 else
6 0
7 end Value
8 From Blah
9* )
SQL> /
0
ERROR at line 6:
ORA-00932: inconsistent datatypes: expected CHAR got NUMBERRegards.
Satyaki De. -
Physical queries generated with DISTINCT clause
Hi All,
We have a model with three dimension tables and a fact table. All the join conditions between dimension and fact tables are defined and dimension with level keys.
Apart from this, one of the dimension is used for applying the row level filter.
Oracle BI is generating physical queries with DISTINCT clause, which is impacting the performance of the query exeuction. Any tips in modification to business model to avoid the generation of DISTINCT clause would be appreciated.
Thanks,
Phanihi,
Open RPD-->Double click on Logical table source in BMM layer and go to content tab .you will have Select Distinct Values by default it was uncheck if that is checked uncheck that one....(this one is only to restrict from BMM layer)
thanks,
saichand.v -
How Can I increase performance of Query with Distinct Keyword
Dear Experts,
In my Query when I execute this without Distinct it give result very soon,
But when I excute this with Distinct its performance is very slow is there any option for increase the performance with Distinct .
I have to use Distinct according to our rewuirement.neither DISTINCT nor GROUP BY do sort. If you need to sort, use ORDER BY. Slight correction, they do a sort, but your results are not guarenteed to be sorted.
The overhead of a sort is incurred, if your results is large enough (my test below isn't) you will be sorting to disk. Which as we all know can be very slow.
9i:
SQL> SET TRIMSPOOL ON
SQL> CREATE TABLE D (A VARCHAR2(2));
Table created.
SQL> INSERT INTO D (A) SELECT DBMS_RANDOM.STRING('U',2) FROM DUAL CONNECT BY LEVEL <= 5000;
5000 rows created.
SQL> COMMIT;
Commit complete.
SQL> SET AUTOTRACE TRACEONLY EXPLAIN;
SQL> SELECT DISTINCT A FROM D;
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 SORT (UNIQUE)
2 1 TABLE ACCESS (FULL) OF 'D'
SQL> SELECT A FROM D GROUP BY A;
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 SORT (GROUP BY)
2 1 TABLE ACCESS (FULL) OF 'D'
SQL> SELECT DISTINCT A FROM D ORDER BY A;
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 SORT (UNIQUE)
2 1 TABLE ACCESS (FULL) OF 'D'
SQL> SPOOL OFF10g
SQL> SET TRIMSPOOL ON
SQL> CREATE TABLE D (A VARCHAR2(2));
Table created.
SQL> INSERT INTO D (A) SELECT DBMS_RANDOM.STRING('U',2) FROM DUAL CONNECT BY LEVEL <= 5000;
5000 rows created.
SQL> COMMIT;
Commit complete.
SQL> SET AUTOTRACE TRACEONLY EXPLAIN;
SQL> SELECT DISTINCT A FROM D;
Execution Plan
Plan hash value: 3079699766
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 5000 | 15000 | 7 (29)| 00:00:01 |
| 1 | HASH UNIQUE | | 5000 | 15000 | 7 (29)| 00:00:01 |
| 2 | TABLE ACCESS FULL| D | 5000 | 15000 | 5 (0)| 00:00:01 |
Note
- dynamic sampling used for this statement
SQL> SELECT A FROM D GROUP BY A;
Execution Plan
Plan hash value: 2712634873
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 5000 | 15000 | 7 (29)| 00:00:01 |
| 1 | HASH GROUP BY | | 5000 | 15000 | 7 (29)| 00:00:01 |
| 2 | TABLE ACCESS FULL| D | 5000 | 15000 | 5 (0)| 00:00:01 |
Note
- dynamic sampling used for this statement
SQL> SELECT DISTINCT A FROM D ORDER BY A;
Execution Plan
Plan hash value: 1773491675
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 5000 | 15000 | 8 (38)| 00:00:01 |
| 1 | SORT UNIQUE | | 5000 | 15000 | 7 (29)| 00:00:01 |
| 2 | TABLE ACCESS FULL| D | 5000 | 15000 | 5 (0)| 00:00:01 |
Note
- dynamic sampling used for this statement
SQL> SPOOL OFF -
[Solved] How to update records in entity based on view with distinct
Hi
- I have a relational view of the form:
SELECT DISTINCT
FROM TAB1, TAB2, ...- I created instead of triggers for delete, update and insert.
- I created an entity object on this and an updatable view object
- I added this to my module.
- Then I test the view object in the module (module > test), and if I try to update an existing record, I receive the error:
(java.sql.SQLException) ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc.
So, ADF internally create as select FOR UPDATE cursor to update the modified record and as the error shows, this is not possible on a view with distinct, ...
Is there a way to work around this error without to rewrite the view?
I was thinking that there is maybe a way to force ADF not to use the select FOR UPDATE and use UPDATE .. SET <value> WHERE <pk>, or maybe to create a new instead of trigger to catch the select FOR UPDATE?Arti,
Here's another solution (that will still use locking, and can help avoid the "lost update" problem):
http://stegemanoracle.blogspot.com/2006/03/using-updatable-views-with-adf.html
John -
ORA-01446: cannot select ROWID from, or sample, a view with DISTINCT, GROUP
In my data model (load from a database) there is this data control and its accessor return.
When I drag the accessor return inside a jsf page I create a adf read only table (that has about 5000 rows), the I run the page and it works.
Since I want to display only 20 ( or 50 or 100 is the same) rows for each page I had changed the access mode from the default value "Scrollable" to "Range Paging" and then I ve selected range size 50.
Now if I run hte jsf page there are no data to display and if I press the small triangles to order the column or insert a word in the filter field I have the message ORA-01446: cannot select ROWID from, or sample, a view with DISTINCT, GROUP.
I dont know how to do to solve this problem.
StefanoAn other thing.
I solved this problem, maybe I dont understand the meaning of access mode and range size , since every value I choose of these 2 fields the visualization of the table doesnt change. -
Dears,
i have this problem after i create tabular from depend on view
ORA-01446: cannot select ROWID from, or sample, a view with DISTINCT, GROUP BY, etc.
this a query that i use
select
"INVOICE_DET",
"INVOICE_DET" INVOICE_DET_DISPLAY,
"INVOICE_ID",
"STORAGE_CODE",
"ITEM_QNTY",
"ITEM_PRICE",
"BONS_QNTY",
"DISC_VALUE",
"TOT_VAL",
"LOCATION_CODE",
"BATCH_DET",
"ITEM_CODE",
"ITEM_NAME",
"UOM_CODE",
"UOM_NAME",
"SIZE_CODE",
"SIZE_NAME",
"COLOR_CODE",
"COLOR_NAME",
"STOREG_BRCHAS_BATCHN",
"EXPR_DATE",
"PROD_DATE",
"ITEM_DET_ID"
from "#OWNER#"."BRCHAS_INVOICE_DET_VIEW"
this the view i created
CREATE OR REPLACE FORCE VIEW "RETAIL"."BRCHAS_INVOICE_DET_VIEW" ("INVOICE_DET", "INVOICE_ID", "STORAGE_CODE", "ITEM_QNTY", "ITEM_PRICE", "BONS_QNTY", "DISC_VALUE", "TOT_VAL", "LOCATION_CODE", "BATCH_DET", "ITEM_CODE", "ITEM_NAME", "UOM_CODE", "UOM_NAME", "SIZE_CODE", "SIZE_NAME", "COLOR_CODE", "COLOR_NAME", "STOREG_BRCHAS_BATCHN", "EXPR_DATE", "PROD_DATE", "ITEM_DET_ID") AS
SELECT invoice_det, invoice_id, storage_code, item_qnty, item_price,
bons_qnty, disc_value, tot_val, location_code, batch_det, item_code,
item_name, uom_code, uom_name, size_code, size_name, color_code,
color_name, storeg_brchas_batchn, expr_date, prod_date, item_det_id
FROM (SELECT d.invoice_det, d.invoice_id, d.storage_code, d.item_qnty,
d.item_price, d.bons_qnty, d.disc_value, d.tot_val,
d.location_code, d.batch_det, i.item_code,
get_item_name (i.item_code) item_name, i.uom_code,
get_uom_desc (i.uom_code) uom_name, i.size_code,
get_size_name (i.size_code) size_name, i.color_code,
get_color_name (i.color_code) color_name,
dd.storeg_brchas_batchn, b.expr_date, b.prod_date,
i.item_det_id
FROM brchas_invoice_det d,
brchas_batch dd,
brchas_batch_det b,
item_uom_size_color i
WHERE dd.storeg_brchas_batchinternn = b.storeg_brchas_batchinternn
AND d.batch_det = b.batch_det
AND b.item_det_id = i.item_det_id
AND d.batch_det IS NOT NULL
UNION ALL
SELECT d.invoice_det, d.invoice_id, d.storage_code, d.item_qnty,
d.item_price, d.bons_qnty, d.disc_value, d.tot_val,
d.location_code, d.batch_det, i.item_code,
get_item_name (i.item_code) item_name, i.uom_code,
get_uom_desc (i.uom_code) uom_name, i.size_code,
get_size_name (i.size_code) size_name, i.color_code,
get_color_name (i.color_code) color_name,
NULL storeg_brchas_batchn, NULL expr_date, NULL prod_date,
i.item_det_id
FROM brchas_invoice_det d, item_uom_size_color i
WHERE d.item_det_id = i.item_det_id
AND d.batch_det IS NULL) ;
CREATE OR REPLACE TRIGGER "RETAIL"."BRCHAS_INVOICE_DET_VIEW_TRG"
INSTEAD OF DELETE OR INSERT OR UPDATE
ON RETAIL.BRCHAS_INVOICE_DET_VIEW
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
V_PRICE NUMBER;
V_BONUS_QNTY NUMBER;
V_DISC_VAL NUMBER;
BEGIN
IF INSERTING THEN
INSERT INTO BRCHAS_INVOICE_DET
(INVOICE_DET,
INVOICE_ID ,
STORAGE_CODE,
ITEM_QNTY,
ITEM_PRICE,
BATCH_DET,
BONS_QNTY,
TOT_VAL,
LOCATION_CODE,
DISC_VALUE,
CREATED_USER,
CREATED_DATE,
ITEM_DET_ID
VALUES
("BRCHAS_INVOICE_DET_SEQ".nextval,
:NEW.INVOICE_ID ,
:NEW.STORAGE_CODE,
:NEW.ITEM_QNTY,
:NEW.ITEM_PRICE,
:NEW.BATCH_DET,
:NEW.BONS_QNTY,
(:NEW.ITEM_QNTY * NVL(:NEW.ITEM_PRICE,0)) - NVL(:NEW.DISC_VALUE,0),
:NEW.LOCATION_CODE,
:NEW.DISC_VALUE,
nvl(v('APP_USER'),user),
sysdate,
:NEW.ITEM_DET_ID);
ELSIF UPDATING THEN
UPDATE BRCHAS_INVOICE_DET
SET INVOICE_ID =:NEW.INVOICE_ID,
STORAGE_CODE =:NEW.STORAGE_CODE,
ITEM_QNTY =:NEW.ITEM_QNTY,
ITEM_PRICE = :NEW.ITEM_PRICE,
BATCH_DET =:NEW.BATCH_DET,
BONS_QNTY = :NEW.BONS_QNTY,
TOT_VAL =(:NEW.ITEM_QNTY * NVL(:NEW.ITEM_PRICE,0)) - NVL(:NEW.DISC_VALUE,0),
LOCATION_CODE =:NEW.LOCATION_CODE,
DISC_VALUE = :NEW.DISC_VALUE,
ITEM_DET_ID =:NEW.ITEM_DET_ID
WHERE INVOICE_DET =:NEW.INVOICE_DET;
ELSE
DELETE FROM BRCHAS_INVOICE_DET
WHERE INVOICE_DET =:OLD.INVOICE_DET;
END IF;
EXCEPTION
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END BRCHAS_INVOICE_DET_VIEW_TRG;
ALTER TRIGGER "RETAIL"."BRCHAS_INVOICE_DET_VIEW_TRG" ENABLE;
please i need help
Thanks
AhmedHi,
This might help
http://www.techonthenet.com/oracle/errors/ora01446.php
Regards,
Jari -
SMTP Host logs - How many users are authenticating - How many emails with distinct titles
hello,
can anyone help me out with gathering the following SMTP host logs to see:
How many users are authenticating.
How many emails with distinct titles
Can this be done in powershell or do i need to be looking somewhere else.
This is for O365Hi ,
Since the emails are hosted with office 365 i would suggest you to raise a ticket with Microsoft and request assistance from them which will make this task much easier
Remember to mark as helpful if you find my contribution useful or as an answer if it does answer your question.That will encourage me - and others - to take time out to help you Check out my latest blog posts on http://exchangequery.com Thanks Sathish
(MVP) -
How to select a max row for each group in SQL ( with distinct code )
Dear friends,
My table is as show in below… for each ‘grpid’ I want get row with Max tiv value and code should be distinct. Ie in result, code & grpid will come only once.
grpid
code
Tiv
2
GB
9
2
IN
7
1
GB
11
1
US
10
Result: ( we are selecting IN even though for grpid 2
‘GB’ has max value )
grpid
code
Tiv
2
IN
7
1
GB
11
-SajidThanks for reply..
please see my DML+ DL
CREATE TABLE [dbo].[tab](
[grpid] [int] NOT NULL,
[code] [varchar](2) NOT NULL,
[Tiv] [int] NOT NULL
) ON [PRIMARY]
delete from tab
insert into tab values (2 , 'GB' ,12)
insert into tab values (2 , 'IN' ,10)
insert into tab values (1 , 'GB' ,11)
insert into tab values (1 , 'US' ,10)
insert into tab values (3 , 'GB' ,10)
insert into tab values (3 , 'US' ,9)
insert into tab values (3 , 'UA' ,10)
insert into tab values (3 , 'IN' ,8)
Result:
grpid code
tiv
1
US 10
2
GB 12
3
UA 10
Thanks in Advance..
-Sajid
CREATE TABLE [dbo].[tab](
[grpid] [int] NOT NULL,
[code] [varchar](2) NOT NULL,
[Tiv] [int] NOT NULL
) ON [PRIMARY]
delete from tab
insert into tab values (2 , 'GB' ,12)
insert into tab values (2 , 'IN' ,10)
insert into tab values (1 , 'GB' ,11)
insert into tab values (1 , 'US' ,10)
insert into tab values (3 , 'GB' ,10)
insert into tab values (3 , 'US' ,9)
insert into tab values (3 , 'UA' ,10)
insert into tab values (3 , 'IN' ,8)
;with mycte as
(select grpid,code,tiv , row_number() over(Partition by code Order by Tiv DESC) rn1, row_number() over(Partition by grpid Order by Tiv DESC) rn2
from tab)
,mycte1 as (Select *
, row_number() over(Partition by grpid, rn1 Order by rn2 ASC) rn3
from mycte
WHERE rn1=1
Select grpid,code,tiv from mycte1
Where rn3=1
drop table tab -
Hi;
I have a three table TB1, TB2 and TB3 which has 20 columns each.
Table TB1 has columns like x1,x2,x3...x20, table TB2 has columns
like x1,x21,x22...x39, TABLE TB3 has columns like x21,x40...x58.
I want to create a table of all disctinct column like
create TABLE TB4 as select TB1.*, TB2.*, TB3 from TB1 left outer join TB2
on TB1.x1=TB2.x1 left outer join TB3 on TB2.X21=TB3.x21However it gives me error duplicate column name, is there anyway I can generate table without mentionin specifically all column names.
I want to use basically wild character like TB1.{noformat}*{noformat}, TB2.{noformat}*{noformat} but if any duplicate column just take from table TB1.
Vkm
Edited by: BluShadow on 16-May-2011 14:17
added {noformat}{noformat} tags for at least some readabilityHi,
You can use a =TopN QUery+ to make sure the result set contains only one row for every distinct value on some column(s), for example:
WITH got_r_num AS
SELECT tb1.*
, tb2.*
, ROW_NUMBER () OVER ( PARTITION BY tb1.x1
ORDER BY NULL -- Or whatever you want
) AS r_num
FROM tb1
LEFT OUTER JOIN tb2 ON tb1.x1 = tb2.x1
SELECT *
FROM got_r_num
WHERE r_num = 1
;However, this will include r_num itself in the result set. You can list all the columns except r_num in the main SELECT clause, but you said you don't want to do that. (Why?) If you're just displaying this output, you your front end may have a way to hide the r_num column. SQL*Plus, for example, has "COLUMN r_num NOPRINT".
If you're using this in a CREATE TABLE AS statement, you'll need to give unique names to all the columns, anyway.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
You'll get better answers faster if you always supply this information whenever you post a question. -
How can I use WM_CONCAT with DISTINCT in a stored provedure?
Hi,
I join three tables. For example one is for employees, other is for employee address, and the last one is for the cities of employees that work. I want to take the employee id, address city, and working cities. It is like the table below, but I want one line for each employee, so I used WM_CONCAT. Because there is duplicate data, I used DISTINCT in WM_CONCAT. It works fine normally, but if I want to use it in a stored procedure it gives an error :ORA-30482: DISTINCT option not allowed for this function. Is there anyone who could solve this problem? Thank in advance.
EXP_ID CITY_ADDRESS CITY_WORK
1 IZMIR IZMIR
2 IZMIR DENIZLI
2 IZMIR IZMIR
2 IZMIR IZMIR
Edited by: 878596 on 10.Ağu.2011 22:48WM_CONCAT is an undocumented function. It is used only for Oracle Internal Purpose. You are not supposed to use it. Never use any Undocumented feature of Oracle. That could get you into trouble.
Said that if you can tell us exactly what problem are you trying to solve with WM_CONCATE we could give you a better solution without using WM_CONCATE.
Please read the Link How to ask a question in this forum? To post the question in a proper way.
SQL and PL/SQL FAQ -
SQL Query with Distinct and Count is wrong.
Hello,
i have another problem with a query.
Here the Data:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
CREATE TABLE TABLE_1
( "ORDER_NR" VARCHAR2 (12)
, "PRIORITY" VARCHAR2 (2)
, "WO_STATUS" VARCHAR2 (1)
, "STATUS_DATE" DATE
, "ART_NR" VARCHAR2 (9)
, "DESCRIPTION" VARCHAR2 (255)
, "PRICE" VARCHAR2 (10)
CREATE TABLE TABLE_2
( "ART_NR" VARCHAR(9)
, "MODELL" VARCHAR2(10)
, "MANUFACT" VARCHAR2(20)
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300612', '12', 'U', TO_DATE('05-FEB-13 10:22:39','DD-MON-RR HH24:MI:SS'), '005231987', '1ST ANNUAL SERVICE', '5000.2546');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300638', '05', 'U', TO_DATE('05-FEB-13 11:38:39','DD-MON-RR HH24:MI:SS'), '005667821', '3RD ANNUAL SERVICE', '5269.7856');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300638', '12', 'U', TO_DATE('06-FEB-13 12:38:39','DD-MON-RR HH24:MI:SS'), '005667821', '1ST BIENNIAL SERVICE', '1234.4468');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300638', '12', 'U', TO_DATE('07-FEB-13 13:38:39','DD-MON-RR HH24:MI:SS'), '005667821', '3RD ANNUAL SERVICE', '4366.7856');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300762', '12', 'U', TO_DATE('22-FEB-13 14:55:48','DD-MON-RR HH24:MI:SS'), '018743356', '3RD ANNUAL SERVICE', '4462.8632');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300766', '12', 'U', TO_DATE('22-FEB-13 08:32:13','DD-MON-RR HH24:MI:SS'), '018743356', '2ND ANNUAL SERVICE', '8762.6643');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300766', '05', 'U', TO_DATE('23-FEB-13 12:32:13','DD-MON-RR HH24:MI:SS'), '018743356', '1ST BIENNIAL SERVICE', '3425.6643');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300766', '12', 'U', TO_DATE('24-FEB-13 14:32:13','DD-MON-RR HH24:MI:SS'), '018743356', '2ND BIENNIAL SERVICE', '6678.6643');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300612', '12', 'U', TO_DATE('06-FEB-13 10:22:39','DD-MON-RR HH24:MI:SS'), '005231987', '1ST ANNUAL SERVICE', '5000.2546');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300638', '05', 'U', TO_DATE('05-FEB-13 11:38:39','DD-MON-RR HH24:MI:SS'), '005667821', '3RD ANNUAL SERVICE', '5269.7856');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300638', '12', 'U', TO_DATE('06-FEB-13 12:38:39','DD-MON-RR HH24:MI:SS'), '005667821', '1ST BIENNIAL SERVICE', '1234.4468');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300638', '12', 'U', TO_DATE('07-FEB-13 13:38:39','DD-MON-RR HH24:MI:SS'), '005667821', '3RD ANNUAL SERVICE', '4366.7856');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300762', '12', 'U', TO_DATE('22-FEB-13 14:55:48','DD-MON-RR HH24:MI:SS'), '018743356', '3RD ANNUAL SERVICE', '4462.8632');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300766', '12', 'U', TO_DATE('22-FEB-13 08:32:13','DD-MON-RR HH24:MI:SS'), '018743356', '2ND ANNUAL SERVICE', '8762.6643');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300766', '05', 'U', TO_DATE('23-FEB-13 12:32:13','DD-MON-RR HH24:MI:SS'), '018743356', '1ST BIENNIAL SERVICE', '3425.6643');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, WO_STATUS, STATUS_DATE, ART_NR, DESCRIPTION, PRICE)
VALUES ('1KKA1Z300766', '12', 'U', TO_DATE('24-FEB-13 14:32:13','DD-MON-RR HH24:MI:SS'), '018743356', '2ND BIENNIAL SERVICE', '6678.6643');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT1');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT2');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT3');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT4');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT5');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT6');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005667821', 'LASER', 'MANUFACT1');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005667821', 'LASER', 'MANUFACT2');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005667821', 'LASER', 'MANUFACT3');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005667821', 'LASER', 'MANUFACT4');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT1');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT2');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT3');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT4');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT5');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT6');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT1');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT2');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT3');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT4');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT5');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005231987', 'X-RAY1', 'MANUFACT6');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005667821', 'LASER', 'MANUFACT1');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005667821', 'LASER', 'MANUFACT2');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005667821', 'LASER', 'MANUFACT3');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('005667821', 'LASER', 'MANUFACT4');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT1');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT2');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT3');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT4');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT5');
INSERT INTO TABLE_2 (ART_NR, MODELL, MANUFACT)
VALUES ('018743356', 'VACCUM', 'MANUFACT6');
COMMIT;And my query:
SELECT T1.ART_NR
, T2.MODELL
, SUM(ROUND(T1.PRICE, 2)) AS TOTAL_PRICE
, COUNT(*) AS QTY
, TO_CHAR(T1.STATUS_DATE, 'MON-RR') AS MONTH
FROM TABLE_1 T1, TABLE_2 T2
WHERE T1.WO_STATUS = 'U'
AND T1.ART_NR = T2.ART_NR
AND TO_CHAR(T1.STATUS_DATE, 'MON-RR') = 'FEB-13'
GROUP BY T2.MODELL
, T1.ART_NR
, TO_CHAR(T1.STATUS_DATE, 'MON-RR')And the result:
ART_NR MODELL TOTAL_PRICE QTY MONTH
018743356 VACCUM 559916.16 96 FEB-13
005667821 LASER 173936.48 48 FEB-13
005231987 X-RAY1 120006 24 FEB-13My problem now is, the OTY field ist wrong it should count how often the equipment was in service in FEB-13 and group it by "MODELL" the MANUFACT field is not interesting for me, but this ist my problem, one Modell can have multible Manufacter and so i got a wrong count for my QTY.
The next step i need is to group the result also by Service type (annual or biennial), like this:
ART_NR MODELL TOTAL_PRICE QTY MONTH SERVICE_TYPE
018743356 VACCUM 1234.56 4 FEB-13 ANNUAL
018743356 VACCUM 4423.48 10 FEB-13 BIENNIAL
005667821 LASER 4783.11 2 FEB-13 ANNUAL
005667821 LASER 1123.77 22 FEB-13 BIENNIAL
005231987 X-RAY1 8966.12 6 FEB-13 ANNUAL
005231987 X-RAY1 7826.44 12 FEB-13 BIENNIALThis values are only out of my head, not the table, only to show what i need.
Thanks for your help.
Greets ReinhardHi,
Here's one way:
WITH got_groups AS
SELECT art_nr
, TRUNC (status_date, 'MONTH') AS month
, CASE
WHEN UPPER (description) LIKE '%ANNUAL%'
THEN 'ANNUAL'
WHEN UPPER (description) LIKE '%BIENNIAL%'
THEN 'BIENNIAL'
END AS service_type
, TO_NUMBER (price) AS price
FROM table_1
WHERE status_date >= DATE '2013-02-01'
AND status_date < DATE '2013-03-01'
, table_2_summary AS
SELECT DISTINCT art_nr, modell
FROM table_2
SELECT g.art_nr
, s.modell
, ROUND ( SUM (g.price)
, 2
) AS total_price
, COUNT (*) AS qty
, g.month
, service_type
FROM got_groups g
JOIN table_2_summary s ON s.art_nr = g.art_nr
GROUP BY g.art_nr
, s.modell
, g.month
, g.service_type
;The reason why your aggregates were originally too high is that you have a many-to-many relationship between the tables. The tables are related only by art_nr, but art_nr is not unique in either table. Look at art_nr '005231987', example. There ate 2 rows in table_1 with that art_nr, and 6 rows in table_2 with the same art_nr. If we join on art_nr, then both of the rows in table_1 will match each of the 6 rows in table_2, so the COUNT will be 2 * 6 = 12, and in the SUM, each of the numbers from table_1 will get added 6 times.
Why is table_2 designed the way it is? Cn there be multiple modells for the same art_nr? If so, what would you want for output? If there can only be 1 modell for each art_nr, then a better design would be to have a table that just had one row per art_nr, and included the modell column, and another table to show which manufacturers produce each art_nr. In this problem, you wouldn't need the manufacturers table, and the other table already has unique art_nrs, so you wouldn't need anything like the sub-query table_2_summary.
Don't store price in a VARCHAR2 column. Storing NUMBERs in a VARCHAR2 column is just asking for problems. Why not use a NUMBER column instead.
You'll notice that I used ROUND (SUM ... where you use SUM ( ROUND. The results might be a little different because of rounding errors. ROUND ( SUM only has to call ROUND once per group (5 times in this example) instead of once per row (16 times in this example). The less rounding you do, the less rounding error creeps in. Also, since there are fewer function calls, it's more convenient. (Of course, you'll never notice the difference between calling ROUND 5 times or 16 times, but in a real-life exampe, the difference could be between calling it 50 times or calling it 16000 times.) If you really need to use SUM (ROUND, you can. -
Hi, I have this query in my 9ir2 database:
SELECT xmit.u_ii_id, xprol.ii_id
FROM xprol, ( SELECT DISTINCT u_ii_id, ii_id
FROM t
WHERE NVL(key_t,'X') <> 'P') xmit
WHERE xprol.ii_id = xmit.ii_id
AND xmit.u_ii_id <> xprol.ii_id ;
u_ii_id ii_id
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
6220 5765
7898 3409
7898 3409
7898 3409
7898 3409
7898 3409
7898 3409
7898 3409
7898 3409
.The query returns 12.294.938 million of rows.
I need to know de best way to filter this values... maybe using Analytic Functions?
The result of the query must be...
u_ii_id ii_id
6220 5765
7898 3409Thanks!ROW_NUMBER () OVER (PARTITION BY xmit.u_ii_id
ORDER BY xmit.u_ii_id,
xprol.ii_id) AS rn
xprol, (SELECT DISTINCT u_ii_id, ii_id
FROM t
HERE NVL (key_t, 'X') <> 'P') xmit
WHERE xprol.ii_id = xmit.ii_id AND xmit.u_ii_id <>
xprol.ii_id AND rn = 1
/code]this will not work it will give , that needs to be in subquery
ORA-00904: "RN": invalid identifier
below is just an example
SQL> select empno,e.deptno ,row_number() over(partition by deptno order by deptno) rn
2 from emp e, (select deptno from dept1
3 where deptno=10) d
4 where e.deptno=d.deptno
5 and rn=1;
and rn=1
ERROR at line 5:
ORA-00904: "RN": invalid identifier
/pre] -
Value Set whose Data come from customize table with distinct record
Dear All,
I am new in Oracle EBS, currently i am creating value set whose data come from customize table which have 40 duplicate record in which distinct column return 27 record .
Table XX_ROUND_SET
Columns (Transactions_id,set_record)
Total Record (40)
Distinct Record (Set_Record --> 27)
I just want to show only 27 record in it.
Thanks
RehanHi Rehan,
PL.IGNORE MY EARLIER UPDATE AND TREAT THIS UPDATE AS YOUR SOLUTION.
Method 1
Create the VIEW based on DISTINCT values; use the VIEW for creates the VALUESET.
Method 2
Paste the QUERY in TABLE field with alias name, and give the column name (with alias name).
(in your case )
TABLE NAME : ( select distinct transactions_id, set_record from XX_ROUND_SET ) Y
VALUE : Y.transactions_id
HTH
Sanjay
Maybe you are looking for
-
Hi Experts, Could you please advice How can we Split the VAT amount in Purchase Order Our requirement is total PO amount with 60,000.. Please advice how can we resolve the issue. Thanks in advance... Diggi
-
Transferring photos from iPhoto to iPod
I have photos in iPhoto on my MacBook that i would like to transfer to my iPod Touch (3rd generation). Is there any way of doing this and if so, how?
-
How do I convert my exsisting nitro files to adobe ?
how do I convert my exsisting nitro files to adobe ?
-
I just bought a new iphone 5c typed in a passcode went back to type the passcode and it's 'invalid now the phone is 'disabled any ideas how to reset the phone thank you
-
Again: SqlException - Creating a ViewObject
Hi Gary! I tried the same with an simple viewobject definition. I chose only one attribute and in my select statement I selected only this one attribute. The test query option on the wizzard tells that the query is valid. But when I then go to the ne