Solved

Do I use a subquery or group by

Posted on 2014-11-14
1
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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