home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / database / oracle / 2668 < prev    next >
Encoding:
Text File  |  1993-01-01  |  1.6 KB  |  51 lines

  1. Path: sparky!uunet!sqlware!kevin.bongiovanni
  2. From: kevin.bongiovanni@sqlware.uunet.UU.NET (Kevin Bongiovanni) 
  3. Newsgroups: comp.databases.oracle
  4. Subject: RE: ORACLE SQLPLUS MATRIX
  5. Message-ID: <563.204.uupcb@sqlware.uunet.UU.NET>
  6. Date: 29 Dec 92 00:56:00 GMT
  7. Distribution: world
  8. Organization: SQLWare Inc., Leesburg, VA - (703) 771-3979
  9. Reply-To: kevin.bongiovanni@sqlware.uunet.UU.NET (Kevin Bongiovanni) 
  10. Lines: 39
  11.  
  12. In response to your SQLPLUS matrix question...
  13.    I found the following program floating around a book in my office.
  14. The idea is that you have a table R(A,B,C) such that element A can be
  15. the row, element B can be the column, and element C the value.
  16.           B1.....Bn
  17.        A1 C1,1...C1,n
  18.        ...
  19.        An Cn,1...Cn,n
  20. The SQL Script is as follows:
  21.    column x noprint
  22. set pause off
  23. set heading off
  24. set feedback off
  25. .
  26. spool temp.sql
  27. select 0 x,'select <element A>, ' from dual
  28.    union
  29. select 1 x,'sum(decode(<element B>,''' || <element B> || ''',<element
  30. C>, 0))' ||' '||<element B>||','
  31. from <table name>
  32. where <element B> is not null
  33.    union
  34. select 2 x,'sum(<element c>) Total from <table name> group
  35.    <element A>;'
  36. from dual
  37. order by 1
  38. /
  39. spool off
  40. set header on
  41. spool result
  42. start temp
  43. spool off
  44. /
  45.                                                                                      
  46. ----
  47. +--------------------------------------------------------------------------+
  48. | SQLWare BBS  703-771-4158 (HST V.32BIS-Specializing in ORACLE and other  |
  49. | RDBMS topics.  Sponsored by SQLWare Inc., Leesburg,VA-300 conferences.   |
  50. +--------------------------------------------------------------------------+
  51.