Sponsored by: Bodybuilding Warehouse - Amazing prices and FREE P&P on bodybuilding supplements from Gaspari, Nutrex, BSN NO-Xplode, Animal Pak & more!

 SQL statement issue


Change Page: 12 > | Showing page 1 of 2, messages 1 to 20 of 26
Author Message
Tony Barnes

  • Total Posts : 17383
  • Reward points : 6637
  • Joined: 07/10/2004
  • Location: Leeds
  • Status: offline
  • Read my TROG
SQL statement issue 31 January 2012 06:50 (permalink)
On the off chance anyone on here is good at pulling apart SQL queries, I can't get this one to work:
 
SELECT p.*,
FLOOR(prodratingtotal / prodnumratings) AS prodavgrating, pi.*,
SUBSTRING_INDEX(SUBSTRING_INDEX(pi.imagefilestdsize, 'x', 2), ' ', 1) / SUBSTRING_INDEX(SUBSTRING_INDEX(pi.imagefilestdsize, 'x', -1), ' ', 2) as ratio,
(SELECT orderid, op.ordprodname, op.ordprodid, SUM(op.ordprodqty) AS qty FROM isc_orders o JOIN isc_order_products op on op.orderprodid=o.orderid WHERE orddate BETWEEN 1271853079 AND 1272637707 GROUP BY op.ordprodid) AS qty,
(SELECT discountpercent FROM isc_customer_group_discounts disc WHERE disc.discounttype='PRODUCT' AND disc.customergroupid='9' AND disc.catorprodid=p.productid) AS prodgroupdiscount,
(SELECT discountmethod FROM isc_customer_group_discounts disc WHERE disc.discounttype='PRODUCT' AND disc.customergroupid='9' AND disc.catorprodid=p.productid) AS discountmethod
FROM (SELECT DISTINCT ca.productid, FLOOR(prodratingtotal / prodnumratings) AS prodavgrating
FROM isc_categoryassociations ca
INNER JOIN isc_products p ON p.productid = ca.productid WHERE p.prodvisible = 1 AND p.prodbrandid = 51) AS ca
INNER JOIN isc_products p ON p.productid = ca.productid
LEFT JOIN isc_product_images pi ON (pi.imageisthumb = 1 AND p.productid = pi.imageprodid)
ORDER BY p.prodcode ASC, p.prodcode ASC
 
I know that it works fine like this:
SELECT p.*,
FLOOR(prodratingtotal / prodnumratings) AS prodavgrating, pi.*,
SUBSTRING_INDEX(SUBSTRING_INDEX(pi.imagefilestdsize, 'x', 2), ' ', 1) / SUBSTRING_INDEX(SUBSTRING_INDEX(pi.imagefilestdsize, 'x', -1), ' ', 2) as ratio,
(SELECT discountpercent FROM isc_customer_group_discounts disc WHERE disc.discounttype='PRODUCT' AND disc.customergroupid='9' AND disc.catorprodid=p.productid) AS prodgroupdiscount,
(SELECT discountmethod FROM isc_customer_group_discounts disc WHERE disc.discounttype='PRODUCT' AND disc.customergroupid='9' AND disc.catorprodid=p.productid) AS discountmethod
FROM
(SELECT DISTINCT ca.productid, FLOOR(prodratingtotal / prodnumratings) AS prodavgrating
FROM
isc_categoryassociations ca
INNER JOIN isc_products p ON p.productid = ca.productid WHERE p.prodvisible = 1 AND p.prodbrandid = 51) AS ca
INNER JOIN isc_products p ON p.productid = ca.productid
LEFT JOIN isc_product_images pi ON (pi.imageisthumb = 1 AND p.productid = pi.imageprodid)
ORDER BY p.prodcode ASC, p.prodcode ASC
 
So I'm basically trying to get this line in there:
(SELECT orderid, op.ordprodname, op.ordprodid, SUM(op.ordprodqty) AS qty FROM isc_orders o JOIN isc_order_products op on op.orderprodid=o.orderid WHERE orddate BETWEEN 1271853079 AND 1272637707 GROUP BY op.ordprodid) AS qty,
 
It keeps throwing

