Solved

MS Access crosstab query -- driving me nuts

Posted on 2016-11-13
5
55 Views
Last Modified: 2016-11-13
OK -- let's say I have two unjoined tables in MS Access. I'm looking for a crosstab query that will find and count instances of each word in Table_A in the strings within records in Table_B. I've made a couple of passes using a crosstab query but get no output. I know I need to pivot the data and I know I need to use something like a Like operator but I'm having trouble assembling it into a workable query. I've actually considered doing this programmatically but thought it might be worth posting first since this is likely a cleaner solution if I can get it to work. BTW, there is no constraint on whether tables A and B can be linked or not -- just hadn't gone there yet.

Table_A
red
yellow
blue


Table_B
I have a red pomegranate.
I have a yellow banana.
I have a red apple.


Results
                        Word count
red                        2
yellow                  1
blue                        0

I'm in a design phase right now, so the table contents are actually pretty close to what my test data and tables look like. Here's a variation on one of the crosstabs I've tried so far:

TRANSFORM Count(Table_B.ID) AS CountOfID
SELECT Table_A.Word
FROM Table_A, Table_B
WHERE (((Table_B.ShortText) Like '* [Table_A].[Word] *'))
GROUP BY Table_A.Word
PIVOT Table_B.ShortText;

Any help that can be supplied here would be GREAT.
0
Comment
Question by:alfamikefoxtrot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41885705
try this

SELECT TableA.FieldA, Sum(IIf(InStr([TableB].[FieldB],[TableA].[FieldA])>0,1,0)) AS WordCount
FROM TableA, TableB
GROUP BY TableA.FieldA;
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41885719
SELECT Table_A.Word, Sum(IIf(InStr([Table_B].[ShortText],[Table_A].[Word])>0,1,0)) AS WordCount
 FROM Table_A, Table_B
 GROUP BY Table_A.Word
0
 
LVL 48

Assisted Solution

by:Dale Fye
Dale Fye earned 100 total points
ID: 41885737
This is not a CrossTab query, this is a Cartesian Join, where you include two unjoined tables in a query and then use a where clause limit the recordset, although could use a non-equi join to get the same feature.

I think Rey's post should give you the answer, but a non-equi join would look like:

SELECT Table_A.Word, Count(B.Sentence) as WordCount
 FROM Table_A
LEFT JOIN Table_B ON TableB.Sentence Like "*" & TableA.Word & "*"
 GROUP BY Table_A.Word
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 400 total points
ID: 41885740
better use this query

SELECT Table_A.Word, Sum(IIf(InStr(" " & [Table_B].[ShortText] & " "," " & [Table_A].[Word] & " ")>0,1,0)) AS WordCount
 FROM Table_A, Table_B
 GROUP BY Table_A.Word

to eliminate word embedded in another word to be counted. i.e.,   red will be counted for word like prepared
0
 

Author Closing Comment

by:alfamikefoxtrot
ID: 41885757
Excellent -- thanks to both of you. Really, nice, elegant solution. And good point on the cartesian join.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question