Skip to main content
Inspiring
January 6, 2010
Question

Left Outer Join for CF write CSV question

  • January 6, 2010
  • 5 replies
  • 1347 views

I'm attempting my first Left Out Join in trying to output data fields in MS Access to a CSV file for downloading...

I may need to be in a different forum since this is a CF Query. Please someone let me know if I need to post this somewhere else.

I have several tables to call from. One of the tables (ewc) contains several different ID numbers and the other tables also contain one or more of these same ID numbers.

I hope I'm right in assuming that I need to do a Left Outer Join, basing the data pulled on the ID numbers in the one table to the corresponding ID fields in the other tables.

I'm getting an error somewhere in the query code (but am including the whole code on the page.

Thanks for anyone's help.

- e

PS: I plan to use cfqueryparam within the query instead of a 'straight query' for the final. Just am trying to go simple so as to test the whole code before going public...

------------------------------------------------------

<cfquery name = "getpredata" datasource = "M_data">
SELECT
ewc.EWCsID, ewc.StID, ewc.SC01, ewc.SC02, ewc.EWCscNumber, sc.EWCscNumber, sc.SC01, sc.SC02, sc.Scme, sc.Level, st.EWCstID, st.EWCscNumber, st.LName, st.FName, st.Et, st.Ge, st.Be, st.Lat, st.S8P, st.D9P, st.B7D, st.PreW, st.PreH, st.PreWHDate, st.HTName
FROM
((ewc_table ewc
LEFT OUTER JOIN
sc_table sc ON ewc.EWCscNumber = sc.EWCscNumber)
LEFT OUTER JOIN
pers_table st ON ewc.EWCsID = st.EWCsID)
WHERE
sc.Level = active
ORDER By
sc.EWCscNumber
</cfquery>

<cfif getpredata.recordcount GTE 66000>

    Too many records for download. Please contact Website Support.

<cfelse>

    <cffile action="write"
             file="D:\website.com\mt\download\mtpredata.csv"
             output="SSC,SC 01,SC 02,ST ID,Last Name,First Name,ET,GE,BE,LAT,S 8 P,D 9 P,B 7 D,Pre W,Pre H,Pre WH Date,HT Name"
             addnewline="yes">
   
    <cfoutput>


        <cfloop query="getpredata">   
       
        <cffile action="append"
                 file="D:\website.com\mt\download\mtpredata.csv"
                  output="#TRIM(Scme)# , #TRIM(SC01)# , #TRIM(SC02)# , #TRIM(StID)# , #TRIM(LName)# , #TRIM(FName)# , #TRIM(Et)# , #TRIM(Ge)# , #TRIM(Be)# , #TRIM(Lat)# , #TRIM(S8P)# , #TRIM(D9P)# , #TRIM(B7D)# , #TRIM(PreW)# , #TRIM(PreH)# , #TRIM(PreWHDate)# , #TRIM(HTName)#"
                 addnewline="yes">


        </cfloop>
    </cfoutput>
   
    <cflocation url="www.website.com/download/mtpredata.csv">
</cfif>

This topic has been closed for replies.

5 replies

Inspiring
January 8, 2010

Things to check

1. The parenthesis in your FROM clause.  Try this: 

FROM ewc_table ewc

    LEFT OUTER JOIN sc_table sc ON ( ewc.EWCscNumber = sc.EWCscNumber )

    LEFT OUTER JOIN pers_table st ON ( ewc.EWCsID = st.EWCsID ) 

2. The WHERE clause.  sc.Level = active looks odd.  What is "active"? 


Message was edited by: JR "Bob" Dobbs

NessmukAuthor
Inspiring
January 7, 2010

Thanks again for the input... (error message below)

Here's the scenerio I'm trying to do: This is for a school program teaching obesity lessons to students. At the beginning of a semester pre-heights and pre-weights and other stats are collected and at the end of the semester post-heights and post-weights and other stats are collected. A handful of schools will be participating. A CSV file will be uploaded that contains basic (State record) info on the student and, after collecting new data in different tables, a CSV will be available for downloading.

A school will upload a CSV file with State records such as student ID, School ID, Name, Gender, etc. and this upload builds a new table with this data. Partly for security in later queries, when this new table is built I'm assigning my own Student ID and School ID and other IDs to this table - autogenerated table Key + 1, table Key + 2, etc.

