Home » SQL & PL/SQL » SQL & PL/SQL » Help with grouping documents (Oracle 10g)
Help with grouping documents [message #610198] Tue, 18 March 2014 06:47 Go to next message
krajesiu
Messages: 3
Registered: March 2014
Location: Poland
Junior Member
Hello, I need your help with fast querry which will set a group of invoices for given activity.
I will try to explain you what I mean on example:
I have table with activities and invoice number for activity:

Activity Invoice INVOICE_GROUP
1111 INV/1
1111 INV/2
1111 INV/3
2222 INV/1
2222 INV/2
3333 INV/1
4444 INV/56
5555 INV/56

Because invoice INV/1 is connected with activity 1111 and 2222 and 3333 it is one group of invoices and because to activity 1111 we have also invoice INV/2 and INV/3 it is also the same group of invoices.
So for the example above querry should give result:

Activity Invoice INVOICE_GROUP
1111 INV/1 1
1111 INV/2 1
1111 INV/3 1
2222 INV/1 1
2222 INV/2 1
3333 INV/1 1
4444 INV/56 2
5555 INV/56 2

I wrote a querry but it is very slow, any idea how to modify it?

SELECT * FROM KKR_ACTIVITI WHERE INVOICE IN(
SELECT INVOICE FROM KKR_ACTIVITI WHERE ACTIVITY IN (
SELECT ACTIVITY FROM KKR_ACTIVITI WHERE INVOICE IN
(SELECT INVOICE FROM KKR_ACTIVITI WHERE ACTIVITY = 1111)))

Replacing 'IN' statement with exists doesn't help.

But my querry is not good for such hardcore case where one acivity is conected another with one invoice (example below):

Activity Invoice INVOICE_GROUP
1111 INV/1 1
2222 INV/1 1
2222 INV/2 1
3333 INV/2 1
3333 INV/3 1
4444 INV/3 1
4444 INV/4 1
5555 INV/4 1
5555 INV/5 1
6666 INV/5 1


Thank you in advance for help
Re: Help with grouping documents [message #610199 is a reply to message #610198] Tue, 18 March 2014 07:08 Go to previous messageGo to next message
krajesiu
Messages: 3
Registered: March 2014
Location: Poland
Junior Member
Create table and inserts:

CREATE TABLE KKR_ACTIVITI
(
ACTIVITY NUMBER,
INVOICE VARCHAR2(13 BYTE)
);
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(1111, 'INV/1');
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(2222, 'INV/1');
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(2222, 'INV/2');
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(3333, 'INV/2');
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(3333, 'INV/3');
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(4444, 'INV/3');
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(4444, 'INV/4');
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(5555, 'INV/4');
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(5555, 'INV/5');
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(6666, 'INV/5');
Insert into KKR_ACTIVITI
(ACTIVITY, INVOICE)
Values
(7777, 'INV/99');
COMMIT;
Re: Help with grouping documents [message #610205 is a reply to message #610198] Tue, 18 March 2014 07:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

This is a hierarchical query, you can't do it with just IN, you have to investigate in CONNECT BY. The problem is now where to START WITH and to express the connection relation to avoid duplicated branches in your result (for instance, in your example, to realize that if you start with 1111 or 2222 or INV/1 or INV/2 ... you get the same group).

Re: Help with grouping documents [message #610207 is a reply to message #610205] Tue, 18 March 2014 08:06 Go to previous messageGo to next message
krajesiu
Messages: 3
Registered: March 2014
Location: Poland
Junior Member
I have never used CONNECT BY, is it possible that you will write a querry?
Re: Help with grouping documents [message #610208 is a reply to message #610207] Tue, 18 March 2014 08:16 Go to previous message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Sorry, I have not the time for the moment this is why I posted some hints but I remember this has been posted many years ago not here but on newsgroup (comp.databases.oracle.server) or on AskTom and I remember this was a difficult issue but I don't remember if in the end there has been a SQL solution or not.

Previous Topic: How to update the foreign key table
Next Topic: Adding spaces to the column value
Goto Forum:
  


Current Time: Tue May 07 17:06:10 CDT 2024