Home » SQL & PL/SQL » SQL & PL/SQL » SQL Query
SQL Query [message #2203] Thu, 27 June 2002 05:47 Go to next message
M.N.Swaminathan
Messages: 10
Registered: May 2002
Junior Member
Hi,

can anyone give me solution for the below

my table

type_Code emp_code Sanc_Amt Disb_Amt
-------------------------------------
rural x 10 0
urban y 12 6
rural z 15 15
urban a 12 8

i want the output like

type_code ac_sanamt tot_sanc ac_disbamt tot_disb

rural 2 25 1 15
urban 2 24 2 14

rds
Ragavan
Re: SQL Query [message #2204 is a reply to message #2203] Thu, 27 June 2002 06:29 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
Is this helping you?
SQL> select * from mytable;

TYPE_CODE  EM SANC_AMOUNT DIST_AMOUNT
---------- -- ----------- -----------
rural      x           10           0
urban      y           12           6
rural      z           15          15
urban      a           12           8
rural      n           22           3
urban      a            1           1

6 rows selected.

SQL> select type_code,count(type_code), f8(type_code),f9(type_code),f10(type_code) from mytable
  2  group by type_code;

TYPE_CODE  COUNT(TYPE_CODE) F8(TYPE_CODE) F9(TYPE_CODE) F10(TYPE_CODE)
---------- ---------------- ------------- ------------- --------------
rural                     3            47            18              2
urban                     3            25            15              3

these are the functions used

SQL> get f8
  1  create or replace function f8(n varchar2) return number is
  2  retval number;
  3  a number;
  4  begin
  5  select sum(sanc_amount) into a
  6             from mytable
  7             where type_code=n;
  8  retval:=a;
  9  return retval;
 10* end;
SQL> /

Function created.

SQL> get f9
  1  create or replace function f9(n varchar2) return number is
  2  retval number;
  3  a number;
  4  begin
  5  select sum(dist_amount) into a
  6             from mytable
  7             where type_code=n;
  8  retval:=a;
  9  return retval;
 10* end;
SQL> /

Function created.

SQL> get f10
  1  create or replace function f10(n varchar2) return number is
  2  retval number;
  3  a number;
  4  begin
  5  select     count(decode(dist_amount,0,null,
  6             dist_amount)) into a from mytable
  7             where type_code=n;
  8  retval:=a;
  9  return retval;
 10* end;
SQL> /

Function created.

Re: SQL Query [message #2210 is a reply to message #2203] Fri, 28 June 2002 01:59 Go to previous message
NV
Messages: 6
Registered: June 2002
Junior Member
hai,

Try with this query :

select type_code,count(type_code),sum(sanc_amt),
count(decode(disb_amt,0,null,disb_amt)) disb_count,
sum(disb_amt) from temp
group by type_code

Hope it will solve ur prob...

Bye
Previous Topic: Bizarre Counts
Next Topic: sequence
Goto Forum:
  


Current Time: Thu Apr 18 01:26:40 CDT 2024