Исключение нулевых значений из таблицы результатов базы данных с использованием MySQL

Я выполняю результат запроса, в котором нет нулевых значений. Ниже мой query

SELECT
  sur.id AS 'Survey ID',
  DATE (sur.capture_date) AS 'Date',
  sur.sub_division AS 'Sub-Div',
  sd.name AS 'Sub-Div Name',
  sd.`circle_name` AS 'Circle Name',
  sur.ref_no AS 'Reference Number',
  sur.customer_id AS 'Customer ID',
  sur.consumer_name AS 'Consumer Name & Address',
  sur.sync_date AS 'Sync Date',
  sur.capture_date AS 'Capture Date',
  sur.latitude AS Latitude,
  sur.longitude AS Longitude,
  sur.near_by_place AS Comments,
  sur.tarrif AS Tariff,
  sur.s_load AS 'Sanctioned Load',
  sur.meter_type AS 'Meter Type (IT)',
  sur.meter_type_field AS 'Meter Type (Field)',
  sur.meter_type_desired AS 'Meter Type (As Desired By SDO)',
  sur.pole_type AS 'Pole Type',
  sur.meter_location AS 'Meter Location',
  sur.est_cable_len AS 'Estimated Cable Length',
  sur.meter_status AS 'Meter Status',
  sur.running_load_entry_a AS 'Running Load Entry (A)',
  sur.running_load_entry_b AS 'Running Load Entry (B)',
  sur.running_load_entry_c AS 'Running Load Entry (C)',
  MAX(CASE WHEN sn.operator_name LIKE '%Zong%' THEN sn.signal_strength END) AS Zong,
  MAX(CASE WHEN sn.operator_name LIKE '%Mobilink%' THEN sn.signal_strength END) AS Mobilink
FROM survey sur
INNER JOIN
  survey_networks sn ON sur.id = sn.survey_id 
INNER JOIN
  survey_hesco_subdivision sd ON sur.sub_division = sd.sub_div_code
WHERE
  sn.`signal_strength` = 'No Signal' AND
  sn.`operator_name` IN ('Zong','Mobilink') AND
  sn.`signal_strength` IS NOT NULL
GROUP BY
  sur.id,
  sur.sub_division,
  sur.ref_no,
  sur.customer_id,
  sur.consumer_name,
  sur.consumer_address ,
  sur.sync_date,
  sur.capture_date,
  sur.source,
  sur.latitude,
  sur.longitude,
  sur.near_by_place,
  sur.tarrif,
  sur.s_load,
  sur.meter_type,
  sur.meter_type_field,
  sur.meter_type_desired,
  sur.pole_type,
  sur.meter_location,
  sur.est_cable_len,
  sur.meter_status,
  sur.running_load_entry_a,
  sur.running_load_entry_b,
  sur.running_load_entry_c 
ORDER BY
  sd.`name` ASC;

Как показано в запросе, который я добавил IS NOT NULL но все же я получаю нулевые значения, как показано на рисунке ниже

Любая помощь будет высоко оценен

1 ответ

Вы получаете результаты таким образом, потому что есть много столбцов, по которым вы группируете, попробуйте сделать max и отфильтруйте эти два столбца в подзапросе следующим образом:

SELECT sur.id AS 'Survey ID'
    ,DATE (sur.capture_date) AS 'Date'
    ,sur.sub_division AS 'Sub-Div'
    ,sd.NAME AS 'Sub-Div Name'
    ,sd.`circle_name` AS 'Circle Name'
    ,sur.ref_no AS 'Reference Number'
    ,sur.customer_id AS 'Customer ID'
    ,sur.consumer_name AS 'Consumer Name & Address'
    ,sur.sync_date AS 'Sync Date'
    ,sur.capture_date AS 'Capture Date'
    ,sur.latitude AS Latitude
    ,sur.longitude AS Longitude
    ,sur.near_by_place AS Comments
    ,sur.tarrif AS Tariff
    ,sur.s_load AS 'Sanctioned Load'
    ,sur.meter_type AS 'Meter Type (IT)'
    ,sur.meter_type_field AS 'Meter Type (Field)'
    ,sur.meter_type_desired AS 'Meter Type (As Desired By SDO)'
    ,sur.pole_type AS 'Pole Type'
    ,sur.meter_location AS 'Meter Location'
    ,sur.est_cable_len AS 'Estimated Cable Length'
    ,sur.meter_status AS 'Meter Status'
    ,sur.running_load_entry_a AS 'Running Load Entry (A)'
    ,sur.running_load_entry_b AS 'Running Load Entry (B)'
    ,sur.running_load_entry_c AS 'Running Load Entry (C)'
    ,MAX(sn.Zong) AS sn.Zong
    ,MAX(sn.Mobilink) AS sn.Mobilink
FROM survey sur
INNER JOIN (
    SELECT MAX(CASE 
                WHEN sn.operator_name LIKE '%Zong%'
                    THEN sn.signal_strength
                END) AS Zong
        ,MAX(CASE 
                WHEN sn.operator_name LIKE '%Mobilink%'
                    THEN sn.signal_strength
                END) AS Mobilink
    FROM survey_networks AS sn
    WHERE sn.`signal_strength` = 'No Signal'
        AND sn.`operator_name` IN (
            'Zong'
            ,'Mobilink'
            )
        AND sn.`signal_strength` IS NOT NULL
    ) AS sn ON sur.id = sn.survey_id
INNER JOIN survey_hesco_subdivision sd ON sur.sub_division = sd.sub_div_code
GROUP BY sur.id
    ,sur.sub_division
    ,sur.ref_no
    ,sur.customer_id
    ,sur.consumer_name
    ,sur.consumer_address
    ,sur.sync_date
    ,sur.capture_date
    ,sur.source
    ,sur.latitude
    ,sur.longitude
    ,sur.near_by_place
    ,sur.tarrif
    ,sur.s_load
    ,sur.meter_type
    ,sur.meter_type_field
    ,sur.meter_type_desired
    ,sur.pole_type
    ,sur.meter_location
    ,sur.est_cable_len
    ,sur.meter_status
    ,sur.running_load_entry_a
    ,sur.running_load_entry_b
    ,sur.running_load_entry_c
ORDER BY sd.`name` ASC
Другие вопросы по тегам