From this newly built table, the code will copy pertainent data fields to the other tables, with one 'main' table containing all the State IDs and all my own IDs. (So that later, when a school downloads a CSV with the data I can pull the State IDs, etc. that the school uses instead of my own personal IDs.) After the data in the CSV upload from the school is written to the other tables, the code will delete the newly uploaded table.

So... One 'main' table containing all IDs - State Student ID and 'my own' State Student ID, etc. This would be the table to base the query on, to pull data from the other tables. 'My own' Student and School IDs will be in each of the other tables... Some queries may address just one table, but for a  CSV download I may be addressing as many as 5 or more tables.

I hope I haven't made it too complicated. For logic, with all the different data to be collected, I need to make a handful of different tables. Each of the tables will be related to the one main table that contains all the IDs associated with one student and school.

Does this make sense in trying to do an outer join here, based on the one 'main' table that contains All the IDs? The one main table with IDs will contain some other pertainent data but it's mainly the other tables that I will need to pull all or most data from... based on the IDs in the one 'main' table.

And, as well, I do want to do the speediest thing with as little strain on the server. I certainly want to do the simplest most logical queries, which I know can begin with table layout in the database.

Just for other info here, login and other pages are dynamic PDFs from XML (Livecycle) through SSL, most pages running client side and admin edits confirmed with key certificate... queries parsed through Coldfusion.

Thanks...

Error message below that I get from the current code as it is in my original post.

- e

-----------------------

Error Executing Database Query. 
[Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1. 
 
The error occurred in D:\ftp\website\Htdocs\m_folder\download\m_data.cfm: line 1

DATASOURCE   MATCH_data
VENDORERRORCODE   -3010
SQLSTATE   07002

-----------------------

Inspiring
January 7, 2010

First, do you need outer joins?  Let's keep it simple.  You get two files, one with the initial heights and weights, one with the finals.  If less than 100% of the people in the initial file are in the final file, and you have to display results for all the people in the initial file, you need an outer join.  Otherwise you don't.

Now the sql error.  The error message is not as helpful as others I've seen.  I troubleshoot situations like this as follows:

1.  Comment everything out except for something very simple, such as

select field1

from table1

2.  Add condition

where 1 = 2

3.  run query.  Hopefully it runs successfully.

4.  Uncomment something and run query.  Repeat until query crashes.  The last thing you uncommented was the problem.

5.  Remove condition

where 1 = 2

NessmukAuthor
Inspiring
January 9, 2010

Thanks for the input, guys.

I changed the parenthases in the query and, am in the process of simplifying the query... to try and find the bug.

I'll re-post the first of the week to let folks now results.

Thanks again for the help!

- ed

NessmukAuthor
Inspiring
January 7, 2010


Thanks for the advice.

The query isn't running at all (Error Executing Database Query).

It's sounding like I need to do a Right Outer Join as it's written right now.

"A" table contains all IDs and "B", "C" and "D" tables contain at least one ID that is in the "A" table... and I need to do a join that pulls most but not all of the fields from "B","C" and "D" tables based on the query to "A".

To pull fields from B,C,D tables based on an ID in A - I would need to query:

FROM B RIGHT OUTER JOIN A ON (A.aField = B.aField)
FROM C RIGHT OUTER JOIN A ON (A.aField = C.aField)
etc.

Maybe the logic would make sense then.

I'll experiment with this and post again here.

Thanks, guys.

- e

Inspiring
January 7, 2010

What does the rest of the error message say?

Plus, if you think you have to do both left and right joins, it's possible that you are complicating something simple.

ilssac
Inspiring
January 6, 2010

You need to do an outer join (left or right) whenever you have one table that is a set of values a

and another table that is a smaller, related set of values and you want all the values from the first table whether or not there is a matching record in the second.  Wheter this is a left or right outer join depends on what side of the equations the table with the desired records is in your SQL clause.  If you put it on the right, you need a right outer join if you put it on the left use a left outer join.

SQL syntax.


FROM allTable LEFT OUTER JOUN someTable ON (allTable.aField = someTable.aField)

OR

FROM someTable RIGHT OUTER JOIN allTabled ON (allTable.aField = someTable.aField).

Inspiring
January 6, 2010

Is the query running, or is it crashing?  If it's crashing, lose the brackets.

You do have a logic error.  When you do this:

select whatever

from t1 left join t2 on t1.somefield = t2.somefield

where t2.anyfield = something

the left join becomes an inner join.  One workaround is to do the left join to a subquery.  The other is to put "t2.anyfield = something" in the join portion of the from clause instead of the where clause.