If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL Query Help (Please)

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-28-09, 14:19
JtSearch JtSearch is offline
Registered User
 
Join Date: Oct 2009
Posts: 4
SQL Query Help (Please)

I'm trying to write a sql statement to relieve countless hours of manual effort. Please help if you can.

Keeping the data simple, think of one user listed 3 times. Action dates are, 8/30/2009, 9/5/2009, and 9/5/2009, using identical account codes. Sequence value for 8/30 is 0. Sequence values for 9/5 are 0 and 1. How can I only show the value for 8/30 with a sequence of 0 (highest for group in that date and account code range) and the one value from 9/5 with a sequence of 1 (max for the date and for the specific account code on that date)? Normally there are many names, account codes, and dates.

I have this:
SELECT NAME, ANNUAL_RT, Acct_CD, DIST_PCT, FUNDING_BEG_DT, JOB_EFFSEQ, ACTION_DT
FROM Table
GROUP BY NAME, ANNUAL_RT, Acct_CD, DIST_PCT, FUNDING_BEG_DT, JOB_EFFSEQ, ACTION_DT
ORDER BY Acct_CD, Max(ACTION_DT);
Reply With Quote
  #2 (permalink)  
Old 10-28-09, 15:29
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Code:
SELECT t.NAME
     , t.ANNUAL_RT
     , t.Acct_CD
     , t.DIST_PCT
     , t.FUNDING_BEG_DT
     , t.JOB_EFFSEQ
     , t.ACTION_DT
  FROM ( SELECT Acct_CD
              , MAX(ACTION_DT) AS max_date
           FROM daTable
         GROUP
             BY Acct_CD ) AS m
INNER
  JOIN daTable AS t
    ON t.Acct_CD    = m.Acct_CD
   AND t.ACTION_DT  = m.max_date
ORDER 
    BY Acct_CD
     , t.ACTION_DT
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-28-09, 16:20
JtSearch JtSearch is offline
Registered User
 
Join Date: Oct 2009
Posts: 4
The query doesn't return the proper results, close but not all. When there are multiple account codes on both 8/30 and 9/30, its only returning 1 record for 8/30 and the correct values for 9/30.

Maybe more information would help.... Use this as an example of the data.
ID Name Annual_RT ACCT_CD DIST_PCT FUND_BEG_DT SEQ ACT_DT
1 JT 25261 G85252 30 9/1/2009 0 8/28/09
2 JT 25261 G85252 30 9/1/2009 0 9/30/09
3 JT 25261 G85252 30 9/1/2009 1 9/30/09
4 JT 25261 G18752 10 9/1/2009 0 8/28/09
5 JT 25261 G18752 10 9/1/2009 0 9/30/09
6 JT 25261 G18752 10 9/1/2009 1 9/30/09
7 JT 25261 K17813 10 9/1/2009 0 8/28/09
8 JT 25261 K17813 10 9/1/2009 0 9/30/09
9 JT 25261 K17813 10 9/1/2009 1 9/30/09
10 JT 25261 T93854 50 9/1/2009 0 8/28/09
11 JT 25261 T93854 50 9/1/2009 0 9/30/09
12 JT 25261 T93854 50 9/1/2009 1 9/30/09
Reply With Quote
  #4 (permalink)  
Old 11-20-09, 17:07
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,297
Quote:
Originally Posted by JtSearch View Post
The query doesn't return the proper results, close but not all. When there are multiple account codes on both 8/30 and 9/30, its only returning 1 record for 8/30 and the correct values for 9/30.

Maybe more information would help.... Use this as an example of the data
What is your expected result for that sample data?
Reply With Quote
Reply

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On