Questions tagged [case]

In SQL, the CASE expression evaluates a list of conditions, returning the result for the first condition which evaluates to true. For programming languages' CASE questions, use the [switch-statement] tag instead.

Filter by
Sorted by
Tagged with
0 votes
0 answers
10 views

How to write a program using switch involving the conversion of kilograms to pounds?

When I define the formula of converting the kilograms to pounds using switch, I ask the user to input the weight of the kilograms. I use case 'p' as my pounds. The program suggest I use & (double)...
Volfan5974's user avatar
1 vote
1 answer
23 views

Oracle : Need to convert ROWS values into COLUMN

I have multiples records which I want to convert into single column. I have table which contains records like below. id prjid studyid a_udf1 b_udf1 a_udf2 b_udf2 a_udf3 b_udf3 1 30 2 abc abc 7-feb-...
munde shubhangi's user avatar
0 votes
4 answers
43 views

Condition on multiple columns in R

I want to create a code that run on a data frame based on conditions form another data frame. The code should do this: if column 1 has A and type1, then give it a score. the condition data frame is ...
Marwah Al-kaabi's user avatar
0 votes
0 answers
20 views

Calculate the median to 4 decimal places [duplicate]

Given a table, station, with one of its column, LAT_N. What is the median of LAT_N to 4 decimal places? with newstation as (select lat_n from station order by lat_n asc) select count(lat_n) as n case ...
Arjun Bindra's user avatar
0 votes
3 answers
31 views

Case when statement to categorized values

TABLE_1: Col_1 Col_2 Col_3 A Apple 100 A Apple 100 A Apple 56 A Apple 44 B Banana 100 C Cucumber 93 C Cucumber 7 I want to use a case when statement to categorize Col 1 as "good" or ...
NEWBIE's user avatar
  • 19
0 votes
1 answer
15 views

Should I provide two "Then" in Cucumber in this scenario?

I am writing test cases in Cucumber and I have been wondering is this type of scenario ok to provide two then's. I have a situation where after user does something in the app, let's say makes an order,...
kkk's user avatar
  • 1
0 votes
2 answers
37 views

Create ranges with CASE statemant - ERROR: Each GROUP BY expression must contain at least one column that is not an outer reference

I'm tring to create ranges using CASE but I have a problem with counting the numbers of appearances. At the moment my code looks like this: SELECT CASE WHEN engine_capacity < 1 THEN 'less than ...
RageAnalyst's user avatar
1 vote
1 answer
33 views

CASE WHEN for changing duplicate value

I am trying to add a case when statement which would add +1day into a date column of a record, if there are duplicates per two columns. This would be the original table: A B C Red Table 2023-01-01 ...
Nairda123's user avatar
  • 149
0 votes
1 answer
29 views

Update multiple rows with multiple 'where' clauses using CASE

I have the following query and want to optimize it. This could be up to 300 or more trips to the database. update evtentries set ro = '1', ediv = 'A' where entid = '1234' and ID = '10024' ...
H.L.'s user avatar
  • 75
0 votes
0 answers
36 views

Weekday from TIMESTAMP in SQL [closed]

SELECT SaleID, SaleDate, CASE DATEPART(weekday, SaleDate) WHEN 1 THEN 'Sunday' WHEN 2 THEN 'Monday' WHEN 3 THEN 'Tuesday' WHEN 4 THEN 'Wednesday' ...
 MNV's user avatar
  • 1
1 vote
2 answers
39 views

SQL: Subtract values within same column based on two different dates

I have a table as below, and I would like to subtract the values within column 'Price', based on the different dates. After subtracting them, I intend just to use the first set of datetime Table ...
Ahmad Fuad Jamaludin's user avatar
-1 votes
1 answer
17 views

How to get distinct records from MySQL table with duplicate/varying Boolean value

I have a table with following structure: Name Subject ExamType Passed Bryan Science Theory True Bryan Science Practical False Joe Science Theory True Joe Science Practical True Jill Science ...
Kapil's user avatar
  • 3
