'How to write SQL if row exists in both ways

looking to filter a table for Facebook Ads.

Currently I have a naming convention for campaigns either containing the string 'test' or does not contain the string 'test' in the campaign name.

How can I manipulate the filters rows that exist in both testing campaigns and non-testing campaigns and only ones that exist in both?

Example query for only 'test' campaigns

SELECT `ads` 
FROM `facebook_ads`
WHERE `campaign_name` LIKE '%test%'


Solution 1:[1]

I found the solution for my problem, and I would like to leave it here for ref. ( found the solution here )

the idea is to specify multiple aggregations inside the aggs object that's all

GET auto/_search
{
  "size": 0,
  "aggs": {

    // aggregate on types
    "types": {
      "terms": {
        "field": "Type",
        "size": 10
      }
    },

    // aggregate on makes
    "makes": {
      "terms": {
        "field": "Make",
        "size": 10
      }
    },

    // aggregate on body types
    "body type": {
      "terms": {
        "field": "Body Type",
        "size": 10
      }
    }
  }
}

the result was as expected

{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "types" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Used",
          "doc_count" : 22414
        },
        {
          "key" : "New",
          "doc_count" : 3021
        },
        {
          "key" : "Pre-registered",
          "doc_count" : 2071
        },
        {
          "key" : "Demonstration",
          "doc_count" : 1427
        },
        {
          "key" : "Employee's car",
          "doc_count" : 900
        },
        {
          "key" : "Antique / Classic",
          "doc_count" : 195
        },
        {
          "key" : "Automatic",
          "doc_count" : 1
        },
        {
          "key" : "Metallic",
          "doc_count" : 1
        }
      ]
    },
    "makes" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 9814,
      "buckets" : [
        {
          "key" : "Volkswagen",
          "doc_count" : 3295
        },
        {
          "key" : "BMW",
          "doc_count" : 3212
        },
        {
          "key" : "Audi",
          "doc_count" : 2768
        },
        {
          "key" : "Ford",
          "doc_count" : 2109
        },
        {
          "key" : "Opel",
          "doc_count" : 1840
        },
        {
          "key" : "Fiat",
          "doc_count" : 1692
        },
        {
          "key" : "Renault",
          "doc_count" : 1691
        },
        {
          "key" : "Peugeot",
          "doc_count" : 1458
        },
        {
          "key" : "Skoda",
          "doc_count" : 1193
        },
        {
          "key" : "SEAT",
          "doc_count" : 958
        }
      ]
    },
    "body type" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Sedans",
          "doc_count" : 8410
        },
        {
          "key" : "Off-Road/Pick-up",
          "doc_count" : 6498
        },
        {
          "key" : "Station wagon",
          "doc_count" : 4894
        },
        {
          "key" : "Compact",
          "doc_count" : 3516
        },
        {
          "key" : "Van",
          "doc_count" : 1844
        },
        {
          "key" : "Transporter",
          "doc_count" : 1539
        },
        {
          "key" : "Coupe",
          "doc_count" : 1144
        },
        {
          "key" : "Convertible",
          "doc_count" : 1104
        },
        {
          "key" : "Other",
          "doc_count" : 654
        }
      ]
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oussama ROUABAH