Error Code: 1241. Operand should contain 1 column(s)

 
Baffled...
 
Any ideas?
 
#1
    Bodybuilding Warehouse Bodybuilding Warehouse - The best bodybuilding supplements from the best brands, Boditronics, CNP Professional, Nutrisport & more all with free delivery!
    essex_chris

    • Total Posts : 17249
    • Reward points : 6227
    • Joined: 21/08/2005
    • Location: Essex
    • Status: offline
    • Read my TROG
    Re:SQL statement issue 31 January 2012 09:13 (permalink)
    Above my paygrade. Sure there was a member on here who was a dab hand with SQL. 
    Awesome pic, but Tony you're not doing yourself many favours posting up tips on preventing the gag reflex and then a picture of a guy touching his toes - Ak
     
    #2
      Tony Barnes

      • Total Posts : 17383
      • Reward points : 6637
      • Joined: 07/10/2004
      • Location: Leeds
      • Status: offline
      • Read my TROG
      Re:SQL statement issue 31 January 2012 09:18 (permalink)
      Yeah, I'm a bit lost in it - don't really do much SQL, maybe an hour a month, so when I get stuck it doesn't half piss me off!!!
       
      #3
        essex_chris

        • Total Posts : 17249
        • Reward points : 6227
        • Joined: 21/08/2005
        • Location: Essex
        • Status: offline
        • Read my TROG
        Re:SQL statement issue 31 January 2012 09:33 (permalink)
        I googled that error, helped massively
        Awesome pic, but Tony you're not doing yourself many favours posting up tips on preventing the gag reflex and then a picture of a guy touching his toes - Ak
         
        #4
          RK86

          • Total Posts : 329
          • Reward points : 3285
          • Joined: 03/06/2008
          • Status: offline
          • Read my TROG
          Re:SQL statement issue 31 January 2012 09:37 (permalink)
          Have you tried putting the join on a new line rather than straight after 'AS qty FROM isc_orders o'
           
          #5
            RK86

            • Total Posts : 329
            • Reward points : 3285
            • Joined: 03/06/2008
            • Status: offline
            • Read my TROG
            Re:SQL statement issue 31 January 2012 09:50 (permalink)
            What about the group by outside the parentheses?
             
            #6
              Tony Barnes

              • Total Posts : 17383
              • Reward points : 6637
              • Joined: 07/10/2004
              • Location: Leeds
              • Status: offline
              • Read my TROG
              Re:SQL statement issue 31 January 2012 10:05 (permalink)
              No disco - don't think that alters the sytnax anyway?

              Spotted I've got 'AS qty' in there twice, but that isn't the cause either
               
              #7
                Tony Barnes

                • Total Posts : 17383
                • Reward points : 6637
                • Joined: 07/10/2004
                • Location: Leeds
                • Status: offline
                • Read my TROG
                Re:SQL statement issue 31 January 2012 10:06 (permalink)
                Nope, not the group by either
                 
                #8
                  gwilym

                  • Total Posts : 3126
                  • Reward points : 6234
                  • Joined: 25/10/2004
                  • Status: offline
                  • Read my TROG
                  Re:SQL statement issue 31 January 2012 10:21 (permalink)
                  Doesn't it tell you which line the error is occurring on?

                  Official MaxiRAW Board Representative 

                   

                     High-quality, tested performance nutrition.
                   Quality assurance & value guaranteed...
                  www.maxiraw.com  

                  Twitter: @MaxiRAW     Tag: #Rawdedication

                   
                  #9
                    Tony Barnes

                    • Total Posts : 17383
                    • Reward points : 6637
                    • Joined: 07/10/2004
                    • Location: Leeds
                    • Status: offline
                    • Read my TROG
                    Re:SQL statement issue 31 January 2012 10:27 (permalink)
                    Nope, fails whole function
                     
                    #10
                      gwilym

                      • Total Posts : 3126
                      • Reward points : 6234
                      • Joined: 25/10/2004
                      • Status: offline
                      • Read my TROG
                      Re:SQL statement issue 31 January 2012 10:39 (permalink)
                      Have you tried just querying the part
                       
                      (SELECT orderid, op.ordprodname, op.ordprodid, SUM(op.ordprodqty) AS qty FROM isc_orders o JOIN isc_order_products op on op.orderprodid=o.orderid WHERE orddate BETWEEN 1271853079 AND 1272637707 GROUP BY op.ordprodid) AS qty
                       
                      on its own?
                       
                       

                      Official MaxiRAW Board Representative 

                       

                         High-quality, tested performance nutrition.
                       Quality assurance & value guaranteed...
                      www.maxiraw.com  

                      Twitter: @MaxiRAW     Tag: #Rawdedication

                       
                      #11
                        Tony Barnes

                        • Total Posts : 17383
                        • Reward points : 6637
                        • Joined: 07/10/2004
                        • Location: Leeds
                        • Status: offline
                        • Read my TROG
                        Re:SQL statement issue 31 January 2012 10:49 (permalink)
                        Yep - though ditching the "AS qty" bit, so the 2 working queries are:
                         

                        SELECT p.*,
                        FLOOR(prodratingtotal / prodnumratings) AS prodavgrating, pi.*,
                        SUBSTRING_INDEX(SUBSTRING_INDEX(pi.imagefilestdsize, 'x', 2), ' ', 1) / SUBSTRING_INDEX(SUBSTRING_INDEX(pi.imagefilestdsize, 'x', -1), ' ', 2) as ratio,
                        (SELECT discountpercent FROM isc_customer_group_discounts disc WHERE disc.discounttype='PRODUCT' AND disc.customergroupid='9' AND disc.catorprodid=p.productid) AS prodgroupdiscount,
                        (SELECT discountmethod FROM isc_customer_group_discounts disc WHERE disc.discounttype='PRODUCT' AND disc.customergroupid='9' AND disc.catorprodid=p.productid) AS discountmethod
                        FROM (SELECT DISTINCT ca.productid, FLOOR(prodratingtotal / prodnumratings) AS prodavgrating
                        FROM isc_categoryassociations ca
                        INNER JOIN isc_products p ON p.productid = ca.productid WHERE p.prodvisible = 1) AS ca
                        INNER JOIN isc_products p ON p.productid = ca.productid
                        LEFT JOIN isc_product_images pi ON (pi.imageisthumb = 1 AND p.productid = pi.imageprodid)
                        ORDER BY p.prodcode ASC, p.prodcode ASC

                         
                        and
                         

                        SELECT orderid, op.ordprodname, op.ordprodid, SUM(op.ordprodqty) AS qty
                        FROM isc_orders o
                        JOIN isc_order_products op on op.orderprodid=o.orderid
                        WHERE orddate
                        BETWEEN 1271853079 AND 1272637707
                        GROUP BY op.ordprodid

                         
                         
                         
                        #12
                          Tony Barnes

                          • Total Posts : 17383
                          • Reward points : 6637
                          • Joined: 07/10/2004
                          • Location: Leeds
                          • Status: offline
                          • Read my TROG
                          Re:SQL statement issue 31 January 2012 10:52 (permalink)
                          The 2 queries need to be joined on op.ordprodid = p.productid
                           
                          #13
                            gwilym

                            • Total Posts : 3126
                            • Reward points : 6234
                            • Joined: 25/10/2004
                            • Status: offline
                            • Read my TROG
                            Re:SQL statement issue 31 January 2012 11:00 (permalink)
                            I assume you've saved both the queries individually.
                             
                            Might sound stupid, its hard to picture without the management studio in front of me, and I havent used it for the past year, but have you tried creating a third query, joining the 2 successful queries?
                             
                            Its not ideal, but I used to do this quite a few times as a work around when trying to get various management info.

                            Official MaxiRAW Board Representative 

                             

                               High-quality, tested performance nutrition.
                             Quality assurance & value guaranteed...
                            www.maxiraw.com  

                            Twitter: @MaxiRAW     Tag: #Rawdedication

                             
                            #14
                              EnfantTerrible

                              • Total Posts : 1405
                              • Reward points : 1391
                              • Joined: 13/01/2011
                              • Location: Southampton
                              • Status: offline
                              • Read my TROG
                              Re:SQL statement issue 31 January 2012 11:05 (permalink)
                              It's because you're trying to return multiple columns in a subquery (which you can't do unless it's for row comparision) and that's why it works on its own. Try gwilym's suggestion!

                              Disclaimer: Not used SQL for over a year now since changing jobs so I may be wrong!
                              <message edited by EnfantTerrible on 31 January 2012 11:08>
                              Journal
                              Squat: 200kg
                              Deadlift: 250kg
                              Bench: 150kg 
                               
                               
                              #15
                                Tony Barnes

                                • Total Posts : 17383
                                • Reward points : 6637
                                • Joined: 07/10/2004
                                • Location: Leeds
                                • Status: offline
                                • Read my TROG
                                Re:SQL statement issue 31 January 2012 11:15 (permalink)
                                Management Studio...? Erm, what's that then? I've only ever hand written SQL statements
                                 
                                Will see if I can fudge that together
                                 
                                #16
                                  EnfantTerrible

                                  • Total Posts : 1405
                                  • Reward points : 1391
                                  • Joined: 13/01/2011
                                  • Location: Southampton
                                  • Status: offline
                                  • Read my TROG
                                  Re:SQL statement issue 31 January 2012 11:18 (permalink)
                                  Are you using iSeries or something?
                                  Journal
                                  Squat: 200kg
                                  Deadlift: 250kg
                                  Bench: 150kg 
                                   
                                   
                                  #17
                                    gwilym

                                    • Total Posts : 3126
                                    • Reward points : 6234
                                    • Joined: 25/10/2004
                                    • Status: offline
                                    • Read my TROG
                                    Re:SQL statement issue 31 January 2012 11:20 (permalink)
                                    SQL Server Management Studio.
                                     
                                    I'm guessing you're using mySQL then?

                                    Official MaxiRAW Board Representative 

                                     

                                       High-quality, tested performance nutrition.
                                     Quality assurance & value guaranteed...
                                    www.maxiraw.com  

                                    Twitter: @MaxiRAW     Tag: #Rawdedication

                                     
                                    #18
                                      gwilym

                                      • Total Posts : 3126
                                      • Reward points : 6234
                                      • Joined: 25/10/2004
                                      • Status: offline
                                      • Read my TROG
                                      Re:SQL statement issue 31 January 2012 11:22 (permalink)
                                      I haven't used mySQL so not sure how to but..
                                       
                                      You need to find a way to create permanent objects out of the 2 individual queries. 
                                       
                                      When you've created the 2 objects, then join them in the manner you want using a simple query.

                                      Official MaxiRAW Board Representative 

                                       

                                         High-quality, tested performance nutrition.
                                       Quality assurance & value guaranteed...
                                      www.maxiraw.com  

                                      Twitter: @MaxiRAW     Tag: #Rawdedication

                                       
                                      #19
                                        Tony Barnes

                                        • Total Posts : 17383
                                        • Reward points : 6637
                                        • Joined: 07/10/2004
                                        • Location: Leeds
                                        • Status: offline
                                        • Read my TROG
                                        Re:SQL statement issue 31 January 2012 11:33 (permalink)
                                        Yeah, MySQL Workbench - the SQL statements are run in PHP scripts, so I can't have anything saved anywhere, needs to be all shoved together.
                                         
                                        #20
                                          Share/Bookmark
                                          Change Page: 12 > | Showing page 1 of 2, messages 1 to 20 of 26


                                          James' Super Smoothie
                                          Jump to:

                                          Current active users

                                          There are 0 members and 2 guests.

                                          Icon Legend and Permission

                                          • New Messages
                                          • No New Messages
                                          • Hot Topic w/ New Messages
                                          • Hot Topic w/o New Messages
                                          • Locked w/ New Messages
                                          • Locked w/o New Messages
                                          • Read Message
                                          • Post New Thread
                                          • Reply to message
                                          • Post New Poll
                                          • Submit Vote
                                          • Post reward post
                                          • Delete my own posts
                                          • Delete my own threads
                                          • Rate post

                                          2000-2012 ASPPlayground.NET Forum Version 3.9
                                          Go To Top Of Page