1 vote
1 answer
20 views

create a new column based on three columns in pyspark dataframe

I have a dataframe like below - |short_1|short_2|full_nm| |"Ginder"|"Ginder v. Ginder"|"Carrie Diane GINDER v. Carl L. GINDER"| |"KENNEY"|"KENNEY v. ...
emiley mille's user avatar
0 votes
1 answer
27 views

Angular Typescript can't get dropdown value correctly for case branch

Declaration of the form with typeTerritoire filter dropdownOptions2: SelectItem[] = []; searchForm = this.fb.group({ .. typeTerritoire: undefined, }); search() { const ...
Andrew's user avatar
  • 288
1 vote
1 answer
54 views

SQL update table , case when, select distinct

I have 2 tables as below Table 1: r_report8 work_order_no report8a report_item7 EGR220511003493 56 1 Table2 : rjob_header work_order_no report_item7 EGR220511003493 1 I already set a triggers on ...
Ray's user avatar
  • 23
-1 votes
1 answer
36 views

Call a CTE based on a criterion - Case statement SQL [closed]

I have two CTEs with the same structure but reading data from two different tables. Based on a date filter how do I call a CTE using an IF or CASE statement in SQL? Something like this: with ...
moikoi's user avatar
  • 25
0 votes
0 answers
33 views

Using google sheets - "Query function" to find during what time of the day the instance_id percentage is lowest

Suppose there’s a log of security related incidences for about 60 days along with instance_id. Now, there are cases when the incident is reported but the incident_id is not. This should be done only ...
Gracia M's user avatar
0 votes
1 answer
67 views

use condition in case_when() once?

I have a data frame with multiple time points but I only want to define the first event per time point. F.i., selecting only the first event between 12 months and 24 months and not all. data %>% ...
Philipp's user avatar
  • 19
-2 votes
1 answer
19 views

Creating new view with match value from another table SQL

I have 2 tables like in the picture. In the first table I have column ID, in the second table I also have ID column and email column. In the end, I want a table 1 with specific email from table 2 ...
Darren's user avatar
  • 7
0 votes
1 answer
63 views

Calcul between two rows SQL with conditions

I have an SQL database containing invoice data. It includes columns for "sum" and "invoice_id," where the total amount is stored (for sum), and if it's a down payment (invoice_id ...
Flavien's user avatar
-3 votes
1 answer
21 views

Write a function named WeekDay() making use of select case, the function will receive a single argument containing a STRING (days Monday - Sunday)

Use Scilab answer the following questions: Paste both your function code, and console output demonstrating the features of the function. Write a function named WeekDay() making use of select case, the ...
Priyanka Gosai's user avatar
-2 votes
1 answer
49 views

i want to display output like this with sql server [closed]

this is my original table in database name qty QMORIASDWAD 1 QMORIASDWAD 3 QMORIASDWAD 4 QMORIASDWAD 5 QMORIASDWAD 6 QMORIASDWAD 7 QMORIASDWAD 8 and i want my reuslt output with select to ...
human's user avatar
  • 1
0 votes
1 answer
28 views

Informix - querying for latest date across multiple columns and multiple rows

I need to get the latest date from multiple database columns (date_entered, date_updated) in an Informix database. Logically, date_updated should always be the latest but that is not true in this data ...
bloodtalon's user avatar
0 votes
1 answer
21 views

Stuck on single row subquery returning more than one row in SQL update statement

Hoping somebody might be able to shine some light on this predicament. How can I work around this, and why does SQL think it's returning multiple lines. It's just a case statement which should ...
allenator's user avatar
0 votes
3 answers
53 views

SQL GROUP BY with CASE and CAST DATE

I have below source table in SQL which I would like to query to get output table as per below to have sum of amount from each source aggregated by date without time. Unfortunately my code doesn't work....
DearDeer's user avatar
  • 173
0 votes
2 answers
37 views

CASE tries to evaluate subexpressions before checking the expression

PostgreSQL version: 15 I have dynamic tables with dynamic columns and I need to calculate some abstract property length. If column is array then I need to use array_length function, if column is ...
Prosto_Oleg's user avatar
0 votes
2 answers
36 views

mutate %>% case_when assigning values to incorrect columns and/or multiple columns

I have inherited a script that is way above my skill set and the person who created it, is no longer around. I have noticed one thing wrong with the script that seems easy to fix (or so I thought). ...
user8107894's user avatar
-2 votes
3 answers
75 views

Need a help in writing case statement for the below scenario

I have a table as below: ID Salary plan End Date 1001 20000 A NULL 1001 300 B NULL 1002 40000 A 2023-04-31 1002 250 B NULL My Requirement is, for an employee if PLAN=A and [END DATE] <> ...
ramesh k's user avatar
0 votes
2 answers
37 views

Comparison operators with a CASE statement and Window Function [duplicate]

I'm trying to use a case function with a window function, but the data I'm getting doesn't add up. Everything seems to work, but I'm only receiving "Above Average" for that entire CASE ...
Bobby's user avatar
  • 3
4 votes
2 answers
95 views

Style aside, is there any reason to prefer simple `CASE WHEN` to searched?

I wish to write the following CASE [FOO] WHEN 1 THEN 'YES' WHEN 0 THEN 'NO' WHEN NULL THEN 'NO' END I could, of course, write CASE WHEN [FOO] = 1 THEN 'YES' WHEN [FOO] = 0 OR FOO IS NULL THEN 'NO' ...
J. Mini's user avatar
  • 1,956
-2 votes
0 answers
15 views

Select-case with read operations do not block if channel is closed, instead returns empty values [duplicate]

I was always under the impression that a select-case (without a default case) will wait for a channel with a value available to read, before the case is executed. I'm just summarizing the spec, but ...
smac89's user avatar
  • 39.6k
-2 votes
5 answers
62 views

Group by and Case when SQL

I have a table containing the date , the phone number , the name , and the sale amount. I want to apply the condition on the name and if the name initial letter is "X" then put the second ...
Razieh Delavar's user avatar
-1 votes
0 answers
31 views

sql case : check if field A exist in field B according a specific id

I have a data extension with the first 3 fields : Id, Email_source1 and Email_source2. I want to create the field named X. The goal is to check if Email_source1 is in Email_source2. Does someone know ...
ayakuza91's user avatar
-1 votes
0 answers
37 views

Check If a row value is present in another table within a group

I have a requirement to check if the value from the master table is present in the transaction table within groups. Master table: ID Item 1 Home 2 Admin 3 Users 4 Roles 5 Deals Transaction ...
Chaitra Murthy's user avatar
-2 votes
0 answers
59 views

Select case when exists between two tables

Table1 workorderno EGR98765432100 EGR65449873210 EGR32165232321 Table2 workorderno EGR98760000000 EGR65449873210 EGR32162223111 Table1 workorderno ReportItem EGR98765432100 No ...
Ray's user avatar
  • 23
0 votes
3 answers
78 views

Create Case statement as Function

I have a table PeriodRanges of date ranges in columns StartDate and EndDate with an assigned Period as the 3rd column. For example: StartDate EndDate Period '2023-01-01' '2023-01-28' 1 '2023-01-29' ...
Carbine734's user avatar
-3 votes
2 answers
88 views

Combine CASE expression in JOIN Clause involving different comparison operators [closed]

I have two SELECT statements and want to combine them into one JOIN statement based on the input param: @UseFirstOne = true. If @UseFirstOne is true then we join on the first condition and if false, ...
Utsab's user avatar
  • 187
0 votes
1 answer
20 views

BigQuery Results in Two Rows - Need to be on the same row

I have hit a wall. I am attempting to get a count of accounts, but also separate this count by account_type. I did try COUNT and COUNTIF first, but that did not work. I then did a CASE statement ...
Aaron Dodge's user avatar
0 votes
0 answers
37 views

How write query for join two tables without nulls?

I have 2 tables Airport + -------------------------------------+-------------+-------------+ | ID | IATA | ICAO | +-------------------------------------...
user6408649's user avatar
  • 1,237
-1 votes
1 answer
45 views

Sql Case When multiple colums and multiple condition

I need to change the column value from 2 columns Example Declare column*A int, column*B int, columnC int If columnA = 1, columnB = 1 then columnC = 1 If columnA = 0, columnB = 1 then columnC = 1 ...
Ray's user avatar
  • 23
-2 votes
1 answer
69 views

How do I get my t-sql code to check for a condition then stop? not check the next condition. Return should be only one result

Pseudo: If condition is true, return results and don't look for next condition. If condition is false, then check for the next condition. etc ...I have like 6 of these to check in a hierarchy order. ...
Tami's user avatar
  • 19
0 votes
1 answer
54 views

How to get my error message (In C, using switch) to show if failed to give arguments correctly?

Warning, I'm super new with C language (and coding) so please explain to me like I'm simple :) So, I'm trying to get my case 'A' and case 'B' to give this message "printf("A(or B) should be ...
Ronja Oksanen's user avatar
0 votes
2 answers
21 views

