+ Reply to Thread
Results 1 to 16 of 16

Having trouble writing a SELECT query

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Unhappy Having trouble writing a SELECT query

    Attached is a demo version of the database. I'm trying to return [tblSong$].ID, [tblTitle$].Name, [tblArtist$].Name

    I have tried multiple rewrites of the query without making any headway. (The most recent attempt was:
    PHP Code: 
    SELECT [tblSong$].ID, [tblTitle$].Name, [tblArtist$].Name FROM [tblTitle$], [tbjSongArtist$] LEFT OUTER JOIN [tblSong$] ON [tblSong$].ID = [tbjSongArtist$].fkSong LEFT OUTER JOIN [tblArtist$] ON [tblArtist$].ID = [tbjSongArtist$].fkArtist WHERE [tblTitle$].ID = [tblSong$].fkTitle 
    )


    Where am I going wrong?
    Attached Files Attached Files
    Last edited by mc84excel; 12-02-2014 at 01:18 AM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Having trouble writing a SELECT query

    I used MS Query in the attached. Here is the SQL statement I used.

    PHP Code: 
    SELECT `tblSong$`.ID, `tblTitle$`.Name, `tblArtist$`.Name
    FROM 
    `C:\Users\Alan Sidman\Desktop\excelforum_DB_Originals.xlsx`.`tbjSongArtist$` `tbjSongArtist$`, `C:\Users\Alan Sidman\Desktop\excelforum_DB_Originals.xlsx`.`tblArtist$` `tblArtist$`, `C:\Users\Alan Sidman\Desktop\excelforum_DB_Originals.xlsx`.`tblSong$` `tblSong$`, `C:\Users\Alan Sidman\Desktop\excelforum_DB_Originals.xlsx`.`tblTitle$` `tblTitle$`
    WHERE `tblSong$`.ID = `tbjSongArtist$`.fkSong AND `tblArtist 
    Attached Files Attached Files
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Having trouble writing a SELECT query

    Quote Originally Posted by alansidman View Post
    I used MS Query in the attached. Here is the SQL statement I used.

    PHP Code: 
    SELECT `tblSong$`.ID, `tblTitle$`.Name, `tblArtist$`.Name
    FROM 
    `C:\Users\Alan Sidman\Desktop\excelforum_DB_Originals.xlsx`.`tbjSongArtist$` `tbjSongArtist$`, `C:\Users\Alan Sidman\Desktop\excelforum_DB_Originals.xlsx`.`tblArtist$` `tblArtist$`, `C:\Users\Alan Sidman\Desktop\excelforum_DB_Originals.xlsx`.`tblSong$` `tblSong$`, `C:\Users\Alan Sidman\Desktop\excelforum_DB_Originals.xlsx`.`tblTitle$` `tblTitle$`
    WHERE `tblSong$`.ID = `tbjSongArtist$`.fkSong AND `tblArtist 
    Thanks for the help Alan. I am getting an error message -2147217887?

    My converted query is:
    PHP Code: 
    SELECT [tblSong$].ID, [tblTitle$].Name, [tblArtist$].Name FROM [tblTitle$], [tbjSongArtist$], [tblTitle$], [tblSong$], [tblArtist$] WHERE [tblSong$].ID = [tbjSongArtist$].fkSong AND [tblArtist$] 
    Is there meant to be something after [Artist$]?

    Also the results tab on the upload is not returning songs that haven't had an artist assigned. Is it possible to include these too?

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: Having trouble writing a SELECT query

    I have not written SQL in Excel, but in Access it requires a semi-colon ";" at the end of the SQL statement. Apparently, MS Query does not supply this. Try adding the semi-colon at the end of the statement. To force the songs that haven't an artist assigned, you will have to change the the join to a left or right join to make this happen.

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Having trouble writing a SELECT query

    Quote Originally Posted by alansidman View Post
    I have not written SQL in Excel, but in Access it requires a semi-colon ";" at the end of the SQL statement. Apparently, MS Query does not supply this. Try adding the semi-colon at the end of the statement.
    Same error. My query string was
    PHP Code: 
    SELECT [tblSong$].ID, [tblTitle$].Name, [tblArtist$].Name 
    FROM 
    [tblTitle$], [tbjSongArtist$], [tblTitle$], [tblSong$], [tblArtist$] 
    WHERE [tblSong$].ID = [tbjSongArtist$].fkSong AND [tblArtist$]; 

    Quote Originally Posted by alansidman View Post
    To force the songs that haven't an artist assigned, you will have to change the the join to a left or right join to make this happen.
    That's what I thought. But I cant seem to get the query to work!

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Having trouble writing a SELECT query

    Stay away from SQL courses folks. Waste of money. I did a course once and I occasionally run into trouble writing Join queries...

    Below is my latest attempt. (It didn't work). I'm not sure about the last JOIN. How can I join tblArtist to tblSong when I am really matching on tbjSongArtist (a junction table)

    PHP Code: 
    SELECT [tblSong$].ID, [tblTitle$].Name, [tblArtist$].Name FROM [tblSong$] LEFT OUTER JOIN [tblTitle$] ON [tblTitle$].ID = [tblSong$].fkTitle LEFT OUTER JOIN [tbjSongArtist$] ON [tbjSongArtist$].fkSong = [tblSong$].ID LEFT OUTER JOIN [tblArtist$] ON [tblArtist$].ID = [tbjSongArtist$].fkArtist 

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Having trouble writing a SELECT query

    How exactly are you running the query?

    If you are using code can you post it?

    Also, what are you trying to return with the query?
    If posting code please use code tags, see here.

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Having trouble writing a SELECT query

    Quote Originally Posted by Norie View Post
    How exactly are you running the query?

    If you are using code can you post it?

    Also, what are you trying to return with the query?
    Good to hear from you again Norie

    Wouldn't you know it, I just solved the main problem. Putting brackets around the JOINs made it happy.

    However I would like to improve the result if possible.

    The query currently returns an array of 204 rows (a song with multiple artists returns 1 row per artist) and, ideally, I would like to return an array of 199 rows (1 song per row)

    To illustrate the problem by an example. Song ID 186 currently appears in the array as two rows:

    --186--AnExampleTitleName186--Alison Krauss
    --186--AnExampleTitleName186--Brad Paisley

    In the final array, I'd like song 186 to return as 1 row:

    --186--AnExampleTitleName186--Alison Krauss (insert separator of your choice here) Brad Paisley


    I have a vague idea that this can be achieved through judicious use of GROUP BY?

    I have uploaded a testing XLSM & a test database. To test it, save both files, open the XLSM then call TestForNorie (the attribute being the fullname of wherever you saved the XLSX file).

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Having trouble writing a SELECT query

    You can't do that, it's a limitation of using excel as a datasource, the sql engine simply isn't comprehensive enough. This isn't trivial even In mssql, sql just isn't designed to do that without hacking around.

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Having trouble writing a SELECT query

    Quote Originally Posted by Kyle123 View Post
    You can't do that, it's a limitation of using excel as a datasource,
    Hi Kyle, it would seem that neither of us are budging on our positions re using Excel as a database (@Everybodyelse - This is a private joke between me & Kyle)

    All jokes aside - What has the datasource have to do with limiting a query like this? I know I have a lot to learn but I would have assumed that any limitations would have been a consequence of the SQL flavour and/or the connection used? Could you please elaborate slightly for my education?

    Quote Originally Posted by Kyle123 View Post
    the sql engine simply isn't comprehensive enough. This isn't trivial even In mssql, sql just isn't designed to do that without hacking around.
    I am sure I have seen this done before through a query that used GROUP BY on the equivalent to Artist.

    Oh well, it's not essential. At this stage of the project, I am working on a form for the user to select 1 song from. If the user selects either version of Song ID 186 that appears in the current array, it will still return the correct primary key - which is all that really matters. (It just would have looked neater to have any songs that are duets etc with all artists for that song appearing in one field of the array)

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Having trouble writing a SELECT query

    I don't really think this has much to do with Excel being the data source, doing this with any other data source would be complicated enough and might even involve the use of code.

    Actually this sort of thing sounds a bit like trying to denormalize things.

  12. #12
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Having trouble writing a SELECT query

    Quote Originally Posted by Norie View Post
    Actually this sort of thing sounds a bit like trying to denormalize things.
    How so? I only want it this outcome for improving the look of the array for end-user selection purposes...

    In any case, it's not an essential goal (see my previous reply to Kyle) so I will mark this thread as solved.

  13. #13
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Having trouble writing a SELECT query

    To use your terminology, it depends what 'flavour' of SQL the data source supports. AFAIK (though I could well be wrong here) you can only use standard SQL with Excel and Access (at least you could last time I used access 2003!). As you are no doubt aware SQL is designed to fill a specific hole, set based, declarative queries of a relational database. However, as you have found out, traditional vanilla sql is also very simple and quite limiting, as such most database vendors have extended 'base' sql to add additional functionality. Each database then departs from the standard sql syntax and publishes their own superset - T-SQL for MSSQL, PL/SQL for Oracle, SQL/PSM for MySQL etc... By using these databases, you can leverage this functionality.

    As way of an example, here's what your concat query would look like in MSSQL:
    MSSQL
    PHP Code: 
    SELECT [tblsong$].id
           [
    tbltitle$].[name], 
           
    Stuff((SELECT ', ' Cast(artist.[name] AS VARCHAR(max)) 
                  
    FROM   [tblartist$] artist 
                         INNER JOIN 
    [tbjsongartist$] 
                                 
    ON fkartist artist.id 
                                    
    AND fksong song.id 
                  
    FOR xml path(''), type).value('.''VARCHAR(MAX)'), 12''
           
    artists 
    FROM   
    [tblsong$] song 
           LEFT JOIN 
    [tbltitle$] 
                  
    ON [tbltitle$].id = [tblsong$].fktitle 
    GROUP  BY 
    [tblsong$].id
              [
    tbltitle$].NAME 
    And here it is in MySQL (the Group_concat function makes this much more cogent IMO)

    MySQL
    PHP Code: 
    SELECT [tblsong$].id
           [
    tbltitle$].[name], 
           
    Group_concat(DISTINCT artist.[nameORDER BY artist.[name]) AS artists 
    FROM   
    [tblsong$] 
           
    LEFT JOIN [tbltitle$] 
                  
    ON [tbltitle$].id = [tblsong$].fktitle 
           LEFT JOIN 
    [tbjsongartist$] 
                  
    ON [tbjsongartist$].fksong = [tblsong$].id 
           LEFT JOIN 
    [tblartist$] artist 
                  ON 
    [tblartist$].id = [tbjsongartist$].fkartist 
    GROUP  BY 
    [tblsong$].id
              [
    tbltitle$].[name
    I honestly don't know whether Access now offers any extensions to standard SQL, it's been a long time since I've used it - though a quick google suggests that you'd have to write a UDF in VBA to accomplish this particular task.

    I too disagree with Norie, were you trying to store your data in this manner then there'd be a de-normalization issue, however there is a big difference between data storage and presentation - I do however find your decision to store the song title in a separate table a step too far in terms of Normalization, are there really that many songs with the same title? Though there may be something that I missed in your design.

  14. #14
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Having trouble writing a SELECT query

    To use your terminology, it depends what 'flavour' of SQL the data source supports. AFAIK (though I could well be wrong here) you can only use standard SQL with Excel and Access (at least you could last time I used access 2003!). As you are no doubt aware SQL is designed to fill a specific hole, set based, declarative queries of a relational database. However, as you have found out, traditional vanilla sql is also very simple and quite limiting, as such most database vendors have extended 'base' sql to add additional functionality. Each database then departs from the standard sql syntax and publishes their own superset - T-SQL for MSSQL, PL/SQL for Oracle, SQL/PSM for MySQL etc... By using these databases, you can leverage this functionality.

    As way of an example, here's what your concat query would look like in MSSQL:
    MSSQL
    PHP Code: 
    SELECT [tblsong$].id
           [
    tbltitle$].[name], 
           
    Stuff((SELECT ', ' Cast(artist.[name] AS VARCHAR(max)) 
                  
    FROM   [tblartist$] artist 
                         INNER JOIN 
    [tbjsongartist$] 
                                 
    ON fkartist artist.id 
                                    
    AND fksong song.id 
                  
    FOR xml path(''), type).value('.''VARCHAR(MAX)'), 12''
           
    artists 
    FROM   
    [tblsong$] song 
           LEFT JOIN 
    [tbltitle$] 
                  
    ON [tbltitle$].id = [tblsong$].fktitle 
    GROUP  BY 
    [tblsong$].id
              [
    tbltitle$].NAME 
    And here it is in MySQL (the Group_concat function makes this much more cogent IMO)

    MySQL
    PHP Code: 
    SELECT [tblsong$].id
           [
    tbltitle$].[name], 
           
    Group_concat(DISTINCT artist.[nameORDER BY artist.[name]) AS artists 
    FROM   
    [tblsong$] 
           
    LEFT JOIN [tbltitle$] 
                  
    ON [tbltitle$].id = [tblsong$].fktitle 
           LEFT JOIN 
    [tbjsongartist$] 
                  
    ON [tbjsongartist$].fksong = [tblsong$].id 
           LEFT JOIN 
    [tblartist$] artist 
                  ON 
    [tblartist$].id = [tbjsongartist$].fkartist 
    GROUP  BY 
    [tblsong$].id
              [
    tbltitle$].[name
    I honestly don't know whether Access now offers any extensions to standard SQL, it's been a long time since I've used it - though a quick google suggests that you'd have to write a UDF in VBA to accomplish this particular task.

    I too disagree with Norie, were you trying to store your data in this manner then there'd be a de-normalization issue, however there is a big difference between data storage and presentation - I do however find your decision to store the song title in a separate table a step too far in terms of Normalization, are there really that many songs with the same title? Though there may be something that I missed in your design.

  15. #15
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Having trouble writing a SELECT query

    To use your terminology, it depends what 'flavour' of SQL the data source supports. AFAIK (though I could well be wrong here) you can only use standard SQL with Excel and Access (at least you could last time I used access 2003!). As you are no doubt aware SQL is designed to fill a specific hole, set based, declarative queries of a relational database. However, as you have found out, traditional vanilla sql is also very simple and quite limiting, as such most database vendors have extended 'base' sql to add additional functionality. Each database then departs from the standard sql syntax and publishes their own superset - T-SQL for MSSQL, PL/SQL for Oracle, SQL/PSM for MySQL etc... By using these databases, you can leverage this functionality.

    As way of an example, here's what your concat query would look like in MSSQL:
    MSSQL
    PHP Code: 
    SELECT [tblsong$].id
           [
    tbltitle$].[name], 
           
    Stuff((SELECT ', ' Cast(artist.[name] AS VARCHAR(max)) 
                  
    FROM   [tblartist$] artist 
                         INNER JOIN 
    [tbjsongartist$] 
                                 
    ON fkartist artist.id 
                                    
    AND fksong song.id 
                  
    FOR xml path(''), type).value('.''VARCHAR(MAX)'), 12''
           
    artists 
    FROM   
    [tblsong$] song 
           LEFT JOIN 
    [tbltitle$] 
                  
    ON [tbltitle$].id = [tblsong$].fktitle 
    GROUP  BY 
    [tblsong$].id
              [
    tbltitle$].NAME 
    And here it is in MySQL (the Group_concat function makes this much more cogent IMO)

    MySQL
    PHP Code: 
    SELECT [tblsong$].id
           [
    tbltitle$].[name], 
           
    Group_concat(DISTINCT artist.[nameORDER BY artist.[name]) AS artists 
    FROM   
    [tblsong$] 
           
    LEFT JOIN [tbltitle$] 
                  
    ON [tbltitle$].id = [tblsong$].fktitle 
           LEFT JOIN 
    [tbjsongartist$] 
                  
    ON [tbjsongartist$].fksong = [tblsong$].id 
           LEFT JOIN 
    [tblartist$] artist 
                  ON 
    [tblartist$].id = [tbjsongartist$].fkartist 
    GROUP  BY 
    [tblsong$].id
              [
    tbltitle$].[name
    I honestly don't know whether Access now offers any extensions to standard SQL, it's been a long time since I've used it - though a quick google suggests that you'd have to write a UDF in VBA to accomplish this particular task.

    I too disagree with Norie, were you trying to store your data in this manner then there'd be a de-normalization issue, however there is a big difference between data storage and presentation - I do however find your decision to store the song title in a separate table a step too far in terms of Normalization, are there really that many songs with the same title? Though there may be something that I missed in your design.

  16. #16
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Having trouble writing a SELECT query


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Having trouble writing a formula with FormulaR1C1
    By octaviogro in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-10-2013, 06:01 PM
  2. [SOLVED] Trouble writing if formula
    By tandi in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 08-10-2012, 11:13 AM
  3. Replies: 1
    Last Post: 05-05-2011, 12:44 PM
  4. Help! Trouble writing VBA scripts
    By kirba in forum Excel General
    Replies: 1
    Last Post: 12-10-2009, 11:28 PM
  5. Trouble writing an excel formula.
    By hbb2699 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-08-2006, 01:36 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1