473,471 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

feed query into second query

tuxalot
200 New Member
I have a query that produces data like this:

Expand|Select|Wrap|Line Numbers
  1. name        date    q1    q2    q3    q4    q5
  2. Subject A    3/1/08    1    2    1    2    1
  3. Subject A    5/3/09    1    2    1    1    2
  4. Subject A    6/4/10    2    1    2    2    2
  5. Subject B    4/5/08    1    1    2    1    2
  6. Subject C    2/7/09    1    2    2    2    1
  7. Subject C    3/8/10    1    2    1    2    1
  8.  
q1-100 represent responses to questions. I am only interested in responses = 2 for consecutive years. So I would like to feed the above into a second query to produce this result:

Expand|Select|Wrap|Line Numbers
  1. name        date    q1    q2    q3    q4    q5
  2. Subject A    3/1/08        2                
  3. Subject A    5/3/09        2                2    
  4. Subject A    6/4/10                         2    
  5. Subject B    4/5/08                        
  6. Subject C    2/7/09        2        2        
  7. Subject C    3/8/10        2        2        
  8.  
Any ideas how I can achieve this? Thanks for any assistance.
Nov 30 '10 #1
4 1409
Oralloy
988 Recognized Expert Contributor
Break your logic down into steps.

I'd probably do this in three steps to start with:

1) Build a query to select all the (subject, year, date) tripples, call it Candidate back to your table. The important part here is that we're selecting out the years:
Expand|Select|Wrap|Line Numbers
  1. SELECT name, Year(date) AS [year], date
  2.   FROM Data
Call this query Candidate...

2) Build a full-join from Candidate against its self to find all possible candidate pairs - something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT c0.name,
  2.              c0.date AS [date0],
  3.              c1.date AS [date1]
  4.   FROM Candidate AS c0, Candidate AS c1
  5.   WHERE (c0.name = c1.name)
  6.         AND
  7.         (c0.year+1 = c1.year)
Call this query CandidatePair...

3) Test all identified candidate pairs - like so:
Expand|Select|Wrap|Line Numbers
  1. SELECT cp.*
  2.   FROM CandidatePair AS cp, Data AS d0, Data AS d1
  3.   WHERE ((cp.subject = d0.subject) AND
  4.          (cp.date = d0.date))
  5.         AND
  6.         ((cp.subject = d1.subject) AND
  7.          (cp.date = d1.date))
  8.         AND
  9.         (((d0.q1 = 2) AND (d0.q1 = d1.q1))
  10.          OR
  11.          ((d0.q2 = 2) AND (d0.q2 = d1.q2))
  12.          OR
  13.          ((d0.q3 = 2) AND (d0.q3 = d1.q3))
  14.          OR
  15.          ((d0.q4 = 2) AND (d0.q4 = d1.q4))
  16.          OR
  17.          ((d0.q5 = 2) AND (d0.q5 = d1.q5)))
Call this FoundCandidatePair....

Then you'll have to do a UNION query to garner your desired rows:
Expand|Select|Wrap|Line Numbers
  1. SELECT d0.*
  2.   FROM FoundCandidatePair AS fcp0,
  3.        Data AS d0
  4.   WHERE (fcp0.name=d0.name) AND (fcp0.date=d0.date)
  5. UNION
  6. SELECT d1.*
  7.   FROM FoundCandidatePair AS fcp1,
  8.        Data AS d1
  9.   WHERE (fcp1.name=d1.name) AND (fcp1.date=d1.date)
Probably there's an easier way, however I'm fairly sure this'll get the job done.

Good Luck!
Nov 30 '10 #2
tuxalot
200 New Member
WOW! Thanks Oralloy for your insight. I am getting an error at your FoundCandidatePair step. When I run my query qxtbRspnsIDxQstnLvls I get these results:

Expand|Select|Wrap|Line Numbers
  1. Response    HName    SurveyStartDate    1    2    3    4    5    6    7    8    9    10    11    12    29    30    33
  2. 1    Test A    11/1/2010    1    2    2    1                                    1    2    1
  3. 2    Test A    10/1/2009    2    2    1    1                                    1    1    1
  4.  
Some of the questions are not answered, so as you can see above there is no data to below some of the questions. In my sample data the only matching question (both responses = 2) is question 2.

So using your code (I hope I modified it correctly), this is what I get:

Step 1
Expand|Select|Wrap|Line Numbers
  1. SELECT Hname, Year(SurveyStartDate) AS [year], SurveyStartDate
  2. FROM qxtbRspnsIDxQstnLvls;
  3.  
Produces this:

Hname year SurveyStartDate
Test A 2010 11/1/2010
Test A 2009 10/1/2009

Step 2
Expand|Select|Wrap|Line Numbers
  1. SELECT c0.Hname, c0.SurveyStartDate AS date0, c1.SurveyStartDate AS date1
  2. FROM Candidate AS c0, Candidate AS c1
  3. WHERE (c0.Hname = c1.Hname)
  4. AND
  5.        (c0.year+1 = c1.year);
  6.  
Produces this:

Hname date0 date1
Test A 10/1/2009 11/1/2010

Step 3