Multiple case conditions are executed And giving error

I am using the following query SELECT d.mon, CASE WHEN MAX(CASE WHEN d.DRR = '80+' THEN 9999 ELSE TO_NUMBER(substr(d.DRR,1,INSTR(d.DRR, '-') - 1)) END) = 9999 THEN '80+' WHEN MAX(CASE WHEN d.DRR = '...
Ali Khalil's user avatar
0 votes
0 answers
24 views

difficult "case when" and datediff sql query

I am trying to make this query work: (case when R.DateofRepairInvoice is not null and [Last Repairs Rcvd] is null then isnull(convert(varchar,(DATEDIFF((Case when Format(R....
John Maygee's user avatar
0 votes
1 answer
26 views

Scaling up case_when (tidyverse) for large dataframe with n rows

I have a dataframe "factDf" where I would like to fill in values in the column "trial_number" based on two conditions. I as well have a reference dataframe "dimDf", the ...
sa-al's user avatar
  • 5
0 votes
1 answer
40 views

Can't Figure How to use case_when and grepl to calculate per 100k for different years

As part of a project with COVID data, I need to calculate the rates per 100,000 people for my table. The math itself is easy, the hard part is that there is population data for two different years: ...
Grace Cooper's user avatar
0 votes
1 answer
60 views

Use same column value twice in a SQL case statement

I have joined multiple views to get the following output for time taken by each Department to complete a task. The output looks like this: ID PROCESS DEPT SECONDS ------------------------------...
moikoi's user avatar
  • 25
0 votes
1 answer
47 views

SQL Sub Query in Case statement

I have two tables: Employee Name Salary Gaurav 2000 John 5500 Wayne 6500 Sam 1000 Threshold id MinSalary Maxsalary band Startdate Enddtl 1 0 3000 Junior 2023-08-01 2023-08-01 2 3001 5000 ...
Gaurav Jan's user avatar
0 votes
1 answer
31 views

Case sum in snowflake and name accordingly

I have joined multiple views to get the following output for time taken by each Department to complete a task. The output looks like this: ID PROCESS DEPT SECONDS S911 BATCH SALES 24268 S911 BATCH ...
moikoi's user avatar
  • 25
0 votes
1 answer
40 views

case_when NA/NaN when using variables to generate a range based condition

I'm trying to generate sequence based on a mod result being in a range defined by variables which are integers, All the methods I've tried so far fail with this sort of error message: Caused by ...
Peter's user avatar
  • 11.7k

1
2 3 4 5
163