Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Problem with subquery returning more than 1 value [Solved]

Problem with subquery returning more than 1 value [Solved]

Scheduled Pinned Locked Moved Database
helpdatabasetutorialquestion
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J James Shao

    Hi, I have a table with 1000 stock tickers and their historical prices.   Today I was trying to write a simple query to retrieve all the stocks with 11/20/2009 prices greater than their 11/17/2009 prices.   However my query returned an error saying, "Subquery returned more than 1 value, this is not permitted when the subquery follows .....". I think I know what the problem is, however I am not sure how to fix this.   Is there an alternative query I can write to get around this? Thanks!! Below's my current query:

    modified on Monday, January 11, 2010 10:52 AM

    D Offline
    D Offline
    dan sh
    wrote on last edited by
    #2

    You might be having something like this:

    select columns from tablename where somecolumn = (select someColumn1 from sometableone)

    Now, that subquery is returning multiple values and hence "=" won't work. Replace "=" by "in" or check the sub query if it should return only one value.

    50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

    J 1 Reply Last reply
    0
    • J James Shao

      Hi, I have a table with 1000 stock tickers and their historical prices.   Today I was trying to write a simple query to retrieve all the stocks with 11/20/2009 prices greater than their 11/17/2009 prices.   However my query returned an error saying, "Subquery returned more than 1 value, this is not permitted when the subquery follows .....". I think I know what the problem is, however I am not sure how to fix this.   Is there an alternative query I can write to get around this? Thanks!! Below's my current query:

      modified on Monday, January 11, 2010 10:52 AM

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #3

      Show us what you have and someone will help.

      Bob Ashfield Consultants Ltd Proud to be a Code Project MVP

      J 1 Reply Last reply
      0
      • A Ashfield

        Show us what you have and someone will help.

        Bob Ashfield Consultants Ltd Proud to be a Code Project MVP

        J Offline
        J Offline
        James Shao
        wrote on last edited by
        #4

        Last night I had some trouble pasting the codes properly in the forum, fortunately the problem is fixed now. Yes I currently have: SELECT Ticker FROM Table WHERE Date = '11/20/2009' AND Price > (Select Price from Table WHERE Date = '11/17/2009')

        D D 2 Replies Last reply
        0
        • D dan sh

          You might be having something like this:

          select columns from tablename where somecolumn = (select someColumn1 from sometableone)

          Now, that subquery is returning multiple values and hence "=" won't work. Replace "=" by "in" or check the sub query if it should return only one value.

          50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

          J Offline
          J Offline
          James Shao
          wrote on last edited by
          #5

          Thanks Danish, I've posted in a reply thread below.   The operator I am using is a ">" instead of a "=", so using "IN" probably won't work for me.   Could you please let me know how I can get around this problem?   I'm not really sure why subquery would still return more than 1 result, since I've specified that Table.Ticker = t2.Ticker and Date = a specific date in the subquery, thus it should only return the price on that specific date for that specific stock.... Thanks in advance!

          1 Reply Last reply
          0
          • J James Shao

            Last night I had some trouble pasting the codes properly in the forum, fortunately the problem is fixed now. Yes I currently have: SELECT Ticker FROM Table WHERE Date = '11/20/2009' AND Price > (Select Price from Table WHERE Date = '11/17/2009')

            D Offline
            D Offline
            dxlee
            wrote on last edited by
            #6

            You can use a join similar to this one:

            select a.ticker from table as a inner join table as b
            on a.ticker=b.ticker and a.price>b.price
            where a.date='11/20/2009' and b.date='11/17/2009'
            group by a.ticker;

            J 1 Reply Last reply
            0
            • J James Shao

              Hi, I have a table with 1000 stock tickers and their historical prices.   Today I was trying to write a simple query to retrieve all the stocks with 11/20/2009 prices greater than their 11/17/2009 prices.   However my query returned an error saying, "Subquery returned more than 1 value, this is not permitted when the subquery follows .....". I think I know what the problem is, however I am not sure how to fix this.   Is there an alternative query I can write to get around this? Thanks!! Below's my current query:

              modified on Monday, January 11, 2010 10:52 AM

              C Offline
              C Offline
              Corporal Agarn
              wrote on last edited by
              #7

              You can always you TOP 1.

              SELECT TOP 1 mycolumn FROM mytable

              This probably will not work for your case but I thought I would throw it in.

              J 1 Reply Last reply
              0
              • J James Shao

                Last night I had some trouble pasting the codes properly in the forum, fortunately the problem is fixed now. Yes I currently have: SELECT Ticker FROM Table WHERE Date = '11/20/2009' AND Price > (Select Price from Table WHERE Date = '11/17/2009')

                D Offline
                D Offline
                dan sh
                wrote on last edited by
                #8

                You must be having multiple prices for that date. If you need the top record only, use order by and top 1 in you statement.

                50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                J 1 Reply Last reply
                0
                • C Corporal Agarn

                  You can always you TOP 1.

                  SELECT TOP 1 mycolumn FROM mytable

                  This probably will not work for your case but I thought I would throw it in.

                  J Offline
                  J Offline
                  James Shao
                  wrote on last edited by
                  #9

                  Thank you this also works   djj55   :)

                  1 Reply Last reply
                  0
                  • D dan sh

                    You must be having multiple prices for that date. If you need the top record only, use order by and top 1 in you statement.

                    50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                    J Offline
                    J Offline
                    James Shao
                    wrote on last edited by
                    #10

                    Thank you D@nish for the tip, yes indeed there are multiple prices on same dates belonging to a ticker, though that shouldn't happen.   Bad data, may need to redownload. Top 1 solves this for now.   Many thanks again.

                    1 Reply Last reply
                    0
                    • D dxlee

                      You can use a join similar to this one:

                      select a.ticker from table as a inner join table as b
                      on a.ticker=b.ticker and a.price>b.price
                      where a.date='11/20/2009' and b.date='11/17/2009'
                      group by a.ticker;

                      J Offline
                      J Offline
                      James Shao
                      wrote on last edited by
                      #11

                      Thank you Dxlee, not only that it works, I learned a new way of using inner join table!   I used to think inner join is only for joining two different tables, but now I see that you can use it to link to the same table filtered for some criterias.   This is going to change the way I write queries from now on   :-D Could you please recommend a good online source where I can learn how to use SQL more effectively?   Using INNER JOIN in this manner (seems creative and ingenious to me) was not covered in W3Schools nor in most SQL tutorial websites.   Many thanks again!

                      D 1 Reply Last reply
                      0
                      • J James Shao

                        Thank you Dxlee, not only that it works, I learned a new way of using inner join table!   I used to think inner join is only for joining two different tables, but now I see that you can use it to link to the same table filtered for some criterias.   This is going to change the way I write queries from now on   :-D Could you please recommend a good online source where I can learn how to use SQL more effectively?   Using INNER JOIN in this manner (seems creative and ingenious to me) was not covered in W3Schools nor in most SQL tutorial websites.   Many thanks again!

                        D Offline
                        D Offline
                        dxlee
                        wrote on last edited by
                        #12

                        I think d@nish's solution is better for your case, even though you found my inner join to be new and helpful to you. I am sorry I don't have a book or website to recommend to you on SQL. I learned something about SQL by reading an Oracle manual on SQL*Plus (which has lots of very interesting examples) about 20 years ago (in 1990) when I was working on a project for a friend. I moved a few times (to different countries) ever since and lost that manual. It was for an old version of Oracle anyway (which ran on VMS with no graphical user interface.) The new Oracle manuals seem to have nothing similar to the one I read. Your best bet is to find a good book on SQL.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups