| Author |
Topic |
|
HomerJ
Starting Member
21 Posts |
Posted - 2010-05-27 : 12:10:03
|
| I have two tables, the first is a list of product names (PRODUCT), the other is a list of text requests (REQUESTS). I'd like to count the number of times a product is requested in a request. Something like:SELECT PRODUCT.NAME, (SELECT COUNT(*) FROM REQUESTS AS TEMP_A WHERE TEMP_A.QUESTION LIKE '* PRODUCT.NAME *')FROM PRODUCTGROUP BY PRODUCTI'm not sure how to write the LIKE statement to find the string of PRODUCTS. For clarity, the PRODUCT list uses the same Product name multiple times, but with differing sizes, colors, etc (that's why the GROUP is there). Thanks. |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-05-27 : 12:16:11
|
| replace * with % and seeif that helps.JimEveryday I learn something that somebody else already knew |
 |
|
|
HomerJ
Starting Member
21 Posts |
Posted - 2010-05-27 : 12:19:10
|
| The problem, I think, is that the quotes offset the PRODUCT.NAME as text instead of a field reference. Can I use wildcards in the statement w/o using quotes?Should I be using a different function? CONTAINS seems likely, but I don't understand the syntax. |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-05-27 : 12:51:47
|
| The % is the wildcard for sql server. LIKE '% abc %' is different than LIKE '%abc%'. The former will look for 'abc' with a blank before it and after in the name field, the latter will look for 'abc' anywhere in the name field. I may not fully understand what you're trying do. Can you give an example of what's in TEMP_A.QUESTION and PRODUCT.NAME?JimEveryday I learn something that somebody else already knew |
 |
|
|
HomerJ
Starting Member
21 Posts |
Posted - 2010-05-27 : 13:04:47
|
| Example:Table 1 is the PRODUCTS table (~20,000 records), with fields:PRODUCTNAME, COLOR, SIZESample data:BIKE, BLUE, 40BIKE, BLUE, 45BALL, Basketball, RegulationBALL, BASEBALL, RegulationTable 2 is the REQUESTS table (~6000 records) that stores all online requests, with fields:EMAIL, DATE, REQUEST_TEXTSample data:bob@yahoo.com, 4/15/2010, I want a bike.bill@yahoo.com, 4/16/2010, I like basketball.sara@yahoo.com, 5/1/2010, Do you have bikes?So what I want to do is to summarize the PRODUCTS table:BIKEBALLand then count the number of times the PRODUCT shows up in the REQUESTsBIKE (2)BALL (1)But I don't know how to write the syntax so that I can summarize all of the product types, and then uses that shortened list |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-05-27 : 13:30:07
|
| SELECT p.Productname,count(*) as [Count]FROM PRODUCTS pINNER JOIN REQUESTS rON r.request_text like '%'+p.productname + '%'JimEveryday I learn something that somebody else already knew |
 |
|
|
HomerJ
Starting Member
21 Posts |
Posted - 2010-05-27 : 14:59:06
|
| "Cannot Join on Memo, OLE, or Hyperlink Object."Thanks for the help so far, but we're not there yet. |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-05-27 : 15:29:17
|
| I'm assuming then that request_text is an actual TEXT field, you could tryONconvert(varchar(max),r.request_text) like '%'+p.productname + '%'is there an actual need for request_text to be text field (more than 8000 characters)? Also, to avoid any future surprises, please provide DDLs for all the involved tables with some sample data for each.JimJimEveryday I learn something that somebody else already knew |
 |
|
|
HomerJ
Starting Member
21 Posts |
Posted - 2010-05-27 : 15:45:57
|
| I'm not sure if the field 'needs' to be greater than 8000, I'm just getting imports from a external website. I'll try the conversion. (Sorry about the memo/text thing, didn't think for a second that it wouldn't be text) |
 |
|
|
HomerJ
Starting Member
21 Posts |
Posted - 2010-05-27 : 16:05:43
|
| Victory! By making a temporary table of REQUEST and converting the REQUEST_TEXT field to text (instead of memo) I was able to run your statement. Thanks Jim for the help! |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-05-27 : 17:42:45
|
| Glad I could help!jimEveryday I learn something that somebody else already knew |
 |
|
|
|