Solved

Do I use a subquery or group by

Posted on 2014-11-14
1
73 Views
Last Modified: 2015-01-14
Using MySQL, I'm looking to get the sum dollar amount of all licenses in our system, broken out or grouped by album.  Our albums are stored as 6 character codes, example ABC001.  Unfortunately the license tables don't store album, only the album_track, which is the album code with a number appended, example ABC001_12.  So I have to do a fuzzy search for 'ABC001%'.  

I'm able to run this query to get the summed license values:

SELECT SUM(a.LicenseAmount)
FROM db1.sync_license_apps a
inner join db1.sync_license_apps_music_used b
on a.appl_ReferenceID = b.fk_appl_ReferenceID
where appl_musicUsedTrackCatNumber like 'ABC001%';

The problem is that I want to essentially run this as a subquery to my Albums table - SELECT album_code from albums.  But I can't do this because I have to use a LIKE filter.

Any idea how to accomplish this?  Thanks!
0
Comment
Question by:Xbradders
1 Comment
 
LVL 13

Accepted Solution

by:
Koen Van Wielink earned 500 total points
ID: 40444150
Based on your description you might not even need your album table:

SELECT SUM(a.LicenseAmount)
, left(appl_musicUsedTrackCatNumber,6) as 'Album_code'
FROM db1.sync_license_apps a
inner join db1.sync_license_apps_music_used b 
on a.appl_ReferenceID = b.fk_appl_ReferenceID
group by left(appl_musicUsedTrackCatNumber,6)  

Open in new window


If your album code is always 6 characters long this will group LicenseAmount from sync_license_apps by the album code taken from the column appl_musicUsedTrackCatNumber.
If you do want to add a table called Albums which joins on that album code, you're looking at something like this:

Select C.*
, s.licenseAmount
, s.Album_code
from (
SELECT SUM(a.LicenseAmount)
, left(appl_musicUsedTrackCatNumber,6) as 'Album_code'
FROM db1.sync_license_apps a
inner join db1.sync_license_apps_music_used b 
on a.appl_ReferenceID = b.fk_appl_ReferenceID
group by left(appl_musicUsedTrackCatNumber,6)
) as s
inner join db1.albums c
on s.album_code = c.album_code

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Currency in SQL? 2 43
CLI command keep running after close 7 56
Comparison query - 4 columns 9 42
Unwanted output from my query 5 40
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

713 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