Questions tagged [case-when]

Use this tag only for SQL queries with case expressions. (Use switch-statement for "case" statements in Ruby and other languages.)

Use this tag for SQL queries. Use for "case" statements in Ruby and other languages.

882 questions
8
votes
3 answers

Mutate, across, and case_when

I am having some trouble getting mutate, across, and case_when to function properly, I've recreated a simple version of my problem here: a <- c(1:10) b <- c(2:11) c <- c(3:12) test <- tibble(a, b, c) # A tibble: 10 x 3 a b c …
RobBot
  • 93
  • 1
  • 3
8
votes
1 answer

r dplyr::case_when Error: must be a character vector, not a double vector

I am trying to use dplyr::case_when within dplyr::mutate to replace some values: data<-data%>% mutate(floor = case_when( floor_id == 2 ~ "ground_floor", floor_id == 3 ~ "mezzanine", floor_id == 1 ~ "basement", floor_id == 30 ~…
kskirpic
  • 155
  • 1
  • 1
  • 7
8
votes
2 answers

How to using select with if condition in oracle?

My requirement is to get a number from a complex query and check if the num = desiredNum. If it is equal to desiredNum, then I must perform another set of select statements, Is there any way I can achieve this in a query rather than writing a…
swathy
  • 189
  • 1
  • 3
  • 14
7
votes
2 answers

Multiple WHEN condition implementation in Pyspark

I've my T-SQL code below which I've converted in Pyspark but is giving me error CASE WHEN time_on_site.eventaction = 'IN' AND time_on_site.next_action = 'OUT' AND time_on_site.timespent_sec < 72000 THEN 1 -- 20 hours WHEN…
Katelyn Raphael
  • 253
  • 2
  • 4
  • 16
7
votes
6 answers

correct way to create a pivot table in postgresql using CASE WHEN

I am trying to create a pivot table type view in postgresql and am nearly there! Here is the basic query: select acc2tax_node.acc, tax_node.name, tax_node.rank from tax_node, acc2tax_node where tax_node.taxid=acc2tax_node.taxid and…
mojones
  • 1,640
  • 3
  • 15
  • 26
6
votes
1 answer

TSQL and case when with multiple whens?

I have several conditions and the result for those should be the same. I searched the net and found stuff like this: CASE ProductLine WHEN 'R' THEN 'Road' WHEN 'M' THEN 'Mountain' WHEN 'T' THEN 'Touring' WHEN 'S' THEN 'Other sale…
grady
  • 12,281
  • 28
  • 71
  • 110
6
votes
0 answers

Behaviour of case_when with numeric(0)

I have a problem understanding how dplyr::case_when works. Here with this pretty simple line : library(tidyverse) case_when(TRUE ~ 50, FALSE ~ numeric(0)) I get numeric(0) while obviously, TRUE is TRUE and so it should send back 50.…
Malta
  • 1,883
  • 3
  • 17
  • 30
6
votes
5 answers

Ansible Playbook: How to run Play based on when condition?

I have a playbook with multiple plays. Here is what it looks like: - name: Play A hosts: localhost roles: - do task to get either B or C - name: Play B hosts: Host B roles: - do necessary task - name: Play C hosts: Host C …
Fitri Izuan
  • 350
  • 1
  • 6
  • 18
6
votes
1 answer

TSQL NVARCHAR Conversion Error inside CASE statement

This select is driving me crazy. The error is: Conversion error converting nvarchar value '17.30 h' to int data type. Data is: (DateTime) (Nvarchar) (DateTime) DATAINICI DATAMANUAL DATAFI null 17.30 h 10/01/2015 01/01/2015 …
JoeCool
  • 907
  • 1
  • 11
  • 25
6
votes
4 answers

SQL Count sickness days

I have a SQL Server table which contains the list of all staff and their sickness. I need to be able to calculate how many days they have had sick in the current quarter The issue is, some people may have been sick for a year so, E.G the FROMDATE…
David Hayward
  • 189
  • 1
  • 1
  • 14
6
votes
3 answers

how use SQL WHERE CASE with NOT IN or equals at the same time?

Hi all (my first post on the Stack!), This works: where Tran_date between @FromDate and @ToDate and Range = @Range and Store_ID = case when @Range = 'RangeName' then 1234 else Store_ID end but how can I achieve…
Warren
  • 1,984
  • 3
  • 29
  • 60
5
votes
2 answers

Conditional term after the `~` in a `case_when` function

I want to put a conditional term after the ~ in a case_when function. My example: df: df <- structure(list(x = c("a", "a", "a", "b", "b", "b", "c", "c", "c", "a", "a", "a"), y = 1:12), class = "data.frame", row.names = c(NA, -12L)) Not working…
TarJae
  • 72,363
  • 6
  • 19
  • 66
5
votes
1 answer

How to mutate two columns with one case_when expression in R?

Depending on the text in one column I want to assign a character and a integer to two other columns. Multiple case_when conditions (LHS) for assigning the character to one column and the integer to another column are equal only the outcome (RHS) is…
Nils
  • 120
  • 1
  • 7
5
votes
1 answer

Using CASE, WHEN, THEN, END in a select query with MySQL

I'm working on a baseball related website. I have a table with a batting lineup for two baseball teams: +----+----------+--------------+--------+ | id | playerId | battingOrder | active | +----+----------+--------------+--------+ Batting order is…
Stephen
  • 18,827
  • 9
  • 60
  • 98
5
votes
3 answers

How to use a case-when statement in a mysql stored procedure?

I want to set the session_id automatically using the request_time parameter so i opted for a mysql stored procedure that contains a case statement.Here goes. create procedure upd_userinput(in request_time timestamp, out user_session_id int) begin…
watkib
  • 347
  • 3
  • 11
  • 25
1
2
3
58 59