Solved

Do I use a subquery or group by

Posted on 2014-11-14
1
72 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a SQL query that creates a header row and one or more detail rows. 7 53
MySQL Backup Strategy 15 44
MySQL - need to join three tables 2 46
query question 12 32
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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 …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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