Pulling Fields From Different Queries
|
|
Thread rating:  |
webspinner - 25 Jan 2007 14:05 GMT I have two queries in a CF page. What I want to do is look at the dataset from one, and if a given variable has a value in that, use it, otherwise use the same variable from query two. And there are around 15 variables. Is there a succinct way to do this?
Thanks,
Rick
Dinghus - 25 Jan 2007 17:03 GMT Simple IF ... ELSE statement should work just fine.
<CFIF qry1.myfield IS "whatever"> <cfset myvar = qry1.myfield> <CFELSE> <cfset myvar = qry2.myfield> </CFIF>
webspinner - 25 Jan 2007 18:41 GMT But wouldn't I then need 15 If/else statements? What if I have 100 variables?
Isn't there a way to search the list of values for my variable in query 1 and then display all from query one and the remainder from query two?
webspinner - 25 Jan 2007 18:41 GMT But wouldn't I then need 15 If/else statements? What if I have 100 variables?
Isn't there a way to search the list of values for my variable in query 1 and then display all from query one and the remainder from query two?
Ian Skinner - 25 Jan 2007 19:17 GMT I have two queries in a CF page. What I want to do is look at the dataset from one, and if a given variable has a value in that, use it, otherwise use the same variable from query two. And there are around 15 variables. Is there a succinct way to do this?
Well the best way would probably do this in the query, if you and your DBMS are capable of it. But otherwise you can do what Dinghus suggested, but combined with a loop over the columnNames list or another list of your own design.
<cfloop list="#qry1.columnNames#" index="columnName"> <CFIF qry1[columnName] IS "whatever"> <cfset myvar = qry1[columnName]> <CFELSE> <cfset myvar = qry2[columnName]> </CFIF> </cfloop>
Dan Bracuk - 25 Jan 2007 20:40 GMT Just for fun,
are the two queries from the same database? if so, could you post the sql for both? also specify the db. do you use the queries for anything other than setting the variables?
As Ian said, maybe you can change the query and get what you want right off the bat.
webspinner - 25 Jan 2007 21:22 GMT I'm sort of succeeding. I have two remaining problems: 1) I'm rendering the results as two contiguous HTML tables, which prevents alphabetizing; and 2) my results are being displayed by a simple condition. The problem here is that it only works when the <cfif> is met. When the test fails and we go to <cfelse> for some reason THEN I get a syntax error on my QofQ WHERE clause.
<cfquery name="completed" datasource="req"> SELECT * FROM tbl_Req_SiteSpecific WHERE (tbl_Req_SiteSpecific.ID='#cookie.PID#') AND (tbl_Req_SiteSpecific.`Clinical Site Name`='#cookie.SelectBox#') </cfquery>
<cfquery name="q_reqmnts_f3" datasource="req" dbtype="odbc"> SELECT Req_SiteID, C_ID, C_Name, Req_Cat, Req_ReqID, Req, Resp_s, Resp_Site, R_Time, R_today FROM Q_req_reqmnts WHERE (C_Name = '#cookie.SelectBox#' and Resp_s = -1) ORDER BY Req_Cat </cfquery>
<cfquery name="unique_reqIDs" dbtype="query"> SELECT Req_reqID, req FROM q_reqmnts_f3 WHERE Req_reqID NOT IN (#valueList(completed.Req_reqID)#) </cfquery>
conditional: <cfif #completed.RecordCount# gt 0>
Thanks for any feedback!
Rick
paross1 - 25 Jan 2007 21:38 GMT Perhaps because if #completed.RecordCount# is 0, then completed.Req_reqID doesn't exist and (#valueList(completed.Req_reqID)#) has no meaning.
Phil
webspinner - 25 Jan 2007 21:43 GMT Thanks Phil,
I figured it was something like that, but wasn't sure how I could salvage what I've done and move forward.
Do I use something else as a conditional? Save something as a variable and use that for my conditional?
Thanks!
Rick
paross1 - 25 Jan 2007 21:55 GMT A simple way would probably just use the values of Req_reqID and req directly from the q_reqmnts_f3 query, since if the query completed doesn't exist, you don't have a NOT IN situation.
Phil
paross1 - 25 Jan 2007 21:55 GMT A simple way would probably just use the values of Req_reqID and req directly from the q_reqmnts_f3 query, since if the query completed doesn't exist, you don't have a NOT IN situation.
Phil
paross1 - 25 Jan 2007 22:52 GMT [Q]it might be possible to solve this problem with a single union query from your db.[/Q] No kidding.. maybe something like this.....
<cfquery name="new_query" datasource="req"> SELECT q.Req_reqID, q.req FROM Q_req_reqmnts q WHERE q.C_Name = '#cookie.SelectBox#' AND q.Resp_s = -1 AND NOT EXISTS (SELECT 1 FROM tbl_Req_SiteSpecific r WHERE r.tbl_Req_SiteSpecific.ID='#cookie.PID#' AND r.tbl_Req_SiteSpecific.`Clinical Site Name`='#cookie.SelectBox#' AND r.Req_reqID = q.Req_reqID) </cfquery>
Phil
Dan Bracuk - 26 Jan 2007 17:20 GMT they are table aliases. if you are this new, I have heard good things about Ben Forta's book, Teach Yourself SQL in 10 Minutes.
webspinner - 26 Jan 2007 18:42 GMT When I try to run this:
<cfquery name="new_query" datasource="req"> SELECT q.Req_reqID, q.req FROM Q_req_reqmnts q WHERE q.C_Name = '#cookie.SelectBox#' AND q.Resp_s = -1 AND NOT EXISTS (SELECT 1 FROM tbl_Req_SiteSpecific r WHERE r.tbl_Req_SiteSpecific.ID='#cookie.PID#' AND r.tbl_Req_SiteSpecific.`Clinical Site Name`='#cookie.SelectBox#' AND r.Req_reqID = q.Req_reqID) </cfquery>
I get this:
Error Executing Database Query. [Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
Dan Bracuk - 26 Jan 2007 19:26 GMT on this line AND r.tbl_Req_SiteSpecific.`Clinical Site Name`='#cookie.SelectBox#'
take the quotes away from `Clinical Site Name`
webspinner - 26 Jan 2007 19:42 GMT OK. What this is doing is successfully giving me the all the values not already in table A.
The problem is I want to include those values from table A, if present, and if not, take them from table b. It is the "c list" below. However, my HTML needs to include the a list, preferrably alphabetized within the additional values from table b.
Here is the a list: 33,31,68 Here is the b list: 101,3,4,12,12,52,69,22,2,31,5,8,7,6,13,1,68,126 Here is the c list: 5,6,7,2,3,1,8,13,52,22,12,12,69,126,101,4
Thanks again,
Rick
webspinner - 26 Jan 2007 20:01 GMT By outputting the two queries, one after the other, I get the effect I want, except (i.e., this is sounding like an earlier post of mine!):
1) they aren't alphabetized; and 2) the date of completion variable is only in one table.
Does it make sense to combine the result sets for "new_query" and "Q-reqmnts_f3" into some uber-result set, then do logic on the date field?
Thx!
Rick
webspinner - 29 Jan 2007 19:34 GMT I'm still at it. :-)
<cfquery name="GetJoined" dbtype="query"> SELECT * FROM completed, new_query WHERE 'completed.SiteID' = 'new_query.C_ID' </cfquery>
Trying to use QofQ to solve this. Does a QofQ where clause only work if the field names are identical from the two queries? This seems like is should work. Both queries have values that are the same in these fields. If CF is typeless why is this getting a recordcount of 0? Can I not select all from the different queries?
paross1 - 29 Jan 2007 20:53 GMT Remove the single quotes......
WHERE completed.SiteID = new_query.C_ID
Phil
webspinner - 29 Jan 2007 21:17 GMT Thanks Phil,
I think I had tried that. Now I get "Unsupported Type Comparision". If one of my fields is a text variable and one is numeric, is there a way to compare them?
Rick
paross1 - 29 Jan 2007 22:36 GMT I suppose that you might trying using CAST() in the original query to alter the data type of the field to match the other field. In other words, something like CAST(new_query.C_ID AS NUMERIC) or CAST(new_query.C_ID AS VARCHAR) in the orignal query as appropriate. Just be sure to alias the result and use that name in your Q-of-Q.
Phil
|
|
|