Solved

Do I use a subquery or group by

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Creating and Managing Databases with phpMyAdmin in cPanel.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now