-
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
Plans Table
NAME......ID.....REP
Name1.....1......Tom
Name3.....3......Jim
Name2.....4......Bob
Trans Table
planid...transid
123.......456
124.......459
125.......460
Trans_Date Table
transid date........zassocrow
456......1-1-2009......1
459......2-1-2009.......2
460......12-1-2008.....3
trans_details table
transid....amount....code....repamount.....zassocrow
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?
-
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:
Code:
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#);
That query produce this result (based to values you have indicates):
Code:
NAME REP AMOUNT CODE REPAMOUNT TRANSDATE
NAME2 JIM $ 2.345,00 ZYX $ 222,00 02/01/2009
N.B.
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 05:09 AM.
Similar Threads
-
By awyeah in forum Database
Replies: 1
Last Post: 07-13-2008, 11:35 AM
-
By cyman73 in forum VB Classic
Replies: 2
Last Post: 11-18-2005, 01:10 AM
-
By dhaya in forum Database
Replies: 11
Last Post: 08-25-2003, 05:24 PM
-
By Santosh in forum Database
Replies: 3
Last Post: 02-28-2001, 03:36 AM
-
Replies: 4
Last Post: 01-18-2001, 10:00 PM
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|