1

I’m not a very experienced programmer so please bear with me! I have to extract data from a table (Chemie) based on data from another table (ProdDesigns). The obvious answer would be to use sub queries. Something like this:

SELECT chemie.rmcode, 
       chemie.sio2, 
       chemie.al2o3, 
       chemie.fe2o3, 
       chemie.feo, 
       chemie.tio2, 
       chemie.cao, 
       chemie.mgo, 
       chemie.na2o, 
       chemie.k2o, 
       chemie.cr2o3, 
       chemie.p2o5, 
       chemie.sic, 
       chemie.c, 
       chemie.mno, 
       chemie.zro2, 
       chemie.b2o3 
FROM   chemie 
       INNER JOIN proddesigns 
               ON chemie.rmcode = proddesigns.rmcode 
WHERE  chemie.rmcode = (SELECT proddesigns.rmcode 
                        FROM   proddesigns 
                        WHERE  proddesigns.batchno = 1234) 

The problem is that the subset of data from table ProdDesigns will ALWAYS be multiple records, so I get the error “At most one record can be returned by this sub query”.So I’m thinking of using temporary tables and attempted the following:

conn.Open();
OleDbCommand cmdRM = null;
cmdRM = conn.CreateCommand();
cmdRM.CommandText = (@"SELECT * INTO ##tempTest FROM ProdDesigns WHERE batchNo = 1234");
cmdRM.ExecuteNonQuery();
dataAdapter2 = new OleDbDataAdapter(@"SELECT Chemie.RMCode, Chemie.SiO2, Chemie.Al2O3, Chemie.Fe2O3, Chemie.FeO, Chemie.TiO2, Chemie.CaO, Chemie.MgO, Chemie.Na2O, Chemie.K2O, Chemie.Cr2O3, Chemie.P2O5, Chemie.SiC, Chemie.C, Chemie.MnO, Chemie.ZrO2, Chemie.B2O3 
        FROM Chemie INNER JOIN ##tempTest ON Chemie.RMCode = ##tempTest.rmCode
        WHERE ((Chemie.RMCode)= ##tempTest.rmCode)", conn);
dataAdapter2.Fill(dataS2, "Chemie");
dtChem = dataS2.Tables["Chemie"];

Any feedback would be greatly appreciated!!

3
  • Good formatting is always your friend.. Commented Dec 11, 2013 at 12:08
  • This is my first post too so I do apologize for poor formatting, indentation, etc. Commented Dec 11, 2013 at 12:11
  • Please don't apologize. We all passed at this way.. You can read FAQ and How to Ask as a start ;) Commented Dec 11, 2013 at 12:15

5 Answers 5

5

I think you could just do

Version1

FROM   chemie 
INNER JOIN proddesigns 
           ON chemie.rmcode = proddesigns.rmcode and proddesigns.batchno = 1234

Version2 (result won't be different with an INNER JOIN)

FROM   chemie 
    INNER JOIN proddesigns 
               ON chemie.rmcode = proddesigns.rmcode 
WHERE proddesigns.batchno = 1234

Version 3

SELECT c.rmcode, 
       --xxx
FROM   chemie c
where exists (select null from proddesigns p
              where c.rmcode = p.rmcode
              and p.batchno = 1234)

Which one should you choose ?

Well, that's your choice. I would take (in order of preference) 3, 1, 2, as you don't really "need a join".

You just wanna take values from chemie where something is related in proddesigns.

And the exists clause is the clearer for me. But that's just a point of view.

But if you need datas from proddesigns, then use a join.

By the way, the versions with the JOIN could retrieve more rows than what you need. That shouldn't happen with your datas, but would happen if you had two proddesigns with same rmcode and batchno=1234.

Sign up to request clarification or add additional context in comments.

3 Comments

Didn't know you could JOIN on more than one condition. Learn something new everyday :)
@Nunners Well it's not a need with an INNER JOIN (you can put this in the where clause). It's often more usefull with LEFT JOIN.
@BabyDoll, I would suggest the option with the JOIN vs EXISTS.. Historically, I've found that IN, NOT IN, EXISTS to a subquery is generally SLOWER than a JOIN based on a qualifying index -- (rmcode, batchno)
0

no need to use the subquery, append and proddesigns.batchno = 1234 after ON chemie.rmcode = proddesigns.rmcode and good to go

Comments

0

Change

WHERE  chemie.rmcode = (SELECT proddesigns.rmcode 
                    FROM   proddesigns 
                    WHERE  proddesigns.batchno = 1234) 

to

WHERE  chemie.rmcode in (SELECT proddesigns.rmcode 
                    FROM   proddesigns 
                    WHERE  proddesigns.batchno = 1234) 

Comments

0

You can optimize your query changing:

WHERE  chemie.rmcode = (SELECT proddesigns.rmcode 
                        FROM   proddesigns 
                        WHERE  proddesigns.batchno = 1234)

to

 WHERE  proddesigns.batchno = 1234

I think you'll notice soon the difference in performance, without using a subquery.

An alternative would be to put this condition in the INNER JOIN statement:

INNER JOIN proddesigns ON chemie.rmcode = proddesigns.rmcode and proddesigns.batchno = 1234

Comments

0

You could simply add a TOP 1 to your subquery

WHERE  chemie.rmcode = (SELECT TOP 1 proddesigns.rmcode 
                        FROM   proddesigns 
                        WHERE  proddesigns.batchno = 1234) 

This makes sure only one record is returned.


I know you got the solution already, but if you wanted one result for each result of the subquery, you could also have used IN:

WHERE  chemie.rmcode IN (SELECT proddesigns.rmcode 
                        FROM   proddesigns 
                        WHERE  proddesigns.batchno = 1234) 

3 Comments

a little bit overcomplicated to use a subquery, no ?
Thing is I do need more than one record returned, but I got the solution. Thanks for your feedback, I'm sure this tip will come in handy in future!
@RaphaëlAlthaus Of course, but my solution fixes the error and he had the subquery already. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.