473,394 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

CrossTab Query three tables

171 100+
I am trying to connect three tables in a crosstab query.
Tbl-1 Field - Staff
Tbl-2 Field - Document
Tbl-3 Fields - Staff Document Viewed
What I am trying is to get a crosstab query or Vb module which can list all the staff from Tbl-1 as the row heading and all the Documents from Tbl-2 as the Column heading and put "Yes" or "No" if the staff viewed the document which is available in Tbl-3
Much appreciated if someone can help me

Regards
Sep 28 '12 #1
36 3634
zmbd
5,501 Expert Mod 4TB
Create a normal select query (Q) that returns all of these values.
Once that is done the cross-tab should be quite easy to create based on (Q)
Sep 28 '12 #2
rajeevs
171 100+
Thank you for the response. But I have tried that approach. The crosstab returns only the staff with Viewed. But my issue is I need to see all the staff from Tbl-1, all the documents in Tbl-2 and the value field to show the staff viewed and the staff names not in the Tbl-3 but available in Tbl-1.
As an example if total staff is 100 in Staff Tbl and Tbl-3 has only 90 staff details who viewed the document I need to crosstab Row source with 100 staff, column heading with the document names and the values as yes or no according to the status of viewed. The viewed field is a date field.
I can generate the crosstab with 90 staff who are available in Tbl-3, but the other 10 staff from the Tbl-1 is not showing. Please help.

Thank you once again
Sep 28 '12 #3
zmbd
5,501 Expert Mod 4TB
Create the select query, change to piviot table view, drag and drop the fields into the correct areas of the piviot table.
V2003: http://office.microsoft.com/en-us/ac...001034580.aspx
V2010: http://office.microsoft.com/en-us/ac...101901543.aspx
Sep 29 '12 #4
rajeevs
171 100+
It still shows the same data. Because the select qry can show only the joined fields match. So when I make Pivot it shows the staff which match with the tbl-3 staff. not showing the staff which is not in Tbl-3. I want the unmatched also in the Staff list which is in Tbl-1.
Sep 29 '12 #5
Rabbit
12,516 Expert Mod 8TB
You need to use an outer join in your query.
Sep 29 '12 #6
zmbd
5,501 Expert Mod 4TB
Thank you Rabbit
At a friends house tonight and lost the network connection just as I clicked post!
....see if I'm back online?!?!?

Yes, back UP!

Rajeevs, please post your SQL, remember to format it in a code block by using the <CODE/> button
Sep 29 '12 #7
rajeevs
171 100+
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. Tbl1.Staff, 
  3. Tbl2.Document, 
  4. Tbl3.Viewed
  5. FROM Tbl2 
  6.  INNER JOIN 
  7.  (Tbl1 INNER JOIN Tbl3 
  8.    ON Tbl1.Staff = Tbl3.Staff) 
  9.    ON Tbl2.Document = Tbl3.Document;
  10.  
Sep 29 '12 #8
zmbd
5,501 Expert Mod 4TB
Have you made any headway on this?
You need to relate the documents to the staff and you want to relate every document to every staff memeber.
Once you have this relationship, you can then relate that back to the third table.

[[[edit]]This doesn't work for access[[/Edit]]
Or you could left join staff on the third
then left join document on the third
then setup a third relationship between these two queries.]
Sep 29 '12 #9
rajeevs
171 100+
Hi zmbd
Thank you for all the effort you showed to help me out. But still I am getting the same result what i was getting earlier. I cannot see all the document names which is in tbl2 but not in tbl3. Can you suggest a different approach with a VBA module which can check all the staff in tbl1, all the documents in tbl2 and staff against the viewed status of documents in Tbl3 and insert to a new table. I am not so good in Vb but I can understand code and know how to change it if i get a bit of it. Hope you can help me. Thank you once again
Oct 3 '12 #10
Rabbit
12,516 Expert Mod 8TB
rajeevs, there's no need for code, you just need to use a left or right outer join instead of the inner joins that you're using. It's a very simple change.
Oct 3 '12 #11
zmbd
5,501 Expert Mod 4TB
I don't understand how you are not able to get the query to work using either Rabbit's or my approach.
I just tested the approach I gave you on one of my test databases... worked very well.
I'm currently playing with the SQL you posted in #8 to see which one(s) to make left/right joins on... if Rabbit said it can be done, I suspect that it can....

