Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

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
  #1  
Old April 29th, 2003, 08:23 AM
npp1 npp1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 4 npp1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry SQL "group by" statement

I am hoping someone out there can help.....

I am currently successfully running the following query with the following results:

> select max(trd_buy_qty), prod_type_cd from trd_vol_dly group by prod_type_cd;

MAX(TRD_BUY_QTY) PROD_TYPE_CD
------------------------- -------------------
5669 01
128989 02
3263 03
5654 04
1241 05
363 06
4461 07
917 09
89 10
330 14
626 15
883 16
1857 17

13 rows selected.

I now, however, want to retrieve that exact set of data only with the date that the max(trd_buy_qty) occurred (which is stored in the table as AS_OF_DA) as well.

If I just try to add AS_OF_DA as shown here I get the following error:

> select max(trd_buy_qty), prod_type_cd, as_of_da from trd_vol_dly group by prod_type_cd;

ERROR at line 1:
ORA-00979: as_of_da not a GROUP BY expression

And if I add AS_OF_DA to the group by clause as shown below I get the max value for EACH day and basically get every row in the table (and I only want the max value for ALL days):

> select max(trd_buy_qty), prod_type_cd, as_of_da from trd_vol_dly group by prod_type_cd, as_of_da;

If anyone out there knows a solution to the problem it would be greatly appreciated.

Thanks in advance.....

Reply With Quote
  #2  
Old April 29th, 2003, 10:06 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,745 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 20 h 11 m 5 sec
Reputation Power: 31
Code:
select trd_buy_qty, 
prod_type_cd ,
 AS_OF_DA
from trd_vol_dly  tvd
where trd_buy_qty in(
select max(q.trd_buy_qty) 
from trd_vol_dly  q
where q.prod_type_cd = tvd. prod_type_cd)



Don't post Oracle queries in the Mysql forumr.

Last edited by swampBoogie : April 29th, 2003 at 10:08 AM.

Reply With Quote
  #3  
Old April 29th, 2003, 10:46 AM
npp1 npp1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 4 npp1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
swampBoogie,

Impressive. Okay, that gives the high across all prod_type_cd's for each day, but I'm looking for the high for each prod_type_cd for the entire tenure and the day that each high occurred. There should be 13 rows. Any ideas there?

My fault for not cleary relaying the issue.

Thanks in advance.

Reply With Quote
  #4  
Old April 29th, 2003, 10:48 AM
npp1 npp1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 4 npp1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Also, this is optional, but is it possible to do without the IN statement and the embedded SQL? After creating this query I am going to attempt to port this into WebFocus and it's SQL bytecode will not allow for the IN statement.

Thanks again.

Reply With Quote
  #5  
Old April 30th, 2003, 07:48 AM
npp1 npp1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 4 npp1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Anyone....anyone....HEEEEELP.

Reply With Quote
  #6  
Old April 30th, 2003, 11:30 AM
hedge hedge is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2002
Posts: 692 hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 20 h 2 sec
Reputation Power: 19
I played with this a bit and came up with essentially the same thing as swampboogie.

That query would assume that the combination of prod_type_cd and as_of_da is unique, Is that the case? We either aren't understanding you or there is something about the data that we don't know about.

I came up with 3 different methods that you may find useful to work around your db issue. I have no idea what webfocus is so I'm no help there.


PHP Code:
 select prod_type_cdtrd_buy_qtyas_of_da
 from trd_vol_dly tvd
where trd_buy_qty 
= (select max(q.trd_buy_qty
                       
from trd_vol_dly  q
                      where q
.prod_type_cd tvdprod_type_cd);


select prod_type_cdtrd_buy_qtyas_of_da
  from trd_vol_dly tvd
 where 
(prod_type_cd,trd_buy_qtyin (select prod_type_cdmax(trd_buy_qty)
                                        
from trd_vol_dly
                                      group by prod_type_cd
);


select prod_type_cdtrd_buy_qtyas_of_da
  from trd_vol_dly tvd
,
       (
select prod_type_cdmax(trd_buy_qtytrd_buy_qty
          from trd_vol_dly
        group by prod_type_cd
tmp
 where tvd
.prod_type_cd tmp.prod_type_cd
   
and tvd.trd_buy_qty tmp.trd_buy_qty

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > SQL "group by" statement


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!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway