Results 1 to 15 of 15
  1. #1
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    65

    Subquery Problem

    Hi Guys,

    I have a problem with a subquery that I can't seem to figure out.

    I've built a media database in Access. I'm in the process of creating some queries that will be used for data display purposes. They will be converted to views when the database is upsized to SQL Server.

    I have one particular query that is giving me real problems. The query itself is very simple except for one piece which requires a subquery. The query produces basic totals for each Artist:

    Total Albums: Count(Title)
    Total Songs: Sum(TrackCount)
    Total Playing Time: Sum(TotalTime)

    I have this part of the query done and it produces the expected values. There is one addition element that I need to add to this query which is the album cover art field for the oldest album for each artist. I use Min(ReleaseYear) to get the Release Year of the oldest album for each Artist. What I'm trying to do is retrieve the Album Cover Art field for this album for each Artist. The field's name is CoverArt and it's an OLE Object field.



    I know that I need a subquery to do this and I've tried it every way I can think of with no success.I would prefer to do this in the same query. My subquery skills are a bit rusty and if someone would be kind enough to assist me with this part of the query I would be most grateful.

    Here's the query:

    Code:
    SELECT tblArtists.Artist, Min(tblArtistTitles.YearReleased) AS MinOfYearReleased, Count(tblTitles.Title) AS TotalAlbums, Sum(tblMediaLocations.TrackCount) AS TotalSongs, Sum(tblMedia.TotalTime) AS TotalPlayTime
    FROM tblTitles INNER JOIN (((tblArtists INNER JOIN tblArtistTitles ON tblArtists.ArtistID = tblArtistTitles.ArtistID) INNER JOIN tblMediaLocations ON (tblArtistTitles.MediaTypeID = tblMediaLocations.MediaTypeID) AND (tblArtistTitles.YearReleased = tblMediaLocations.YearReleased) AND (tblArtistTitles.TitleID = tblMediaLocations.TitleID) AND (tblArtistTitles.ArtistID = tblMediaLocations.ArtistID)) INNER JOIN tblMedia ON (tblMediaLocations.DiskNumber = tblMedia.DiskNumber) AND (tblMediaLocations.MediaTypeID = tblMedia.MediaTypeID) AND (tblMediaLocations.YearReleased = tblMedia.YearReleased) AND (tblMediaLocations.TitleID = tblMedia.TitleID) AND (tblMediaLocations.ArtistID = tblMedia.ArtistID)) ON tblTitles.TitleID = tblArtistTitles.TitleID
    GROUP BY tblArtists.Artist;
    Thanks so much for your help.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Maybe TOP N parameter would help. Review http://allenbrowne.com/subquery-01.html#TopN
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    65
    Tried that too, no luck

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    65
    Here's a copy of my DB
    Attached Files Attached Files

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    You want the oldest album cover for each artist included in the aggregate query? What is the name of the query you posted? I am not finding exact match.

    I added an autonumber field to tblArtistTitles to provide a unique record ID. Then query to pull the record of oldest album year for each artist:

    SELECT ArtistID, TitleID, YearReleased, CoverArt FROM tblArtistTitles WHERE ID IN
    (SELECT TOP 1 ID FROM tblArtistTitles AS Dupe WHERE Dupe.ArtistID=tblArtistTitles.ArtistID ORDER BY Dupe.ArtistID, Dupe.YearReleased, Dupe.TitleID);

    Join that query to aggregate query. Need to include ArtistID in the aggregate query.


    I've never used OLEObject field, never could get OLEObject control to do what I want. I use Image Control to dynamically display images. I set its ControlSource property to an attachment field or to a text field that has file path of external image file.

    Using special characters (@, #) as part of an ID value is really unusual. This might cause some issues.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    65
    The query name is qryArtistTotals. There are some additional fields in the query such as Artist and Min(ReleaseYear) but this is the query that produces the required totals for each artist. Thank you for your other suggestions, but I'm content with my database design the way it is.

  8. #8
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    65
    The @ and # being used as part of keys is for a reason. The first character of each key is the first letter of the artist or title. In the case where a title starts with a number I use the # and in the case where a title starts with a special character I use the @. I don't like using the autonumber field as a key so this is my solution. Thanks for your advice.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    I added the autonumber field because it was the only way I could get the nested query to work. It does not have to be the key field. So none of the table relationships are changed. You will find a unique identifier like the autonumber useful for various nested queries.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    65
    Well, I have yet to find a good use for them myself. They are a lazy excuse for defining a real key. My Sys Admin and DB Admin instructors in college taught me that a key field should be a unique piece of DATA not some magic number that has no meaning. Anyway, the ReleaseYear identifies the unique record in question for each artist. Thanks for the assist I appreciate it.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Yes, but the year is not unique in the table. Multiple records have the same year. Still need a unique ID for the query to work. As I said, the autonumber is not being used as primary key.

    There are competing points of view on use of autonumber as primary key. I've developed only in Access but I understand the autonumber may be unique to Access. I have used both autonumber and custom generated ID as primary key. I personally do not like compound keys and use an autonumber to avoid them.

    Good luck with your project.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    65
    Year is unique to the album in question for each Artist. An Artist can only have one album that's the oldest so it should suit my needs. I don't like compound keys to a point either, but I like Autonumber even less. This DB is going to be upsized to SQL Server once I'm done creating the queries I need so that I'll have greater flexibility in creating my DAL as well as the ability to create Stored Procs. I don't mean to seem stubborn in my point of view I guess I'm something of a purist. My instructors at DeVry were very good at what they did and what they taught me has served me well. Thanks so much for your help.

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Year is unique to each title for each artist but it is not unique in the table. The nested query needs a unique record identifier. I gave it one more try without the autonumber field. Required using 3 fields to produce unique ID. See if this gets desired output.

    SELECT ArtistID, YearReleased, TitleID, CoverArt FROM tblArtistTitles
    WHERE (((ArtistID & YearReleased & TitleID) In (SELECT TOP 1 ArtistID & YearReleased & TitleID FROM tblArtistTitles AS Dupe WHERE Dupe.ArtistID=tblArtistTitles.ArtistID ORDER BY Dupe.ArtistID, Dupe.YearReleased, Dupe.TitleID)));

    Wasn't trying to change your point of view nor did I even suggest the autonumber should be primary key. But perhaps you can see that autonumber made the query simpler.

    I think SQL table can be set to generate a unique record ID but doesn't have the same constraints as Access autonumber. http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    And really, what is 'purist' about not using autonumber? I suggest the DeVry instructors were too rigid and inflexible on this point. I have found both autonumber and custom ID useful in database structure.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    65
    I did not say that ReleaseYear by itself was a unique value in the table, nor did I say that I was looking for the oldest album in the table. I said that I was looking for the oldest album for each artist so that I can retrieve the album cover art field for that album. The primary key for the ArtistTitles table is ArtistID, TitleID, ReleaseYear, and MediaTypeID so your concatenation of these fields is of no great surprise to me. ReleaseYear is included in the key because it is possible for an Artist to have more than one album by the same title and MediaTypeID is part of the key to distinguish different media by the same Artist with the same title such as an album and a related live concert video.

    I appreciate your efforts to help me resolve this issue.

  15. #15
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Both of the queries I suggested (with and without autonumber) accomplish retrieving "the oldest album for each artist". If you want the oldest album for each title for each artist, that is different.

    +
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Subquery
    By tomclavil in forum Queries
    Replies: 3
    Last Post: 02-27-2012, 03:05 AM
  2. TOP subquery
    By helpaccess in forum Queries
    Replies: 5
    Last Post: 08-30-2011, 10:28 AM
  3. Access SQL Subquery Problem
    By dfenton21 in forum Access
    Replies: 2
    Last Post: 07-26-2011, 07:54 AM
  4. Subquery sum?
    By anemoskkk in forum Access
    Replies: 0
    Last Post: 04-29-2011, 12:36 PM
  5. Problem with subquery
    By bakerdenn in forum Queries
    Replies: 1
    Last Post: 04-24-2009, 10:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums