473,756 Members | 8,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using results of query in a query

Hello all,
I'm looking for some hints as to how to use the results of a query in
another query. I assume I'm 'thinking' wrong in how to solve this, so
I'm hoping someone can clobber me and send me in the right direction.

My question is best illustrated by simplified example. I have one
table (table A) with a single column called "Word". This table
contains 100 records of single words. Another table (table B)
contains whole sentences in a single column. I want to eventually
count the occurances of all of the words in Table A, in each of the
sentences in Table B and store these results in another table. Here's
the 'thinking wrong' part. The only way I can see to do this is to
select the entire table A using a cursor, then select 'LIKE's of the
result against each individual row in table B. This seems horribly
inefficient, and I know there have to be one or two MUCH better ways
to do this, but I can't get me head wrapped around thinking in SQL at
this stage. Any info appreciated.

Thanks,
Stan
Jul 20 '05 #1
2 4085
st*********@hot mail.com (Stan) wrote in message news:<9a******* *************** ****@posting.go ogle.com>...
Hello all,
I'm looking for some hints as to how to use the results of a query in
another query. I assume I'm 'thinking' wrong in how to solve this, so
I'm hoping someone can clobber me and send me in the right direction.

My question is best illustrated by simplified example. I have one
table (table A) with a single column called "Word". This table
contains 100 records of single words. Another table (table B)
contains whole sentences in a single column. I want to eventually
count the occurances of all of the words in Table A, in each of the
sentences in Table B and store these results in another table. Here's
the 'thinking wrong' part. The only way I can see to do this is to
select the entire table A using a cursor, then select 'LIKE's of the
result against each individual row in table B. This seems horribly
inefficient, and I know there have to be one or two MUCH better ways
to do this, but I can't get me head wrapped around thinking in SQL at
this stage. Any info appreciated.

Thanks,
Stan


Something like this may get you started:

select
w.word,
p.phrase,
(len(p.phrase) - len(replace(p.p hrase, w.word, ''))) / len(w.word) as
'Count'
from dbo.Words w
join dbo.Phrases p
on p.phrase like '%' + w.word + '%'

This isn't very accurate because of short words which occur inside
larger ones, but it may be good enough, depending on your needs. You
can also have issues with punctuation, word boundaries etc. You might
need to consider doing this outside the database, with a language
which has better text processing capabilities.

Simon
Jul 20 '05 #2
Stan (st*********@ho tmail.com) writes:
I'm looking for some hints as to how to use the results of a query in
another query. I assume I'm 'thinking' wrong in how to solve this, so
I'm hoping someone can clobber me and send me in the right direction.

My question is best illustrated by simplified example. I have one
table (table A) with a single column called "Word". This table
contains 100 records of single words. Another table (table B)
contains whole sentences in a single column. I want to eventually
count the occurances of all of the words in Table A, in each of the
sentences in Table B and store these results in another table. Here's
the 'thinking wrong' part. The only way I can see to do this is to
select the entire table A using a cursor, then select 'LIKE's of the
result against each individual row in table B. This seems horribly
inefficient, and I know there have to be one or two MUCH better ways
to do this, but I can't get me head wrapped around thinking in SQL at
this stage. Any info appreciated.


SQL is not the most optimal tool for this problem, and you may be better
off writing a Perl script that gets the data and performs the counting.
Provided that you Perl, that is. (Well, VBscript, C etc might also be
better candidates.)

And why is SQL not the right tool? Becase, as Celko, would put it: you
have your data model wrong. OK, may not really in this case, but your
sentence column has a repeating group of words, and repeating groups is
really a no-no.

Anyway, there is an article on my web site that deals in long detail
about unpacking a list of items into a table. Mainly the article discusses
this for a single list (typically an input parameter), but there is a
very brief section that discusses this for tables. The direct link to
that section is http://www.sommarskog.se/arrays-in-s...#unpack-tblcol.

In your case, you would first have to strip the sentences of all punctuation
characters, so that you can use space as your delimiter.

--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2026
by: John Victor | last post by:
In my php page I'm using a mysql database. I want to run a query on a table, then run a second query on the results returned from the first query. In mysql this would be the same as running a query on a table to create a view, and then running a query on the resulting view. What is the php equivalent of this? Thanks.
0
4199
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS Excel 2003, I have VBA code that creates and executes queries using the Access database, and returns the results to an Excel sheet. The first time the query is executed, results are returned to Excel in usually less than 10 seconds. However, if the...
3
2535
by: bob.herbst | last post by:
I have been trying to use HTML_Table from PEAR to write a PHP script that will access a database and retrieve my data into an HTML table that can be sorted by column. Currently I am using the script below, which does not include sorting (I want the basic table to work first) but all I get is the column headers and no data in the column can anyone tell me how to fix this problem and have the script access my database to display the table...
10
6730
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that contains only tables. I have linked the tables for the front end to the back end database. I am trying to set the recordsource of a form to a query established by the user to narrow the scope but I don't want to display the form if there are no...
1
4286
by: tomlebold | last post by:
Having problems displaying query results from combo boxes on a sub form, which is on the same form that is used to select criteria. This has always worked form me when displaying query results on another main and sub form. The requery on the sub form and refresh comands on the main form do not work when the form is first displayed and when the selection criteria is changed. Should I be doing a refresh and then repaint of the sub form. ...
0
9212
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9973
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9790
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9779
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8645
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6473
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5247
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2612
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.