Please read the posts again and post your efforts.
Oct 3 '12 #12
zmbd
5,501 Expert Mod 4TB
@Rabbit:
Tweaking SQL in post 8 every which way and all I get is errors from msaccess.
However, If I do a cross-product to expand staff against documents and then relate this back against the viewed documents on the staff_pk and document_pk then I can return exactly what OP wants... all staff in the rows and all documents in the columns with a single yes/no/blank at the intersections.

Easier method?
Oct 4 '12 #13
Rabbit
12,516 Expert Mod 8TB
The only thing that I see that may be causing errors is the parentheses they're trying to use. But if you reorder the joins a little bit, and get rid of the parentheses, I think it should work by doing a table1 left join table3 left join table2.
Oct 4 '12 #14
zmbd
5,501 Expert Mod 4TB
Thnx Rabbit: I thought I had tried that and MSA tossed an "Ambiguous Outer Joins…" However, the test DB I built is on the home PC while listening to the children try to bathe each other (2x3yr+1x9yr)=chaos+flooded_bathroom… I kind-of had to go put a stop to that before the bathtub floated down the street!
;-)
I'll give that rearrangement a shot when I get home...
Oct 4 '12 #15
rajeevs
171 100+
Dear Zmbd

Can you send me your test DB so I can have a look and try to build the same way you did.
Thank you
Oct 10 '12 #16
zmbd
5,501 Expert Mod 4TB
I'll pass that along this evening... sorry the DB is at home :)

In the meantime, will you please post what you have been working on to solve this?

(While I'm at it: Rabbit, tried the suggestion as given in #14, that doesn't seem to work)
Oct 10 '12 #17
Rabbit
12,516 Expert Mod 8TB
I'll wait till you post your sample db as I'm too lazy to mock one up right now lol.
Oct 10 '12 #18
zmbd
5,501 Expert Mod 4TB
Just waiting for rajeevs to post the most recent effots... otherwise the information in post 9 is how I built the DB for the cross-product-to-hasread queries.
However, the second paragraph in 9 didn't work quite as expected... stupid jet.
Oct 12 '12 #19
rajeevs
171 100+
No luck so far. I am stuck with another issue in another db. I will be posting that because it is very critical for me at the moment. But to this issue so far i can make a report based on viewed staff and documents. Main issue is still pending
Hope you will give me your test DB.

Thank you
Oct 12 '12 #20
rajeevs
171 100+
I am still waiting for zmbd's sample db. because the query approach was not clear for me
Oct 17 '12 #21
twinnyfo
3,653 Expert Mod 2GB
rajeevs (et al),

Try this query. I did not use any right or left joins, because it would cause ambiguous joins and the query would not work. I simply count how many records are not null, and if it is not, then the person has viewed the document.....

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM IIf(Count(Tbl3.Staff) Is Not Null,"Yes","No") AS Viewed
  2. SELECT Tbl1.Staff
  3. FROM Tbl2
  4.     INNER JOIN (Tbl1 INNER JOIN Tbl3
  5.         ON Tbl1.Staff = Tbl3.Staff)
  6.         ON Tbl2.Document = Tbl3.Document
  7. GROUP BY Tbl1.Staff
  8. PIVOT Tbl2.Document;
  9.  
Hope this hepps....
Oct 17 '12 #22
zmbd
5,501 Expert Mod 4TB
I had read R's last post to mean that R would post latest efforts.
Oct 17 '12 #23
zmbd
5,501 Expert Mod 4TB
@ Twinnyfo:
Tried your query in my test DB.
If in the documents table there is (are) a document(s) that no-one has beend assigned (or read) then that title does not show in the cross-tab.
Nor does is show all of the staff.
So this is the result using your query:



Whereas this is what I think OP is after:
(well, I didn't do the fill in the blanks as your query does... maybe I'll do that here in a moment, easier on the eyes me-thinks.)




{Edit} Rememebered why I left those empty cells...
Staff/Document intersection:
Blank = not assigned
No = Assigned but not read
Yes = Assigned and read.{/edit}

Attached Images
File Type: jpg TF_Qry_3tblCTQ.jpg (43.9 KB, 665 views)
File Type: jpg Z_Qry_3tblCTQ.jpg (26.8 KB, 620 views)
File Type: jpg Z_Qry_3tblCTQ_pretty.jpg (52.5 KB, 670 views)
Oct 17 '12 #24
twinnyfo
3,653 Expert Mod 2GB
Z,

