Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Create groups of related column values

Dibyendu ChakrabortyMay 28 2010 — edited May 29 2010
Hi,
I have a table, say t1 (create table t1 (c1 number, c2 number)). The column C1 hold a set of persons which are related to C2. Now I want to create groups which are related.
the insert script is as below:
insert into t1 values(120, 110);
insert into t1 values(130, 110);
insert into t1 values(130, 170);
insert into t1 values(170, 180);
insert into t1 values(220, 210);
insert into t1 values(220, 200);
insert into t1 values(220, 190);
insert into t1 values(230, 220);
insert into t1 values(280, 270);
insert into t1 values(280, 260);
insert into t1 values(280, 250);

So, the records in the target table will be like:

groupid c3
-----------------------------------------
1 120
1 110
1 130
1 170
1 180
2 220
2 210
2 200
2 190
2 230
3 280
3 270
3 260
3 250

Please let me know how can I do that?
Hierarchical query is not helping me. The original table number of records are around 50K.

Please. This is urgent.

Comments

Karthick2003
What do you mean by compare? Can you explain a bit more?
Dbb
Select a.empcode, sum(a.totalvalue) , sum(b.totalvalue )
from table_sales a, table_sales b
where a.empcode = b.empcode
and a.trndate between ......
and b.trndate between ........
group by a.empcode

Edited by: Agustin UN on 08-nov-2010 13:22
726742
Do you mean Sum(totalvalue) between those periods?
select 
Select sum(totalvalue) from table where trndate between to_date('01-10-2010', 'DD-MM-YYYY') AND  to_date('08-01-2010', 'DD-MM-YYYY')

SELECT ((SELECT SUM(totalvalue)
         FROM   table1
         WHERE  trndate BETWEEN to_date('01-10-2010', 'DD-MM-YYYY') AND
                to_date('08-10-2010', 'DD-MM-YYYY')) -
       (SELECT SUM(totalvalue)
         FROM   table1       
         WHERE  trndate BETWEEN to_date('01-11-2010', 'DD-MM-YYYY') AND
                to_date('01-11-2010', 'DD-MM-YYYY')))
FROM   dual
Be more clear on your requirement :)
787094
I need some what like that, but there is a problem that if there is no sales in one range reports are not coming properly.

Please help

Thanks

Niloy
726742
SELECT ((SELECT SUM(nvl(totalvalue,0))
         FROM   table1
         WHERE  trndate BETWEEN to_date('01-10-2010', 'DD-MM-YYYY') AND
                to_date('08-10-2010', 'DD-MM-YYYY')) -
       (SELECT SUM(nvl(totalvalue,0))
         FROM   table1       
         WHERE  trndate BETWEEN to_date('01-11-2010', 'DD-MM-YYYY') AND
                to_date('01-11-2010', 'DD-MM-YYYY')))
FROM   dual
787094
I have table with the following data :

custcode emp Trndate Totalvalue
1 1 2010-10-01 500.00
2 1 2010-10-02 500.00
3 1 2010-10-03 500.00
4 1 2010-10-08 100.00
5 1 2010-11-02 175.00
6 5 2010-11-01 130.00
7 5 2010-11-02 135.00
8 5 2010-11-03 130.00
9 6 2010-10-01 100.00
10 6 2010-10-02 12.00


I'd like to build a query that returns something like this:

Empcode TotalSales_OCT TotalSales_NOV
====== ========= ===========
1 1600 175
5 0 395
6 112 0


Please help...

Thanks
Dbb
Select a.empcode,b.empcode, sum(a.totalvalue) , sum(b.totalvalue )
from table_sales a, table_sales b
where a.empcode(+) = b.empcode
and a.trndate(+) between ......
and b.trndate(+) between ........
group by a.empcode
Union
Select a.empcode,b.empcode, sum(a.totalvalue) , sum(b.totalvalue )
from table_sales a, table_sales b
where a.empcode = b.empcode(+)
and a.trndate(+) between ......
and b.trndate(+) between ........
group by a.empcode

Edited by: Agustin UN on 08-nov-2010 13:50
Dbb
with table_sales as (
select 1 custcode, 1 empcode, '2010-10-01' Trndate, 500.00 Totalvalue from dual union
select 2, 1, '2010-10-02', 500.00 from dual union
select 3, 1, '2010-10-03', 500.00 from dual union
select 4, 1, '2010-10-08', 100.00 from dual union
select 5, 1, '2010-11-02', 175.00 from dual union
select 6, 5, '2010-11-01', 130.00 from dual union
select 7 ,5, '2010-11-02', 135.00 from dual union
select 8, 5, '2010-11-03', 130.00 from dual union
select 9, 6, '2010-10-01', 100.00 from dual union
select 10, 6, '2010-10-02', 12.00 from dual
)
Select a.empcode, sum(a.totalvalue) , sum(b.totalvalue)
from table_sales a ,table_sales b
where
a.empcode = b.empcode(+)
and a.Trndate(+) between '2010-10-01' and '2010-10-31'
and b.Trndate(+) between '2010-11-01' and '2010-11-30'
group by a.empcode
union
Select b.empcode empcode, sum(a.totalvalue) , sum(b.totalvalue)
from table_sales a ,table_sales b
where
a.empcode(+) = b.empcode
and a.Trndate(+) between '2010-10-01' and '2010-10-31'
and b.Trndate(+) between '2010-11-01' and '2010-11-30'
group by b.empcode
Aketi Jyuuzou
In this case,Pivot clause is not useful :D
with table_sales(custcode,empcode,Trndate,Totalvalue) as(
select 1, 1,date '2010-10-01',500.00 from dual union
select 2, 1,date '2010-10-02',500.00 from dual union
select 3, 1,date '2010-10-03',500.00 from dual union
select 4, 1,date '2010-10-08',100.00 from dual union
select 5, 1,date '2010-11-02',175.00 from dual union
select 6, 5,date '2010-11-01',130.00 from dual union
select 7 ,5,date '2010-11-02',135.00 from dual union
select 8, 5,date '2010-11-03',130.00 from dual union
select 9, 6,date '2010-10-01',100.00 from dual union
select 10,6,date '2010-10-02', 12.00 from dual)
select empcode,
sum(case when Trndate
         between date '2010-10-01' and date '2010-10-08'
         then Totalvalue else 0 end) as OCT,
sum(case when Trndate
         between date '2010-11-01' and date '2010-11-08'
         then Totalvalue else 0 end) as NOV
  from table_sales
group by empcode
order by empcode;

EMPCODE   OCT  NOV
-------  ----  ---
      1  1600  175
      5     0  395
      6   112    0
ramanlakunde
SELECT ((SELECT SUM(nvl(totalvalue,0))
FROM table1
WHERE trndate BETWEEN to_date('01-10-2010', 'DD-MM-YYYY') AND
to_date('08-10-2010', 'DD-MM-YYYY') group by empcode) -
(SELECT SUM(nvl(totalvalue,0))
FROM table1
WHERE trndate BETWEEN to_date('01-11-2010', 'DD-MM-YYYY') AND
to_date('01-11-2010', 'DD-MM-YYYY') group by empcode))
FROM dual
787094
Hi,
If I put this query I got the following error :

SELECT ((SELECT SUM(nvl(dispvalue,0))
*
ERROR at line 1:
ORA-01427: single-row subquery returns more than one row


Thanks
Niloy
1 - 11
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 25 2010
Added on May 28 2010
10 comments
1,643 views