Trouble with complex SQL query - multiple tables - pppor design
I am having trouble creating the appropriate query to pull information from three different tables. Admittedly the database design IMHO is horrible but it cannot be changed as it is part of a paid product that won't budge or even offer help with what I am doing. I am trying to run some reporting off this database.
Tables are as follows:
trans, trans_date, trans_details
456 .......$1234 .......XYZ ......$123 ..........1
459 .......$2345 .......ZYX ......$222 ..........2
460 .......$1122........ZYC ......$111 .........3
Plan holds information about the client that purchased the plan.
Trans_details holds dates and amount of a transaction but has no reference to the client (Client information is in Plan table).
I need to pull all transactions between a certain date for a certain sales rep but I also need to display the client information (from Plan table)
This thing has been killing me because the tables are several hundred thousand rows and I just can't seem to get a query that returns the correct data without returning millions of rows.
I'm sure it is a matter of a couple or three more joins.
What do you have that is returning too much data?
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
Just the basic jon joined query which I know is wrong and why it's returning way too many rows but I've tried doing a join but I'm just having trouble with the proper order and syntax of the statement to get it working correctly.
SELECT plans.name, plan.rep, trans_detail.amount, trans_detail.code, trans_detail.repamount WHERE trans_date (date range goes here) AND trans_details.code = (code criteria here)
That obviously won't work because it returns millions of rows.
I've not been able to get a functioning JOIN statement yet as I am accustomed to the more simple SQL queries.
Basically, the table TRANS is just a table of the PLANS ID's and TRANS ID's. This data is originally from an IBM UniVerse database that has internal references for the TRANS table and you don't even really see the TRANS_DATE and TRANS_DETAILS table until migrated to a MS SQL database.
Objectively, to relate PLANS table to TRANS_DETAIL table need, at least, a field to relate:
PLANS table <-> TRANS table.
The PLANS.ID and TRANS.Planid should are the right fields, to does it,
but I see that values are different in this two fields.
If PLANS.ID and TRANS.Planid contains the same value, then the query below should be right:
That query produce this result (based to values you have indicates):
SELECT PLANS.NAME, PLANS.REP, TRANS_DETAILS.AMOUNT, TRANS_DETAILS.CODE, TRANS_DETAILS.REPAMOUNT, TRANS_DATE.TRANSDATE
FROM TRANS_DATE INNER JOIN (TRANS_DETAILS INNER JOIN (PLANS INNER JOIN TRANS ON PLANS.ID = TRANS.PLANID) ON TRANS_DETAILS.TRANSID = TRANS.TRANSID) ON TRANS_DATE.TRANSID = TRANS.TRANSID
WHERE (TRANS_DETAILS.CODE='ZYX' AND TRANS_DATE.TRANSDATE=#2/1/2009#);
NAME REP AMOUNT CODE REPAMOUNT TRANSDATE
NAME2 JIM $ 2.345,00 ZYX $ 222,00 02/01/2009
You doesn't indicate the database you use, so I used a Access db, which use the (#) sign for DateTime field. If you use a different database use the appropriate sign, usually is the (').
Last edited by gibra; 05-14-2009 at 06:09 AM.
By awyeah in forum Database
Last Post: 07-13-2008, 12:35 PM
By cyman73 in forum VB Classic
Last Post: 11-18-2005, 01:10 AM
By dhaya in forum Database
Last Post: 08-25-2003, 06:24 PM
By Santosh in forum Database
Last Post: 02-28-2001, 03:36 AM
Last Post: 01-18-2001, 10:00 PM
Tags for this Thread
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center