How did you join your tables? After playing around with this one, I presume you came upon this:

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM IIf(Count(Tbl3.Staff) Is Not Null,IIf(Count(Tbl3.Staff)>0,"Yes","No")) AS Viewed
  2. SELECT Tbl1.Staff
  3. FROM Tbl2
  4.     LEFT JOIN (Tbl1
  5.         RIGHT JOIN Tbl3 ON Tbl1.Staff = Tbl3.Staff)
  6.         ON Tbl2.Document = Tbl3.Document
  7. GROUP BY Tbl1.Staff
  8. PIVOT Tbl2.Document;
  9.  
I agree with you that values in each row/column is not so easy on the eyes....
Oct 17 '12 #25
zmbd
5,501 Expert Mod 4TB
Nice try,
You return all of the documents accross the top; however, now you only return a blank row for unassigned staff to documents and only those staff with documents assigned to them.
You also can not do two lefts to the assigned as JET chokes
Oct 17 '12 #26
twinnyfo
3,653 Expert Mod 2GB
Mine seems to work fine with the two lefts. You just have to get them in the right order. And, when the Value result is "None", it idicates the document is not assigned to anyone (or hasn't been viewed by anyone).

My question is how would we know a document was assigned to a person? IF the document is listed in Tbl3, assigned to a person, that seems to indicate that it has been read. Another (perhaps,better) thought is to add a check box field to Tbl3, just to indicate whether the document has been viewed. Use that field as the Value, either as max or min or sum. Then, if a document is assigned to someone, it will show up in the column headings, and it will indicate whether it is read. Also, if a document is not assigned to anyone, it will not show up on the list. This also would not require any right or left joins.....
Oct 17 '12 #27
zmbd
5,501 Expert Mod 4TB
By two lefts:
Tbl_RequiredReading
RequiredReading_PK
FK_Staffpk
FK_Documentpk

If you open query and try to left join the tbl_staff on primary key to tbl_RequiredReading AND
left join tbl_documents to tbl_RequiredReading
(tbl_staff.staff_pk LEFT Tbl_RequiredReading.fk_staffpk
(tbl_documents.documents_pk LEFT Tbl_RequiredReading.fk_documentspk)
You will get an error.

If you: staff_pk>required_fk_staff::required_fk_document>d ocuments_pk you also do not return as OP desired.

Twinnyfo - From what I read in the second part of your post, you have the basic design of my test db - My db:
tbl_staff
staff_pk - autonum
staff_fname - text(50)

tbl_documents
documents_pk - autonum
documents_title - text(50)

tbl_requiredreading
requiredreading_pk - autonum
[{FK_Staff_inRR - numeric(long) (tbl_Staff(1:m))}
{FK_Documents_inRR - numeric(long)(tbl_documents(1:m))}
compound index as unique not pk]
requiredreading_hasread - boolean(default-false)

As you've guessed, what I've done:
In tbl_requiredreading, if the document is assigned to the staff member as a required document to read then a record is entered with FK_requiredreading_Staff=Staff_pk and FK_requiredreading_Documents=documents_pk. When the Staff reads the document requiredreading_hasread=true.

What you suggest does not meet OP's requirements... it appears that OP wants something along an EXCEL worksheet that has all of the titles accross the top, assigned or not, and all of the staff along the rows - even if no assigned reading has been made. (or pivot the header/row) This makes perfect sense to me as it is easy to tell if the document has been assigned to anyone and the status etc... the solution I used is given in post#9
Oct 17 '12 #28
twinnyfo
3,653 Expert Mod 2GB
Z,

Well, I will admit that I was unclear from the original post exactly what fields were actually included in OP's tables. That being said, this can also be done without any relationships at all between the tables in the query, since we just want a complete list of all staff and a complete list of all documents. Then, we just determine if the assigned reading table meets the criteria of the rows/columns. If so, then determine if the doc has been read based on the Check, and convert to a readable format. The below maintains OP's original table/field names, also fulfilling all requirements of all staff and all docuemnts.


Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Max(IIf(Tbl-3.Staff=Tbl-1.Staff And Tbl-3.Document=Tbl-2.Document,IIf(Viewed,"Read","Not Read"))) AS Read
  2. SELECT Tbl-1.Staff
  3. FROM Tbl-1, Tbl-2, Tbl-3
  4. GROUP BY Tbl-1.Staff
  5. PIVOT Tbl-2.Document;
  6.  

I am just looking for simpler ways to skin this cat. I am not, however, trying to beat a dead horse. I just want to understand the solution better myself. Your solution in Post #9, when I did it, produced the error of ambiguous outer joins, which is why I've been throwing other ideas out there. Reversing the join does not produce an error, but will not include all staff or documents when unassigned....
Oct 17 '12 #29
zmbd
5,501 Expert Mod 4TB
Twinny,
That caught it.
In the attached, it is qry_twinnyfo3... I've also included your other suggestions in the DB

Post#9: 1st: crossproduct between staff and documents then do the double left join between the crossproduct and the required reading table. The advantage here is no iif statement for the SQL engine to bugger.

Post#9 second part fails - I noted that in post#19, I've also edited post#9 noting that the suggestion in paragraph 2 doesn't work.

Attached is in V2010 give a jingle if needed in v2003
Attached Files
File Type: zip PivotTable.zip (51.6 KB, 103 views)
Oct 17 '12 #30
twinnyfo
3,653 Expert Mod 2GB
Z,

Thanks for the test DB. I can understand now what you've done and how you got there. I am sure that your method would be much quicker with large sets of data, as well.

Thanks for your advice on this, as it helps me in my query design as well...
Oct 17 '12 #31
rajeevs
171 100+
Hi zmbd
I tried to copy the attached DB, but I am running Access 2007 and I am unable to open this DB. Could you advise how to?
Regards
Oct 22 '12 #32
zmbd
5,501 Expert Mod 4TB
I saved it in V2010 and it should be compatible with 2007 as I used only the most basic of elements...
Silly question ... please honestly I mean no offense... did you un-zip it first?
I sent some files to a friend once, he's quite intelligent and he couldn't get the files to open... turns out... he was trying to open the zipped Excel workbooks... chuckle.
Oct 23 '12 #33
rajeevs
171 100+
Hi zmbd

Yes - I unzip the file using the program 7-Zip. My windows ver is Windows 7 Professional and office with Access 2007.
After unzip, when I tried to open the DB, I get the error message as unrecognized database format for pivottable.accdb

Regards
Oct 23 '12 #34
zmbd
5,501 Expert Mod 4TB
win7 64bit
or 32bit?
Oct 23 '12 #35
rajeevs
171 100+
Win7 64 bit and office 2007
Oct 24 '12 #36
zmbd
5,501 Expert Mod 4TB
Most unfortunate.
D/L to WinXP(MCE)SP3 at home used windows to extract... file opened.
Tried to convert to v2003; however, some setting is preventing that ... something about general sort order. I'll try that agian later.

In the meantime, the information provided in post#28 and in the 1st part of post#9 should be enough to re-create the DB on your end.
Oct 24 '12 #37

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

Similar topics

1
by: Matthew Wells | last post by:
I have a crosstab query based on anothe query. The base query resultset has no null values in its "Quantity" column. However, when I create the new crosstab query from the base query, the records...
0
by: Kaur | last post by:
Hi, I have created a report based on a cross tab query and would like to Sort the column heading of the report based on the sort order of another field. I have three tables called survey...
2
by: Nenad Markovic | last post by:
Hi everybody, When executing a Crosstab Query I see only rows (defined in a row heading) that have values (defined in value field) in at least one column (defined as column headings). How can...
2
by: jason.teen | last post by:
Hi All, I am having trouble creating a crosstab query. In my original data I have two columns, One called "Categorized" and one called "Mapped' in which those columns can hold values of "true"...
6
by: John | last post by:
Hi I have three tables with a common id with which they can be linked. I need to merge them in a way that the resultant table has all records from three tables. Below is what sort of result I am...
6
by: Peter Herath | last post by:
I want to create a dynamic report using a crosstab query...... pls someone look into my attached example database and help me out to do the report generation.... example is like dis...: there...
0
by: Peter Herath | last post by:
I want to create a custormizable report . For an example, there's a form with four combo boxes and two of them having database tables columns/field names as values in the combo box(one for select row...
4
by: m.wanstall | last post by:
I have a crosstab query that compiles data for Months of the year. I have a stacked select query on top of that crosstab query that uses the latest 2 months data and exports it to a fixed length...
6
by: keyur1719 | last post by:
Hi, I have a crosstab query which is based on a simple select query. Here is how the query works.. The base query gets it date from employee table and their incentives table for the given...
3
by: NigelBrown | last post by:
Hi All, I have written the below UNION QUERY (I know its wrong but trying to show what I want to achieve) I would like all three tables to convert to one - this is only a one off every day and the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.