70218

how to run a different select statement based on condition in Hive SQL

Question:

I would like to know how to run a different select statement based on condition in Hive SQL.

The following query does not work but throws an error.

<blockquote>

Error while compiling statement: FAILED: ParseException line 4:2 cannot recognize input near '(' 'SELECT' '1' in expression specification

</blockquote> SELECT CASE WHEN '${UN}'!= '' THEN ( SELECT * from table1 t WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}' AND t.un in ('${UN}') ) ELSE ( SELECT * from table1 t WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}' AND t.un in ( (SELECT o.unq_num as un FROM table2 as o WHERE o.date >= '2017-01-01' AND upper(o.srl_num) in ('${R}') LIMIT 1) ) ) END

Answer1:

Use UNION ALL with your queries + add conditions for switching corresponding query:

select * from table1 t where (t.yymmddval BETWEEN '${D1}' and '${D2}') and t.un in ('${UN}') and '${UN}'!= '' --switching condition union all select * from table1 t where (t.yymmddval BETWEEN '${D1}' AND '${D2}') and t.un in (SELECT o.unq_num as un FROM table2 as o WHERE o.date >= '2017-01-01' AND upper(o.srl_num) in ('${R}') LIMIT 1) and '${UN}'= '' --switching condition

Recommend

  • How to reference component inside a composite component when using a converter
  • Could not transfer artifact from/to central because of InvalidAlgorithmParameterException: the trust
  • How to open camera in android webview?
  • How to write a background service to get latitude and longitude?
  • Python SKLearn: How to Get Feature Names After OneHotEncoder?
  • How to get metadata from Media objects
  • GC.start has no effect in Pry, but does in IRB
  • Encode string to Base64 in Inno Setup (Unicode Version of Inno Setup)
  • MySql tables, error #1064 & error #1068 Multiple primary key defined [closed]
  • how to synchronize web site content while loading page
  • in Gwt, there are 2 different packages (or 2 options) for doing drag n Drop? Which one is better?
  • Nested AJAX Calls using .done
  • Windows: How do I get the mode/access rights of an already opened file?
  • Map Annotation Disclosure Indicator - Xamarin.Form
  • “proxy_pass” cannot have URI part in location given by regular expression
  • Error processing job: Project has not enabled BigQuery
  • Simultaneous animation when entering editing mode of UITableViewCell
  • Homomorphic filtering in OpenCV
  • How to get a time and Date Separately?
  • Disabling swipe gesture in Windows Phone 8.1 pivot control
  • How to put an object in the air?
  • Sample deviceQuery cuda program
  • Android studio import problems. (Apktool)
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • How to use Flask's render_template from an ajax POST form submit
  • Disable account chooser FirebaseUI React
  • SQL Server 2012 not showing unicode character in results
  • What is the difference between dynamically creating a script tag and statically embed a script tag?
  • Regex not working in java 1.5
  • Java .policy file - how to prevent java.util.Date() from being accessible
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • Google Spreadsheet Script to Blink a range of Cells
  • Comparing variables with strings bash
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Using redis as an LRU cache for postgres
  • Google App Engine Datastore: Dealing with eventual consistency
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • ssh remote server login script
  • How to call different template for different category archive page in woocommerce
  • WPF custom control and direct content support