| Author |
Topic |
|
jparker
Posting Yak Master
118 Posts |
Posted - 2005-08-23 : 06:21:56
|
Hello,I have a table of data that I need to group by certain key words in the title and then to return the total amount for each of those groupings. For example : - FIELD 1 | FIELD 2My Apple Pie | 1.99Apples | 2.99Oranges | 0.99Orange Vest | 2.99Apple Turnovers | 2.99So the result I would like back is : -Apples 7.97Oranges 3.98Any ideas without having to do a load of different calls to the database and creating temp tables. Which I feel is the only way to do this!! Your help would be appreciated |
|
|
SMc
Starting Member
7 Posts |
Posted - 2005-08-23 : 06:50:35
|
| Assuming you know the keywords you can do something like:CREATE TABLE test (item char(15), cost money)INSERT INTO test VALUES ('My Apple Pie', 1.99)INSERT INTO test VALUES ('Apples', 2.99)INSERT INTO test VALUES ('Oranges', 0.99)INSERT INTO test VALUES ('Orange Vest', 2.99)INSERT INTO test VALUES ('Apple Turnovers', 2.99)SELECT s.term, SUM(t.cost) FROM test t INNER JOIN (SELECT 'Apple' AS term UNION SELECT 'Orange') s ON t.item LIKE '%' + s.term + '%' GROUP BY termDROP TABLE testThis gives the results you want, you could always use another table to hold the search terms and join to that instead. |
 |
|
|
jparker
Posting Yak Master
118 Posts |
Posted - 2005-08-23 : 07:04:46
|
| Excellent so what if I wanted to do the following : - CREATE TABLE test (item char(15), cost money)INSERT INTO test VALUES ('My Apple Pie', 1.99)INSERT INTO test VALUES ('Apples', 2.99)INSERT INTO test VALUES ('Banana', 0.99)INSERT INTO test VALUES ('Orange Vest', 2.99)INSERT INTO test VALUES ('Apple Turnovers', 2.99)CREATE TABLE test1 (item char(15))INSERT INTO test1 VALUES ('Apple')INSERT INTO test1 VALUES ('Banana')INSERT INTO test1 VALUES ('Oranges')SELECT s.term, SUM(t.cost)FROM test tINNER JOIN (SELECT item AS term from test1) s ON t.item LIKE '%' + s.term + '%'GROUP BY termDROP TABLE testDROP TABLE test1I get the results Banana .9900Whereas I wanted all three. Can you help please |
 |
|
|
SMc
Starting Member
7 Posts |
Posted - 2005-08-23 : 07:17:25
|
| You need to changeLIKE '%' + s.term + '%'toLIKE '%' + RTRIM(s.term) + '%'in your example because you have used a char field in test1 - changing the table to use varchar instead would also solve this problem.Note that in your example you will still only get results for Apple and Banana because Oranges doesn't match any of the strings you are searching, it would have to be Orange.HTH,Steve |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-23 : 07:20:39
|
| Otherwise use varchar data typeMadhivananFailing to plan is Planning to fail |
 |
|
|
jparker
Posting Yak Master
118 Posts |
Posted - 2005-08-23 : 07:20:42
|
| Excellent thank you for all your help SMcfinal code : -CREATE TABLE test (item varchar(25), cost money)INSERT INTO test VALUES ('My Apple Pie', 1.99)INSERT INTO test VALUES ('Apples', 2.99)INSERT INTO test VALUES ('Banana', 0.59)INSERT INTO test VALUES ('Oranges Vest', 2.99)INSERT INTO test VALUES ('Apple Turnovers', 2.99)CREATE TABLE test1 (item varchar(15))INSERT INTO test1 VALUES ('Apple')INSERT INTO test1 VALUES ('Banana')INSERT INTO test1 VALUES ('Orange')SELECT s.term, SUM(t.cost)FROM test tJOIN (SELECT item AS term from test1) s ON t.item LIKE '%' + s.term + '%'GROUP BY s.termDROP TABLE testDROP TABLE test1RESULTApple 7.9700Banana .5900Orange 2.9900 |
 |
|
|
SMc
Starting Member
7 Posts |
Posted - 2005-08-23 : 07:32:28
|
| You're welcome :) |
 |
|
|
jeffreyfrog
Starting Member
1 Post |
Posted - 2009-06-04 : 17:05:35
|
| I think all of them answer your question,, butif you have any question or problem may this could help you..[url]team building[/url] |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-04 : 17:14:47
|
What is the reason for you to do that? (kaput link) No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-06-05 : 02:14:00
|
quote: Originally posted by jeffreyfrog I think all of them answer your question,, butif you have any question or problem may this could help you..[url]team building[/url]
How can it be helpful? MadhivananFailing to plan is Planning to fail |
 |
|
|
|