Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 9th, 2009, 11:25 AM
lau_03 lau_03 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 2 lau_03 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 22 sec
Reputation Power: 0
Complex query

Hi guys,
Sorry about the title, that's as much as I can describe it right now.

I need to run a bunch of counts and sum on one table and return different fields for each aggregate. It is giving me a headache. I'm sure there is a cleaner way of doing this than what I am doing now.

This is a sample data:
QUOTE_NAME CREATE_DATE PRICE SUPPLIER_NAME
abc 29-APR-09 347 SUP1
abc 29-APR-09 155 SUP1
abc 29-APR-09 0 SUP1
abc 29-APR-09 0 SUP1
abc 29-APR-09 0 SUP1
abc 29-APR-09 0 SUP1
abc 29-APR-09 0 SUP1
abc 29-APR-09 0 SUP1
def 24-APR-09 1004 SUP2
def 24-APR-09 680 SUP2
def 24-APR-09 170 SUP2
def 24-APR-09 200 SUP2
def 24-APR-09 200 SUP1
def 24-APR-09 692 SUP1
def 24-APR-09 692 SUP1
def 24-APR-09 3041.99 SUP1
def 24-APR-09 857 SUP1
ghi 22-SEP-08 155 SUP1
ghi 22-SEP-08 155 SUP2
ghi 22-SEP-08 155 SUP2
ghi 22-SEP-08 155 SUP2
ghi 22-SEP-08 909.99 SUP3
ghi 22-SEP-08 297 SUP3
ghi 22-SEP-08 649 SUP3
ghi 22-SEP-08 240 SUP3
ghi 22-SEP-08 122.99 SUP3

I need to get this
QUOTE_NAME SUP1 SUP2 SUP3 Sum
def 5 4 0 7536.99
abc 8 0 0 502
ghi 1 3 5 2838.98

And so far, the SQL:
Code:
select distinct q1.quote_name as quote_name,
  (select count(*) from quote_table bc_i1 where bc_i1.quote_id= q1.quote_id and bc_i1.supplier_name='SUP1') as SUP1,
  (select count(*) from quote_table bc_i2 where bc_i2.quote_id= q1.quote_id and bc_i2.supplier_name='SUP2') as SUP2,
  (select count(*) from quote_table bc_i2 where bc_i2.quote_id= q1.quote_id and bc_i2.supplier_name='SUP3') as SUP3

FROM quote_table q1


This is a horrible query. It is not flexible. If I have a SUP4 tomorrow, I'll have to edit it.
Second, it is slow
Third, I am not including the sum of price

I might have to break that down. Maybe even incorporate PL/SQL? Any help would be appreciated.

PS: I'd like to format the tables for better readability... Can I do that?

Reply With Quote
  #2  
Old June 9th, 2009, 12:25 PM
shammat shammat is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,532 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 16 h 34 m 21 sec
Reputation Power: 209
Try this:
Code:
select quote_name,
         sum(case when supplier_name = 'SUP1' then 1 else 0 end) as SUP1, 
         sum(case when supplier_name = 'SUP2' then 1 else 0 end) as SUP2, 
         sum(case when supplier_name = 'SUP3' then 1 else 0 end) as SUP3
FROM quote_table 
GROUP BY quote_name;
Probably a lot faster, but you'll still need to edit it when a new supplier comes up.

Quote:
Third, I am not including the sum of price
Do you want the sum of price on quote or supplier level?
Comments on this post
clivew agrees: I like that approach - neat

Reply With Quote
  #3  
Old June 9th, 2009, 12:35 PM
clivew clivew is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 1,124 clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 6 Days 1 h 39 m 23 sec
Reputation Power: 272
Quote:
Originally Posted by lau_03
Maybe even incorporate PL/SQL?

This may be your most effective solution.
You can probably limit the number of times you retrieve the sorted result set to one
and use procedural code (PL/SQL) to accumulate your values as you walk
the result set.

I think there are also some newer cross-tab SQL extensions in Oracle that might be
helpful; but I have not personally been using them.

Clive

Reply With Quote
  #4  
Old June 9th, 2009, 01:29 PM
lau_03 lau_03 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 2 lau_03 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 22 sec
Reputation Power: 0
Thanks clivew and shammat! That answers a lot already

Quote:
Do you want the sum of price on quote or supplier level?


Yes, shammat, I need both the count of items and the sum of price in this case. The reason I didn't include it was that the query was already taking ages to run. Adding another sum() would have made it worse.

I understand the CASE lines, but I don't know how to make a sum(price) fit.

clive, I'm goggling "cross-tab SQL extensions" big time

EDIT
Silly me
Code:
sum(CASE WHEN supplier_name='SUP1' then ext_price end) as SumPriceSUP1

Reply With Quote
  #5  
Old June 9th, 2009, 01:42 PM
LKBrwn_DBA's Avatar
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 506 LKBrwn_DBA User rank is First Lieutenant (10000 - 20000 Reputation Level)LKBrwn_DBA User rank is First Lieutenant (10000 - 20000 Reputation Level)LKBrwn_DBA User rank is First Lieutenant (10000 - 20000 Reputation Level)LKBrwn_DBA User rank is First Lieutenant (10000 - 20000 Reputation Level)LKBrwn_DBA User rank is First Lieutenant (10000 - 20000 Reputation Level)LKBrwn_DBA User rank is First Lieutenant (10000 - 20000 Reputation Level)LKBrwn_DBA User rank is First Lieutenant (10000 - 20000 Reputation Level)LKBrwn_DBA User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 4 Days 12 h 34 m 59 sec
Reputation Power: 192
Cool Rollup

If you have 10g+ you can use the GROUP BY ROLLUP option to get the totals.

If your version is 11g, then check out the PIVOT function.
__________________

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Complex query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
Stay green...Green IT