SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL 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 ASP Free Forums Sponsor:
  #1  
Old April 1st, 2004, 05:20 PM
rance rance is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 rance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation SQL statement help with joins

I have a massive database that I inherited so Im still having some trouble getting around it.

I have 4 sql statements that I need to merge into one.

I have a sql statement that is the basis for a report in MS Access (the tables are all linked to an odbc host)

Here is the idea, I have a sql statement that pulls and joins data from five different tables and it works as it should.

I think it is pretty complicated with all the inner joins and so forth, but anyway

here it goes:
SELECT DB2ADMIN_LOAD_HEADER.LOAD_ID, DB2ADMIN_LOAD_HEADER.LOAD_DATE, DB2ADMIN_LOAD_HEADER.EXTRA_DROPS, DB2ADMIN_LOAD_HEADER.EXTRA_PICKS, DB2ADMIN_LOAD_HEADER.CARRIER_ID, DB2ADMIN_REGION.REGION_RATE, DB2ADMIN_LOAD_ITEMS.ORDER_NO, DB2ADMIN_LOAD_ITEMS.STOP_NO, DB2ADMIN_LOAD_ITEMS.MILES, DB2ADMIN_ORDER_MASTER.NAMES, DB2ADMIN_ORDER_MASTER.ADDRESS_1, DB2ADMIN_ORDER_MASTER.ADDRESS_2, DB2ADMIN_ORDER_MASTER.CITY, DB2ADMIN_ORDER_MASTER.STATE, DB2ADMIN_ORDER_MASTER.ZIP, DB2ADMIN_ORDER_MASTER.CARRIER_MSG, DB2ADMIN_ORDER_MASTER.CUSTOMER_PO_NUMBER, Sum((CLng([DB2ADMIN_ORDER_DETAIL].[CASES_ORDERED]))) AS CASE_COUNT, Right([DB2ADMIN_ORDER_MASTER].[REQ_DEL_DATE],2) AS reqmonth, Mid([DB2ADMIN_ORDER_MASTER.REQ_DEL_DATE],5,2) AS reqday, Left([DB2ADMIN_ORDER_MASTER.REQ_DEL_DATE],4) AS reqyear
FROM ((DB2ADMIN_LOAD_ITEMS INNER JOIN (DB2ADMIN_REGION INNER JOIN DB2ADMIN_LOAD_HEADER ON DB2ADMIN_REGION.REGION_NAME = DB2ADMIN_LOAD_HEADER.REGION_NAME) ON DB2ADMIN_LOAD_ITEMS.LOAD_ID = DB2ADMIN_LOAD_HEADER.LOAD_ID) INNER JOIN DB2ADMIN_ORDER_MASTER ON DB2ADMIN_LOAD_ITEMS.ORDER_NO = DB2ADMIN_ORDER_MASTER.ORDER_NUMBER) INNER JOIN DB2ADMIN_ORDER_DETAIL ON DB2ADMIN_ORDER_MASTER.ORDER_NUMBER = DB2ADMIN_ORDER_DETAIL.ORDER_NUMBER
GROUP BY DB2ADMIN_LOAD_HEADER.LOAD_ID, DB2ADMIN_LOAD_HEADER.LOAD_DATE, DB2ADMIN_LOAD_HEADER.EXTRA_DROPS, DB2ADMIN_LOAD_HEADER.EXTRA_PICKS, DB2ADMIN_LOAD_HEADER.CARRIER_ID, DB2ADMIN_REGION.REGION_RATE, DB2ADMIN_LOAD_ITEMS.ORDER_NO, DB2ADMIN_LOAD_ITEMS.STOP_NO, DB2ADMIN_LOAD_ITEMS.MILES, DB2ADMIN_ORDER_MASTER.NAMES, DB2ADMIN_ORDER_MASTER.ADDRESS_1, DB2ADMIN_ORDER_MASTER.ADDRESS_2, DB2ADMIN_ORDER_MASTER.CITY, DB2ADMIN_ORDER_MASTER.STATE, DB2ADMIN_ORDER_MASTER.ZIP, DB2ADMIN_ORDER_MASTER.CARRIER_MSG, DB2ADMIN_ORDER_MASTER.CUSTOMER_PO_NUMBER, Right([DB2ADMIN_ORDER_MASTER].[REQ_DEL_DATE],2), Mid([DB2ADMIN_ORDER_MASTER.REQ_DEL_DATE],5,2), Left([DB2ADMIN_ORDER_MASTER.REQ_DEL_DATE],4), DB2ADMIN_LOAD_HEADER.NORBEST_LOAD_DATE, DB2ADMIN_LOAD_HEADER.NORBEST_LOAD_NO
HAVING (((DB2ADMIN_LOAD_HEADER.NORBEST_LOAD_DATE)=[Norbest Load Date]) AND ((DB2ADMIN_LOAD_HEADER.NORBEST_LOAD_NO)=[Norbest Load No]));


Its already so long I'm having trouble navigating through it, but as I said, it works

its for a shipping system, you create a truck load, and you put orders on the load, but most of the order detail is in the orders tables, so that where all the joins come from.

the load_header table has room for 3 extra charges that may or may not be on a particular load.
those columns are extra_charge1, extra_charge2, and extra_charge3

there is a charges table with three columns: charge_code, charge_desc, and charge_rate

Now, the following sql statement was a test...

SELECT DB2ADMIN_LOAD_HEADER.LOAD_ID, DB2ADMIN_CHARGES.CHARGE_DESC, DB2ADMIN_CHARGES.CHARGE_RATE
FROM DB2ADMIN_LOAD_HEADER LEFT JOIN DB2ADMIN_CHARGES ON DB2ADMIN_LOAD_HEADER.EXTRA_CHARGE1 = DB2ADMIN_CHARGES.CHARGE_CODE;

I will need two more of this same select statement only difference would be that the join would be on extra_charge2, and then again on extra_charge3

I should be able to pick up then all of my extra charges that are for a given load.

I need to merge these left join charge table selects into the big master one above.

I realize that im going to need some table aliasing to get all three charge types as charge_desc1, and charge_desc2 for example

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > SQL statement help with joins


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


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT