title: Grafana: SQL Query: How to apply a filter only if it is selected
body: <p>I am using PostgreSQL Connection with Grafana.</p>
  <p>I have the following SQL query:</p>
  <pre><code>select video_type as video, 
    sum(cv.duration)/3600 as &quot;Duration (Hours)&quot;
    from fact.content_view cv 
  where  $__timeFilter(cv.datetime at time zone 'UTC+5')
    and cv.channel_id in ($channel)
  group by 1
  order by 2 desc
  </code></pre>
  <p>When this query is executed by Grafana it is transformed to:</p>
  <pre><code>select video_type as video, 
    sum(cv.duration)/3600 as &quot;Duration (Hours)&quot;
    from fact.content_view cv 
  where  cv.datetime at time zone 'UTC+5' BETWEEN '2021-12-30T17:14:09.53Z' AND '2022-01-06T17:14:09.53Z'
    and cv.channel_id in ('48','13002','1071','1076','310','309','19','13004','1073','815','806','824','823','624','52','670','186','827','14','191','687','13001','58','54','63','305','306','2','6','13005','896','10016','37','304','103','105','104','148','149','946','808','11011','31','192','268','224','269','283','294','183','11001','218','5','188','189','767','759','300','11020','11021','116','814','86','96','99','1049','690','231','204','213','13003','292','1051','46','156','1048','297','164','33','820','32','165','166','206','55','56','119','917','271','100','232','285','220','247','248','61','110','228','187','36','111','193','34','251','11027','244','246','245','117','1087','151','242','11037','163','197','1031','829','828','39','258','178','147','252','11028','10009','756','256','11024','9120','270','273','121','291','1050','290','948','198','721','159','185','51','24','10010','68','1074','124','11022','862','296','219','10011','870','16','114','298','1026','308','90','93','95','853','97','833','142','98','138','137','140','861','272','118','699','113','145','10012','696','10015','771','210','287','106','249','4','62','109','10005','154','11025','102','243','240','229','211','128','254','1033','157','10013','10003','1058','1078','1080','1077','1081','1079','1082','9147','1015','49','42','59','10002','259','683','208','295','71','859','11036','205','10018','265','241','335','908','10014','864','1072','1000','28','107','30','226','621','27','301','233','127','6666','-1','112','29','311','320','312','313','314','315','316','317','318','319','239','126','200','10008','238','184','253','108','160','282','11035','122','10006','278','279','280','281','20','209','65','1070','66','321','53','236','195','222','115','207','199','293','214','162','10007','123','11032','230','11030','181','10001','731','1034','286','216','302','793','212','217','38','132','158','790','791','792','263','950','284','826','227','1057','203','1092','1124','1131','7','1141','1157','1142','1143','1144','1145','1146','1147','1155','1156','1111','1132','1133','1134','1135','1136','1137','1138','1139','1140','1093','1110','1129','1112','1113','1114','1098','1099','1126','1101','1102','1088','1090','1117','1118','1130','1158','1105','1120','1121','1122','307','44','674','134','261','73','88','1107','1108','255','1020','1038','129','12','825','267','8','266','13','143','146','150','18','1028','72','75','69','171','84','25','170','169','43','35','40','21','94','11','10','74','80','67','257','41','76','173','85','87','262','182','179','174','177','152','77','153','172','260','168','180','176','83','47','45','89','26','81','9','70','91','78','92','175','82','23','10017','288','925','196','734','120','1022','201','235','1','131','11023','17','237','11031','130','1075','3','11033','1053','144','655','264','907','22','139','817','935','194','11002','722','215','12001','125','764','816','57','812','813','101','64','161','819','190','1027','11029','274','275','276','277','167','133','155','299','860','223','136','663','60','289','640','50','847','830','839','225','11034','768','625','202','1039','742','221','15','1055','303','79','818','1056','10004')
  group by 1
  order by 2 desc
  </code></pre>
  <p>The channel variable is defined in the following way:</p>
  <pre><code>select  channel_id __value, name __text  from dimension.channel where name not like '\_%' order by 2
  </code></pre>
  <p><a href="https://i.stack.imgur.com/X5IwI.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>My problem is that when 'all' is selected in the 'channel' filter, then Grafana passes all 'channel ids' to the SQL query. And in case I have more than 5000-6000 ids, those are cut by Grafana. Thus I got incorrect result.</p>
  <p>So I am trying somehow, in case there are no values selected from the filter then the whole filter to be skipped. Only in case certain values are selected then this filtered to be applied. Ideally I should be able to limit the selection by e.g. 10-20 values from the filter. Unfortunately, I cannot make that work. Other suggestion to overcome the problem are welcomed as well.</p>
  <p>Thanks</p>