shantanuo
shantanuo

Reputation: 32316

Refactor of SQL Statement

The following is working as expected. It shows the default CPM and CPC for the country if it is missing from the original table. I am using a temporary table and I will like to know if it can be done without using temp table.

mysql> create temporary table country_list (country_name varchar(100));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into country_list values ('IN'), ('SA');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

select dt.country_name as country, dt.operator,
if (dt.cpm is null, (SELECT cpm FROM ox_bidding_configuration where country = '' and operator = ''), dt.cpm) as updated_cpm,
if (dt.cpc is null, (SELECT cpc FROM ox_bidding_configuration where country = '' and operator = ''), dt.cpc) as updated_cpc,
if (dt.cpa is null, (SELECT cpa FROM ox_bidding_configuration where country = '' and operator = ''), dt.cpa) as updated_cpa
from (
select a.country_name, b.operator, cpm, cpc, cpa from country_list as a left join ox_bidding_configuration as b on a.country_name = b.country group by b.country, b.operator, cpm, cpc, cpa) as dt;
+---------+----------+-------------+-------------+-------------+
| country | operator | updated_cpm | updated_cpc | updated_cpa |
+---------+----------+-------------+-------------+-------------+
| SA      | NULL     |      1.0000 |      1.0000 |      1.0000 |
| IN      |          |      2.0000 |      2.0000 |      2.0000 |
| IN      | abcd     |     11.0000 |     23.0000 |      4.0000 |
+---------+----------+-------------+-------------+-------------+

The country SA is not in the primary table. So I can not use self left joins. I will like to know if there is a better way.

Upvotes: 0

Views: 84

Answers (1)

deroby
deroby

Reputation: 6002

For starters, from where I'm standing you're using the (temporary) country_list table as the PRIMARY table and match that against the ox_bidding_configuration where possible (left outer join); hence your result-set only contains the countries SA & IN.

Although I'm not 100% sure this syntax works on MYSQL, being used to TSQL I would write your query as :

SELECT dt.country_name as country, 
       dt.operator,
       COALESCE(dt.cpm, def.cpm) as updated_cpm,
       COALESCE(dt.cpc, def.cpc) as updated_cpd,
       COALESCE(dt.cpa, def.cpa) as updated_cpa
  FROM (SELECT DISTINCT a.country_name, 
                        b.operator, 
                        b.cpm, 
                        b.cpc, 
                        b.cpa 
          FROM country_list a
          LEFT OUTER JOIN ox_bidding_configuration as b
                       ON b.country = a.country_name ) dt
  LEFT OUTER JOIN ox_bidding_configuration def
               ON def.country = '' 
              AND def.operator = ''

IMHO this is more clear, and probably it's slightly faster too as the default config only needs to be fetched once.

To get rid of the country_list table, you could convert it into an in-line query, but frankly I don't see the benefit of it.

Something along the lines of below :

SELECT dt.country_name as country, 
       dt.operator,
       COALESCE(dt.cpm, def.cpm) as updated_cpm,
       COALESCE(dt.cpc, def.cpc) as updated_cpd,
       COALESCE(dt.cpa, def.cpa) as updated_cpa
  FROM (SELECT DISTINCT a.country_name, 
                        b.operator, 
                        b.cpm, 
                        b.cpc, 
                        b.cpa 
          FROM (SELECT 'SA'  AS country_name
                 UNION ALL 
                SELECT 'IN') a
          LEFT OUTER JOIN ox_bidding_configuration as b
                       ON b.country = a.country_name ) dt
  LEFT OUTER JOIN ox_bidding_configuration def
               ON def.country = '' 
              AND def.operator = ''

Upvotes: 2

Related Questions