Questions tagged [case]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
-1
votes
1answer
24 views

CASE WHEN SET vs SET CASE WHEN

I have these normalized tables: modules id | module | type -- ------ ---- 1 foo VARCHAR 2 bar TEXT 3 baz VARCHAR content id | module_id | varchar | text -- --------- ...
0
votes
3answers
63 views

Is there a way to use a Case statement to read from one field and change another field? [closed]

I have a simple MS SQL database created from an import from a CSV file. I need to check a field to see if a word is in that field, and if it is, change the contents of another field. Like this: When [...
0
votes
1answer
37 views

Improve CASE select performance on view creation

I am new to SQL, and the below view query is taking a long time to return values because of the case statement, can someone help in identifying how I can speed things up? CREATE OR REPLACE FORCE VIEW ...
1
vote
1answer
28 views

Null check condition in Where clause

DECLARE @SubDesc1 nvarchar(100) SET @SubDesc1 = null I would like to do something like that. Query 1 SELECT * From Table1 where Category = 'Category1' AND [Description] ='ABC' AND ...
-2
votes
1answer
27 views

How to concatenate rows into one comma seperated value when used with case statements

I have a query that has multiple case statements. I am trying to reduce the results to one row per USER_ID that has one of the columns (specialty_id) comma separated because some users have more than ...
0
votes
3answers
64 views

Nullable smallint column usage in a CASE Statement nested in a STUFF function

I'm having issues writing a Stored Procedure that uses a CASE statement inside of a STUFF function. I thought my issue may have to do with Restricted being a nullable smallint. I tried converting it (...
0
votes
0answers
34 views

Case inside case in oracle query

Can anyone help me? I want to use query using CASE WHEN with logic where time less than 03:00 PM on Monday until Friday will get value start from tomorrow 03:00 PM until today 03:00 PM SELECT ...
0
votes
3answers
43 views

I want to know which table the value comes from in inner join

I have a simple query where i do 2 inner joins to combine data from 3 tables. within the table i would like to know which table each value comes from. i attempted the following but the syntax is wrong....
4
votes
1answer
111 views

Conditional selection in FROM clause

I have two tables: parent with columns identifier (pkey) and period; child with columns identifier (pkey), parent_identifier and period. There are additional columns, but I did not list them here as ...
0
votes
1answer
37 views

Conditional Join in DB2

2 tables are given Table T_PRICE P_Price P_Type P_Route 15 red long 10 long 5 small Table T_ROUTE R_Type R_Route blue long red ...
1
vote
1answer
61 views

Case statements for multiple fields when only certain cases exist for each field

We have an inventory feature where we generate Bills. There is an Edit Bill API call. We have implemented it as PATCH call. A Bill with id = 1 has 2 LineItems : | Stock Id | Qty | Rate ...
0
votes
1answer
58 views

Postgresql, case, select in when

System PostgreSQL 11.5 (Ubuntu 11.5-0ubuntu0.19.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 8.3.0-6ubuntu1) 8.3.0, 64-bit Case Does select work in case condition? I need return 1 state ...
2
votes
1answer
41 views

How to combine date and time with a string value in column created by Case in SQL Query

I am in the process of creating a query for an SSRS report that will show a column that has DATETIME values. The column can have NULL values. In my query I would like to use a CASE expression. I ...
0
votes
1answer
22 views

Building distinct Column from a common column from two tables

Good afternoon team, hope all is well I am hoping you can help me. I have following two tables "GODS.DQ_TOBE" & "GODS.DQ_CURRENT". Within each table there is a common column called "...
0
votes
2answers
32 views

Update with rolling sum grouped by another column

I'm trying to build a query to update a MySQL 8.0 table that looks like this ----------------------- user_id | total | delta ----------------------- 1 | 0 | 30 1 | 0 | -10 ...
0
votes
1answer
14 views

Postgresql Date Function Returning Range In Readable Format

I am wondering if it is possible to use postgresql's date-time , concat and case functions to give one of the following results: June 4th to 11th 2019 (where the 1st and 2nd date are within the same ...
0
votes
1answer
26 views

Multiple THEN outputs on a PostgresQL query

Helllo there, I've been playing around with a materialised view, and my goal is to pre-process as much information as I can, aggregating the info into this view that would speed up the data analysis. ...
1
vote
2answers
38 views

Split cases in WHERE clause based on boolean variable

I have to split a WHERE clause based on a boolean variable: If ShowZeroVariable is true, I only what to show rows value 0 in a certain integer column. If false I want to show all that are greater ...
0
votes
0answers
31 views

SQL Server - CASE for multiple tables

hope I can explain myself right in this question. I have three (3) tables from which I have to build a report, this is the data selection process I need to do in order to correctly output the data to ...
0
votes
2answers
167 views

How to use OR operator in CASE THEN statement

I have a situation like this select col1, col2 from table1 where col1 = case when (booleanexp1) then a OR col1 = b when (booleanexp1) then c OR col1 = d end I mean I need to get ...
-3
votes
3answers
109 views

if statement in a query [closed]

I am getting an error while running this code: Incorrect syntax near the keyword 'IF' I need the output through function only with table return . CREATE FUNCTION [dbo].[...
1
vote
1answer
260 views

Case Statement for on same month

i have a CASE statement, which classify my Customer-Revenue and set Flag, when Revenue <> 0 is. But my problem i should search also same month record on my case statement. Logic : if two records (...
-1
votes
1answer
146 views

Get percentage from results from two case results

How i can get percentage from two resuts from case staments? SELECT *, COUNT(CASE WHEN estado_rep >=11 and RELATION=1 THEN 1 ELSE NULL END) as pendiente, COUNT(CASE WHEN estado_rep >=...
0
votes
1answer
2k views

CASE in WHERE clause in MySQL

I'm working in a project with MySQL and using stored procedures to make my SELECT queries, so all of them have this same structure: DELIMITER $$ CREATE PROCEDURE `case_in_where`(IN `column_selector` ...
1
vote
0answers
34 views

Using conditions to return specific rows

From the following table i am trying to retrieve specific rows based on a criteria +----+--------------+------+ | ID | UPDATED_DATE | TYPE | +----+--------------+------+ | 1 | 01/13 | P | |...
4
votes
3answers
396 views

postgres simple number to string mapping

I have a an Enum at the programming language level which is stored as a simple integer on the table. Think: APPLE = 1 GOOGLE = 2 MSFT = 3 AMAZON = 4 ... (100s more) I just wanna query the table and ...
-1
votes
2answers
443 views

DB2 Join with case when statement or union [closed]

How can I use a case statement in a join? Given: 3 Tables Dealer DEALER_NO CUST_ID 001 100 002 200 003 300 Table: Customer CUSTOMER_NO NAME CUSTOMER_MAIL 100 JOHN ...
-2
votes
1answer
52 views

Looking for help with this shipping report! The Case Syntax is throwing me for a loop

So, the goal of this report is to get all orders that have been shipped in a month (October here). I was initially just using the 'PickedUpOn' field but soon found that there were instances where this ...
2
votes
1answer
72 views

Filtering previous month data with case statement

These are my available attributes: DT_CURRENT_DT Month Year 11-OCT-18 10 2018 12-OCT-18 10 2018 13-OCT-18 10 2018 14-OCT-18 10 2018 15-OCT-18 10 2018 16-OCT-18 ...
0
votes
1answer
601 views

MYSQL Using CASE Function between Dates

SQL Version: MySQL 5.6.32-78.1 I have this table below: +-----+----------+--------------+--------------+--------------+------------+--------------+ | id | amount | currency | date_created | ...
0
votes
2answers
66 views

subquery inside a case statement

I have a huge view -the view gathers information of players. In a specific part the view assigns agents for players, those players are selected from table 'S' and are being assigned using a case ...
1
vote
3answers
145 views

Return a value only when conditions satisfy for entire column

Can anyone help me to how to achieve below result Input data ORDER_NUMBER LINE_ITEM FLAG LP-13288 1 TRUE LP-13288 2 FALSE LP-13288 3 (null) LP-13288 4 FALSE LP-13289 1 ...
1
vote
1answer
3k views

How to write case when in where statement in Oracle [closed]

I wrote below statement. In MySQL it executes ok, but in Oracle it fails with error: meg:ORA-00905: missing keyword How should I write this SQL? SELECT count(*) FROM userTable WHERE ( CASE ...
3
votes
3answers
4k views

How do I write a conditional statement (IF) with MySQL?

What is wrong in below simple query? I tried to google it but not found IF 1 = 1 THEN SELECT 1; ELSE SELECT 12; END IF;
1
vote
2answers
818 views

SQL case statement not executing 'else' [closed]

I am on Oracle 12.1.0.2.0 I have a query that I wrote utilizing a CASE statement. I have 2 "WHEN" statements and one "ELSE" statement. The query is working as expected except for the "ELSE" statement. ...
4
votes
2answers
179 views

String comparison without CASE statements?

Is there a shorter more terse way to do a string comparison in SQL Server without wrapping it in a CASE WHEN ... THEN 1 END? In PostgreSQL, I can do SELECT c, c='foo' FROM ( VALUES ('foo'),('bar') ) ...
3
votes
2answers
3k views

case statement sub query need to return multiple values

Below query need to return multiple values select CASE WHEN LTRIM(RTRIM([Order].ClientProject_Id))!='' THEN (SELECT StationeryClientProject.ImpFile from StationeryClientProject WHERE ...
4
votes
2answers
212 views

Sargability of queries against a view with a case expression

I have a view that looks similar to this: CREATE VIEW Shipment.Shipment AS SELECT CASE WHEN shipmentOld.SHIPMENT_ID = 0 OR shipmentOld.SHIPMENT_ID = -1 THEN NULL ...
0
votes
1answer
6k views

Use of null in case expression [closed]

In the customers' table, many customers do not have state data in the state column therefore when we select customers, the state column displays NULL values, which is not meaningful for the reporting ...
1
vote
2answers
258 views

Collapsing a column to yes/no on query

I have a table with two columns name and value. There are many rows with the same name, but with different values. I want to create a function that returns a table with 2 columns. The first column ...
0
votes
1answer
632 views

How can I stop script execution if insert value is null and set value if not null?

I have tried to use Case and SET NOEXEC ON but get the following errors: Syntax Error: unexpected 'SET' (set) Syntax Error: missing 'closing parenthesis' INSERT INTO tag (table, repr, tag, ...
0
votes
1answer
3k views

MYSQL - Operand should contain 1 column(s) - Nested Case statement [closed]

I have written the below query with nested case statements. The syntax checks out but when I run it I receive error code 1241: Operand should contain only 1 column(s). I have attempted to remove the ...
2
votes
1answer
13k views

Empty result set and Case When = 1

I'm pretty sure this is a implicit conversion but I wasn't able to find information about it. Suppose I have this SQL: SELECT CASE WHEN (SELECT 1 WHERE (1=1)) = 1 THEN 1 ELSE 0 END My subquery is ...
0
votes
1answer
1k views

case when IsNull Alternatives?

Is there a way, to optimize this query, where several IsNull in relation with dates are used? Unfortunately the query is a way slow and actually I have no glue, how to make it way faster. case when ...
0
votes
1answer
69 views

SQL CASE and ELSE

Hello I am considering the following: CASE WHEN xyz.something = 1 THEN 'SOMETEXT' WHEN xyz.somethingelse = 1 THEN 'SOMEOTHERTEXT' WHEN xyz.somethingelseagain = 2 THEN 'SOMEOTHERTEXTGOESHERE'...
10
votes
1answer
5k views

How to multiply rows for a column that contains negative and zero values?

I'm trying to get the Product of all rows for a specific column in a grouped by query. Most examples I've found point me towards combining exp, sum and log exp(sum(log([Column A]))) The problem I'm ...
0
votes
1answer
56 views

Looking for another method to count in a single query

So, I found this article is very useful Join two tables and return data and count in a single query and here Single query to return counts over different IDs in a single record for some IDs or ...
0
votes
2answers
63 views

Categorize rows as first, last or in between

I have a table called road_events. create table road_events ( event_id number(5,0), road_id number(5,0), event_type nvarchar2(50), lifecycle_number number(5,0) ); insert ...
6
votes
1answer
722 views

SUM CASE WHEN CLAUSE

I have the below Query and I want the Ageing profile to show as a new column my ageing profile is 0-30, 31-60, 61-90, 91-120, 121-180, 181-365, 365+ I basically want the results to appear lie this ...
-4
votes
4answers
39k views

Using IS NULL in CASE Expression in WHERE Clause [closed]

I have a WHERE clause that I want to use a CASE expression in. However, my CASE expression needs to check if a field IS NULL. If the @UserRole variable value = 'Analyst', then the ...