0

I have 2 queries, that are almost exactly the same.

Query #1:

SELECT 
    cman_code, cman_name 
FROM
    capder 
JOIN 
    caprange ON capder.cder_rancode = caprange.cran_code 
JOIN 
    capman ON caprange.cran_mantextcode = capman.cman_code 
JOIN 
    capmod ON capder.cder_modcode = capmod.cmod_code 
JOIN 
    nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID 
JOIN 
    nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode 
JOIN 
    nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode 
JOIN 
    nvdmodelyear ON capder.cder_ID = MY_Id
                    AND nvddictionarytechnical.DT_TechCode = 67 
                    AND nvdtechnical.TECH_Value_Float < '100' 
                    AND nvdtechnical.TECH_Value_Float != 0      
                    AND cder_transmission = 'M' 
                    AND cman_code IN ('1','140','164','836','869','1283','1333','1925','2193','2514','4256','4537','4721','4835','5088','5171','5182','5238','5261','5509','6058','6425','7104','7983','8083','8219','9909','10172','10251','10383','10519','10968','12243','12764','14786','15155','21223','22439','44394','47841') 
                    AND (cmod_discontinued=0 OR cmod_discontinued=2015) 
                    AND cder_discontinued = '0000-00-00 00:00:00' 
GROUP BY 
    cman_code 
ORDER BY 
    cman_name DESC

Query #2:

SELECT 
    cman_code, cman_name 
FROM 
    capder 
JOIN 
    caprange ON capder.cder_rancode = caprange.cran_code 
JOIN 
    capman ON caprange.cran_mantextcode = capman.cman_code 
JOIN 
    capmod ON capder.cder_modcode = capmod.cmod_code 
JOIN 
    nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID 
JOIN 
    nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode 
JOIN 
    nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode 
JOIN 
    nvdmodelyear ON capder.cder_ID = MY_Id
WHERE 
    nvddictionarytechnical.DT_TechCode = 11 
    AND nvdtechnical.TECH_Value_Float < 25  
    AND nvdtechnical.TECH_Value_Float != 0
    AND cder_transmission = 'M' 
    AND cman_code IN ('1','140','164','836','869','1283','1333','1925','2193','2514','4256','4537','4721','4835','5088','5171','5182','5238','5261','5509','6058','6425','7104','7983','8083','8219','9909','10172','10251','10383','10519','10968','12243','12764','14786','15155','21223','22439','44394','47841') 
    AND (cmod_discontinued=0 OR cmod_discontinued=2015) 
    AND cder_discontinued = '0000-00-00 00:00:00' 
GROUP BY 
    cman_code 
ORDER BY 
    cman_name DESC

Both queries work perfectly and produce the exact results what I want.

The only difference between the two is the values in nvddictionarytechnical.**DT_TechCode** and nvdtechnical.**TECH_Value_Float**

I am having trouble bringing them together.

The - TECH_Value_Float - column, contains both CO2 and MPG values.

I just need to merge these into 1 query.

Using this does not work:

where 
(
   (
     nvddictionarytechnical.DT_TechCode = 67 
     AND nvdtechnical.TECH_Value_Float < '100'
   ) 
   or 
   (
     nvddictionarytechnical.DT_TechCode = 11 
     AND nvdtechnical.TECH_Value_Float < 25
   )
 ) 

The where and or produce results like:

|CO2 | MPG|
-----------
 94  | 78 |
 378 | 19 |

I need for the MPG and CO2 to both be true. And results like:

|CO2 | MPG|
-----------
 15  | 18 |
 10  | 17 |

Any advice from the pros? Should I just make 2 queries? Has anyone ever come across a problem like this? Is this very unusual?

Thanks for your help.

0

2 Answers 2

3

Have you tried:

where 
(
   (
     nvddictionarytechnical.DT_TechCode = 67 
     AND nvdtechnical.TECH_Value_Float < '100'
   ) 
   or 
   (
     nvddictionarytechnical.DT_TechCode = 11 
     AND nvdtechnical.TECH_Value_Float < 25
   )
 ) 
3
  • Beat me to it by the click of a submit button. You win... :-) Apr 1, 2015 at 19:14
  • I have tried this thank you. It does not work. Both need to be true for the row to qualify. I need to say - give me the MAKE that has x MPG value and also x CO2 value. They both work perfect separate - just cant figure how to do them together. I think I need to re merge tables again?? Not sure.. or maybe 2 queries ???
    – user4386053
    Apr 2, 2015 at 19:19
  • The where and or are producing results like MPG 78 and CO2 94 OR MPG 19 and CO2 378. It cant be either - it needs to be both true
    – user4386053
    Apr 2, 2015 at 19:24
0

What about this?

SELECT cman_code, cman_name 

from capder 

JOIN caprange ON capder.cder_rancode = caprange.cran_code 
JOIN capman ON caprange.cran_mantextcode = capman.cman_code 
JOIN capmod ON capder.cder_modcode = capmod.cmod_code 
JOIN nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID 
JOIN nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode 
JOIN nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode 
JOIN nvdmodelyear ON capder.cder_ID = MY_Id

AND ((nvddictionarytechnical.DT_TechCode = 67 
       AND nvdtechnical.TECH_Value_Float < '100') 
   OR (nvddictionarytechnical.DT_TechCode = 11 
       AND nvdtechnical.TECH_Value_Float < 25 ))

AND nvdtechnical.TECH_Value_Float != 0      

AND cder_transmission = 'M' 
AND cman_code IN ('1','140','164','836','869','1283','1333','1925','2193','2514','4256','4537','4721','4835','5088','5171','5182','5238','5261','5509','6058','6425','7104','7983','8083','8219','9909','10172','10251','10383','10519','10968','12243','12764','14786','15155','21223','22439','44394','47841') 

AND (cmod_discontinued=0 OR cmod_discontinued=2015) 
AND cder_discontinued = '0000-00-00 00:00:00' 
GROUP BY cman_code ORDER BY cman_name DESC

That way you check for both of the conditions!

Your Answer

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