Questions tagged [case-statement]

A case statement allows sequences of SQL statements to be selected for execution based on search or comparison criteria, and is typically used in stored procedures. Do not use this tag, use [switch-statement] instead.

The CASE statement provides a mechanism for conditional execution of SQL statements.

It exists in two forms: the simple case and the searched case.

The simple case involves an equality comparison between one expression and a number of alternative expressions, each following a WHEN clause.

The searched case involves the evaluation for truth of a number of alternative search conditions, each following a WHEN clause.

In each form of the CASE it is the first WHEN clause to evaluate to true, working from the top down, that determines which sequence of SQL statements will be executed.

There may be one or more SQL statements following the THEN clause for each WHEN. If none of the WHEN clauses evaluates to true, the SQL statements following the ELSE clause are executed. If none of the WHEN clauses evaluates to true and there is no ELSE clause, an exception condition is raised to indicate that a case was not found.

Providing an ELSE clause supporting an empty compound statement will avoid an exception condition being raised, in cases where no ‘else’ action is required, when none of the WHEN alternatives evaluates to true.

430 questions
3
votes
1 answer

Scala warning erasure in case

I have the following pattern matching case in a scala function: def someFunction(sequences: Iterable[Seq[Int]]):Seq[Int] = sequences match{ case Seq() => Seq(1) case _ => ... ... } And I get the following warning: warning: non variable…
GTDev
  • 5,488
  • 9
  • 49
  • 84
3
votes
5 answers

Why must you GROUP BY the columns in a CASE statement?

I'm trying to run a query in SQL Server 2008 against a table where some of the data was entered inconsistently and I have to handle this. Table data example: OrderID Qty Price MarkedUpTotal 1 10 1.00 11.00 1 -1 1.00 …
MDStephens
  • 1,013
  • 2
  • 9
  • 10
2
votes
5 answers

SQL: How can i build a string from column values of one row?

I have rows like this... | NAME | RED | BLUE | GREEN | LeAnn 1 0 1 Jim 0 0 0 Timothy 1 1 1 I want to write a select statement that would return the following rows... |…
daveomcd
  • 6,367
  • 14
  • 83
  • 137
2
votes
2 answers

Case in nested select

I'm having trouble getting a CASE statement to work in a nested select. I think I'm close but I can't quite get the syntax right. So far I've tried: SELECT l.*, Credit = ( CASE WHEN ISNULL(M.POSTCODE,'') <> '' THEN sum(Credit) from Balance cb…
Echilon
  • 10,064
  • 33
  • 131
  • 217
2
votes
1 answer

In Haskell, how to do a case statement on Dynamic TypeRef

I tried the following: intType = typeOf (5::Int) stringType = typeOf "s" dynFunc :: Dynamic -> IO () dynFunc d = case dynTypeRep d of stringType -> polyFunc ((fromDyn d "") :: String) intType -> polyFunc ((fromDyn d 0) :: Int) _ …
mentics
  • 6,852
  • 5
  • 39
  • 93
2
votes
2 answers

How to resolve nested aggregate function error?

I used case function to bucket aggregate counts of an item and want to divide the sum total of all counts into the value for each bucket (looking to have each bucket shown as a % of the total). However, I am getting an error that I cannot nest…
2
votes
1 answer

SQL Server: Pattern Matching in Case Statements with Multiple Possible Conditions

I am writing a query in SQL Server wherein I'm attempting to generate a data indicator based on multiple "fuzzy matched" criteria. Some example code to illustrate this task: CASE WHEN COLUMN_1 IN ('CRITERIA_A') AND COLUMN_2 NOT LIKE…
2
votes
2 answers

Get successive row values without loop in SQL

I have the following table: AppId Id Direction Text Date aaa 11 in hello 11/2/2021 3:03:00 PM aaa 22 out yes? 11/2/2021 3:04:00 PM aaa 33 in need help! 11/3/2021 3:06:00 PM aaa 44 in you there? 11/4/2021 3:10:00…
trojan horse
  • 347
  • 2
  • 10
2
votes
1 answer

SQL VIEW WITH JOINS

I have 3 tables Node table - Nodeid, Node relationship id(NodeRelID) Node relationship table - id, Nodeid, Node Link id Eventstatus Tabel - id, Nodeid, Node Status. I want to create a view where it displays each node's id and the status of the node…
2
votes
1 answer

Oracle SQL only: Case statement or exists query to show results based on condition

I am trying to create computed column based on some conditions by using case statement. I am so close to goal but unable to see where the query is going wrong. Hope I get some best/easier methods and some help here. Below are the tables: PERSON…
Richa
  • 337
  • 4
  • 18
2
votes
1 answer

How do I print the matched pattern that produced the result of a multi-like case statement?

I am looking to store in a column (matched_pattern), as a string value, the matched pattern for a string field (title) from a multi-like case when statement in SQL. Example table: | Title | Email …
K8123
  • 23
  • 4
2
votes
2 answers

Impala Query to get next date

I have 2 Impala tables. 1st table T1 (additional columns are there but I am interested in only date and day type as weekday): date day_type 04/01/2020 Weekday 04/02/2020 Weekday 04/03/2020 Weekday 04/04/2020 Weekend 04/05/2020…
Padfoot123
  • 1,057
  • 3
  • 24
  • 43
2
votes
3 answers

Switch Over Value Determined at Runtime in c#

I've got a case statement in c#. I would like to chose the values for the cases from a configuration file at run time. Is this possible?
macleojw
  • 4,113
  • 10
  • 43
  • 63
2
votes
5 answers

C - Tricky Switch Case working .. !

Folks, Recently started learning C. Stuck at a point. Its about working of switch-case statement. Here's the code : #include int main() { int i=4; switch(i) { default : …
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
2
votes
2 answers

How to create multiple 'THEN' clauses for a BigQuery standard SQL case statement?

I'm using standard SQL on BigQuery to create a new table based on certain conditions within an existing table. I have multiple WHEN clauses to support this (as there are several different conditions I'm checking for). What I now want to do is have…