Skip to content
Advertisement

Show the manufacturers that sold cellphone in 2010 but didn’t in 2009. (USING MYSQL)

SELECT Manufacturer_Name
FROM DIM_MANUFACTURER
     INNER JOIN DIM_MODEL ON DIM_MANUFACTURER.IDManufacturer = DIM_MODEL.IDManufacturer
    WHERE DIM_MANUFACTURER.IDManufacturer IN
          (
          SELECT DIM_MODEL.IDManufacturer
            FROM DIM_MODEL
            INNER JOIN FACT_TRANSACTIONS ON DIM_MODEL.IDModel = FACT_TRANSACTIONS.IDModel
             WHERE DIM_MODEL.IDMODEL IN 
             (
             SELECT FACT_TRANSACTIONS.IDModel
              FROM FACT_TRANSACTIONS
             WHERE IDModel NOT IN (YEAR(STR_TO_DATE(Date_d,'%m/%d/%Y')) = 2009) AND 
                IDModel In (YEAR(STR_TO_DATE(Date_d,'%m/%d/%Y')) = 2010)));

Advertisement

Answer

Hmmm . . . I am thinking exists and not exists:

select m.*
from dim_manufacturer m
where exists (select 1
              from transactions t join
                   dim_models mo
                   using (idmodel)
              where m.IDManufacturer = mo.IDManufacturer and
                    t.date_d >= '2010-01-01' and t.date_d < '2011-01-01'
             ) and
      not exists (select 1
                  from transactions t join
                       dim_models mo
                       using (idmodel)
                  where m.IDManufacturer = mo.IDManufacturer and
                        t.date_d >= '2009-01-01' and t.date_d < '2010-01-01'
                 ) ;
   
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement