Trouble with complex SQL query - multiple tables - pppor design


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Trouble with complex SQL query - multiple tables - pppor design

Hybrid View

  1. #1
    Join Date
    May 2009
    Posts
    2

    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.

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    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

  3. #3
    Join Date
    May 2009
    Posts
    2
    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.

  4. #4
    Join Date
    Mar 2009
    Location
    Italy - Breganze (VI)
    Posts
    120
    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

  1. Simple sql query for recordset
    By awyeah in forum Database
    Replies: 1
    Last Post: 07-13-2008, 11:35 AM
  2. multiple tables, recordsets, and sql ?
    By cyman73 in forum VB Classic
    Replies: 2
    Last Post: 11-18-2005, 12:10 AM
  3. query tuning
    By dhaya in forum Database
    Replies: 11
    Last Post: 08-25-2003, 05:24 PM
  4. Updating 2(two ) tables at a time in SQL
    By Santosh in forum Database
    Replies: 3
    Last Post: 02-28-2001, 02:36 AM
  5. SQL - The Order of Tables and WHERE
    By YFM in forum Database
    Replies: 4
    Last Post: 01-18-2001, 09: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center