Expand|Select|Wrap|Line Numbers
  1. SELECT cp.*
  2. FROM CandidatePair AS cp, qxtbRspnsIDxQstnLvls AS d0, qxtbRspnsIDxQstnLvls AS d1
  3. WHERE ((cp.Hname = d0.Hname) AND
  4.          (cp.date = d0.date))
  5.         AND
  6.         ((cp.Hname = d1.Hname) AND
  7.          (cp.date = d1.date))
  8.         AND
  9.         (((d0.1 = 2) AND (d0.1 = d1.1))
  10.          OR
  11.          ((d0.2 = 2) AND (d0.2 = d1.2))
  12.          OR
  13.          ((d0.3 = 2) AND (d0.3 = d1.3))
  14.          OR
  15.          ((d0.4 = 2) AND (d0.4 = d1.4))
  16.          OR
  17.          ((d0.5 = 2) AND (d0.5 = d1.5)))
  18.  
  19.  
Note: questions are not actually preceded by the letter "Q" as I put in my example
Also, I will have upwards of 150 questions. Once I get this going, is there a way I can loop through instead of specifying each question as you did above?
Nov 30 '10 #3
Oralloy
988 Recognized Expert Contributor
Oh my....

At this point you have several options:
  1. [#]Build support VB code to dig through the CandidatePair result, load each pair of records, and test them.
    [#]Build support VB code to run a query which selects all fields associated with each CandidatePair record.
    [#]Build the complete SQL query FoundCandidatePair

All three are a pain, how you progress depends on how you need the output information.

One thing you need to be aware of is that many databases limit the number of fields in an SQL statement. I don't know if Access has a 256 field limit or not. This is, however, something you need to be aware of, given the size of your problem space.

Let's assume that you have a table FoundCandidatePair, that you are trying to populate. Let's try some pseudocode, as I'm not willing to build a database and actually code this out...

Expand|Select|Wrap|Line Numbers
  1. RecordSet0 = DB.Execute("SELECT * FROM CandidatePair")
  2.  
  3. FOR EACH Record0 IN RecordSet0
  4.   name = "'" + Record0("name") + "'"
  5.   date0 = Record0("date0")
  6.   date1 = Record0("date1")
  7.  
  8.   queryA = "SELECT * FROM Data WHERE (name=" + name + ") AND (date=" + date0 + ")"
  9.   queryB = "SELECT * FROM Data WHERE (name=" + name + ") AND (date=" + date1 + ")"
  10.  
  11.   RecordSetA = DB.Execute(queryA)
  12.   RecordA = RecordSetA.fetch       ' or whatever the VBA code is to do this
  13.   RecordSetB = DB.Execute(queryB)
  14.   RecordB = RecordSetB.fetch
  15.  
  16.   FOR EACH FieldA IN RecordA.fields
  17.     IF FieldA.name = "name" THEN
  18.       ' noOp
  19.     ELSE IF FieldA.name = "date" THEN
  20.       ' noOp
  21.     ELSE IF FieldA.value <> 2 THEN
  22.       ' not interesting
  23.     ELSE IF FieldB(FieldA.name).value <> 2 THEN
  24.       ' not interesting
  25.     ELSE
  26.       ' match, save the candidate as a found
  27.     END IF
  28.   END FOR EACH
  29. END FOR EACH
There are a couple problems, like multiple column matches that you can sort out.

However, I hope this makes sense, so you can get on with your project.

Luck!
Oralloy
Dec 1 '10 #4
tuxalot
200 New Member
thanks again Oralloy. I'll muss around and see what I can get going.
Dec 1 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Stu | last post by:
I have the following code which I am having difficulty getting to work. I think it may be a problem with the $got query that is being run as if I set that to a set value then my site seems to run...
1
by: Don Seckler | last post by:
Ok, I took a whack at this, but it's still not returning anything. Granted I used Dreamweaver to create the queries, but I must be messed up somewhere. Any help would be appreciated. 1st...
4
by: laurenq uantrell | last post by:
I am trying to determine which of three stored procedure designs are fastest in the Query Analyzer: One query is a straight SELECT query with all desired rows and a dozen (tblName.RowName =...
6
by: Sven Pran | last post by:
Probably the answer is there just in front of me only awaiting me to discover it, but: 1: I want to build a query that returns all records in one table for which there is no successful "join"...
6
by: johntarr | last post by:
I hope I am not asking about something that has been done before, but I have searched and cannot find an answer. What I am trying to do is to run a query, and then perform some logic on the...
1
by: mamun | last post by:
Hi All, I am doing it for the first time in C# (using Visual Studio 2005), First I need to check if data exists in a table for a variable. If so then run the second query and display the...
4
by: lorirobn | last post by:
Hi, I have a report displaying items that are missing from a room. I created 2 queries, the first getting the items IN the room, and the second being an "unmatched" query that references the...
1
by: imnewtoaccess | last post by:
Hi all, I have created a query in msaccess with a parameter. Now I want to query that query but without the parameter. Is it possible ?
0
by: =?Utf-8?B?VG9ueSBBLg==?= | last post by:
I'm trying to develop a parameter query using Query Builder, the database is Access 2003. I placed a ? in the column to we quered and linked it to a checkbox on the form. When the user selects an...
1
by: ziycon | last post by:
Ok, i got the main query working, my issue is that the GROUP BY isn't working, its should only show one of each location: SELECT _teamdb_country.name FROM _teamdb, _teamdb_country WHERE...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.