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 > How to improve this SQL statement?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-30-03, 23:33
qxz qxz is offline
Registered User
 
Join Date: Apr 2002
Posts: 84
Red face How to improve this SQL statement?

I have a query in MS Access that will select the only record from a table DECISION, given three input parameters: pMonth, pLoansize and pLTV. The record should have the maximal possible value for MONTH, LOAN_SIZE and PERCENTAGE, in this order.

My query is working, but a bit slow since it seems joining four tables. I'd appreicate for any idea about how to improve this query, which is as
follow. Thanks.

-----------------------------------------------------------------------------
SELECT distinct C.DECISION_ID, C.DECISION_NAME_ID, C.MONTH, C.LOAN_SIZE, C.percentage, C.ADMIN_FEE_HL_ID, C.PNTY_ID, C.OFFER_RATE_ID
FROM DECISION AS A, DECISION AS B, DECISION AS C, DECISION AS D
WHERE
(D.MONTH = (SELECT max(MONTH) FROM DECISION D WHERE pLoansize>=D.LOAN_SIZEand pMONTH >= D.MONTH AND pLTV >= D.percentage))
AND
(B.LOAN_SIZE= (SELECT max(loan_size) FROM DECISION B WHERE B.MONTH = D.MONTH AND pLoansize>=B.LOAN_SIZEAND pLTV >= B.percentage))
AND
(A.percentage = (SELECT max(percentage) FROM DECISION A WHERE A.MONTH = D.MONTH and A.loan_size=B.LOAN_SIZE
AND pLTV >= A.percentage))
AND C.MONTH = D.MONTH
AND C.LOAN_SIZE= B.loan_size
AND C.percentage = A.percentage
Reply With Quote
  #2 (permalink)  
Old 07-01-03, 07:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
total shot in the dark, please let me know if this works:
Code:
SELECT DECISION_ID
     , DECISION_NAME_ID
     , MONTH
     , LOAN_SIZE
     , percentage
     , ADMIN_FEE_HL_ID
     , PNTY_ID
     , OFFER_RATE_ID
  FROM DECISION        AS A
 WHERE MONTH = (
            SELECT max(MONTH)
              FROM DECISION 
               )
   AND LOAN_SIZE = (
            SELECT max(loan_size) 
              FROM DECISION 
             WHERE MONTH = A.MONTH 
                   )
   AND percentage = (
            SELECT max(percentage)
              FROM DECISION  
             WHERE MONTH = A.MONTH 
               and loan_size = a.LOAN_SIZE 
                    )
rudy
http://r937.com/
Reply With Quote
  #3 (permalink)  
Old 07-01-03, 21:36
qxz qxz is offline
Registered User
 
Join Date: Apr 2002
Posts: 84
Hi r937,

Thanks for the try. But I am afraid that it is not working. Your SQL may end up with no record selected. For example, a simplified DECISION table:

Month, Loan_size, Percentage
8, 100, 70
4, 200, 50
6, 150, 80
Reply With Quote
  #4 (permalink)  
Old 07-01-03, 22:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
did you try it?

i tried it on your sample of 3 and it gave the row with

8, 100, 70


if you just wanted the max of all three colulmns, that's a totally different query (and a lot simpler, too)

i thought you wanted row integrity

for example, suppose there are 80 rows in the table, of which 20 belong to the highest month

then out of those 20 rows which have the highest month, 6 of those rows have the highest loan_size for that month

then out of those 6 rows which have the highest loan_size for the highest month, one of them has the highest percentage

my query will always return a row, as long as there is at least one row in the table


perhaps you did not explain your problem correctly?

i tried to see what your query was attempting to do, but it's seriously messed up

or maybe i totally misunderstood you


rudy
Reply With Quote
  #5 (permalink)  
Old 07-01-03, 23:06
qxz qxz is offline
Registered User
 
Join Date: Apr 2002
Posts: 84
Thumbs up

Hi Rudy,

Sorry it was my mistake. The query is working now since I missed
out something. Thank you very much.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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