ms access - SQL grouping by words LIKE (with wildcards), how to achieve correct sum -


i working access query in excel vba. table contains mix of more general , specialised products, column strings e.g:

    mountain bike x2321, 10     enduro mountain bike special y3233, 20      mountain bike a2a321, 10     skateboard z23233, 10     skateboard y2dsf3, 10     skadeboard pro advanced special 20, etc 

i sum total sales values , aggregate sums categories specified in separate tableb, dictionary substrings: (

mountain bike special, mountain bike skateboard pro skateboard.

i sorted table in descending order longer names show before shorter ones. sorting expected join result in scateboard pro tested existence against record before scateboard tested.

my expected result be

mountain bike special   total 20 mountain bike           total  20 skateboard pro total  20 skateboard      total 20 

however query totals mountain bikes, special ones , skateboards, pro ones incorrect. them summed in separate categories. sorting descending tableb, dictionary did not hepl. :

mountain bike special   total 20 mountain bike                 total  40 skateboard pro total  20 skateboard      total 40 

my attempted query includes searching , wildcards, looks follows:

select "" kod, sum(b) wartosc, tableb.descr opis_grupy

from tablea left join tableb on tablea.descr (""+tableb.descr+"")

where tablea.a not null

group tableb.descr

order tableb.descr desc

unfortunately, such query not sum correctly. contemplating use of first np. http://www.mrexcel.com/forum/excel-questions/417807-sql-join-question-excel-access-only-return-first-join-instance.html (użycie funkcji first) have no idea how implement query should sum expected. hepling me out.

you try use (sub-)query first find longest matching category, like:

select tablea.descr adescr, tableb.descr bdescr tablea left join tableb on tablea.descr "*" + tableb.descr + "*" group tablea.descr, tableb.descr having len( tableb.descr ) = max( len( tableb.descr ) ) 

notice having clause limit results longest match tableb. once have dataset can aggregate bdescr in next step.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -