Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Do I use a subquery or group by

Posted on 2014-11-14
1
Medium Priority
?
76 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 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

721 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