|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > SQL statement help with joins |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|