What's the deal with Deno? We talk with a major contributor to find out. Listen now.

Questions tagged [case]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
-3
votes
0answers
16 views

Need to duplicate rows that have same criteria in SQL case statement

I have below data set, from which I want to extract multiple rows. account 123 start_date 5/10/20 end_date 10/20/20 Using the below code my result is only 1 row with "today", but since ...
0
votes
2answers
14 views

Creating a column in dataset for later filter use (mysql)

I have a date column (format: 14.05.2018) and a count of sales. I would like to create an extra column that from the current date perspective flags fields that are +/-6 days from today. The following ...
0
votes
0answers
37 views

Why are NULL values not being recognized as null in my CASE expression?

I've tried writing this two ways. The first one ignores the WHEN NULL THEN '' part, and the second one just doesn't work. SELECT buffer_mol, (CASE buffer_mol WHEN NULL THEN ' ' ...
1
vote
1answer
34 views

MySQL CASE WHEN is not working as expected with user-defined variables?

i need help please with the following query. I am pulling zip codes (plz) along with user id (uid) from DB and ORDER them by zip (plz) and uid. Also I use a user-defined variable @prev to check if ...
0
votes
1answer
20 views

Case overcounting what is expected

So I am attempting to setup a query that can track actions that are automated within our web system. The first thing I am attempting to check is the reminders we are sending out to customers. I have ...
0
votes
1answer
22 views

Return single row with one to many relationship based on evaluated conditions

Database is Postgres. I have an issue whereby I have a table with the following columns: ID, Product_ID Customer_ID, Price and Item_Type. This table is joined on Product_ID to another table. For a ...
0
votes
1answer
14 views

PostgreSQL CASE expression

how to write a case expression with Postgres? I have this case when rc.stat = 0 Then unpaid when rc.stat = 1 Then paid when rc.stat = 2 Then dispute else rc.stat end but when I go to run ...
1
vote
1answer
35 views

CASE in WHERE clause - retuns a single-row result instead of 4 rows?

Everyone, I do not want to bother you with the whole tables schema so I will paste a couple of screenshot only so that my question becomes clear - I need to show all projects that are related to ...
0
votes
1answer
51 views

CASE WHEN DATE not providing correct results

When using the below query the "Result_field" returns "NO" even if the date is greater than 12/31/2020. How can I get it to show "YES" when the date is greater than 12/31/2020 and "NO" When Less Than?...
0
votes
0answers
22 views

Error Code: 1242. Subquery returns more than 1 row in Case statement

I have table called tblDailyCheckIn(DailyCheckInID, EmployeeID, WorkingDate, CheckIn, Checkout, WorkingHours, Comment, IsActive, CreatedBy, CreatedDate, ModifyBy, ModifyDate). Here I want to select ...
0
votes
1answer
25 views

Sql Statement to display multiple true statement

I have a sql statement that checks for certain special characters in a string and returns that. SELECT CASE WHEN (LEN(DSC1) - CHARINDEX(char(1), DSC1)) <> LEN(DSC1) THEN '[DSC1 - NUL (null)], '...
0
votes
1answer
47 views

Using case and group by in select statement not working

I'm using a case and group by in a select statement and I would like for the case field name (item number) to not be apart of the group by clause statement. I've calculated the invoice weight as well ...
1
vote
1answer
33 views

Conflicting 'LIKE' conditions in MySQL CASE statement

I have two conditions that are causing the CASE statement not to work as intended. I've tried changing which condition comes first, but it still only proceeds with one of them. Any suggestions? CASE ...
5
votes
1answer
294 views

Include non-inserted rows as result of INSERT INTO … RETURNING

I'm using PostgreSQL 11. I want to conditionally insert values into a table while having the result of the insertion include a null for each row of the input which did not result in an insertion. For ...
0
votes
2answers
18 views

Combining previously different rows after changing value during query

I'm currently running a query that is supposed to return a salesperson's aggregated revenue, grouped by individual clients. Because of how user input was handled, there are a lot of near-duplicate ...
0
votes
1answer
284 views

ORDER BY items must appear in the select list if SELECT DISTINCT is specified

SELECT DISTINCT * FROM ( SELECT a.Title, a.ID FROM TableA a LEFT JOIN TableB b ON a.ID = b.XID) c ORDER BY CASE WHEN c.ID > 10 THEN c.Title ELSE c....
1
vote
4answers
61 views

SUM several CASE results

Given: CASE WHEN A IS NULL THEN 1 ELSE 0 END AS [A] , CASE WHEN B IS NULL THEN 1 ELSE 0 END AS [B] , CASE WHEN C IS NULL THEN 1 ELSE 0 END AS [C] How can I SUM A+B+C being the ...
0
votes
1answer
53 views

Help with SQL CASE WHEN using % Query

I have two columns in a SQL Query ( Estimate and Billed ) which contain un-formatted numbers. I am trying to write a CASE WHEN query for when the BILLED column is between 50% and 70% of the BILLED ...
0
votes
2answers
67 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
2answers
48 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
34 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
40 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
73 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
39 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
47 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
113 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
159 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
109 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
109 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
50 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
24 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
45 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
18 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
27 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
73 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
2answers
367 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
211 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
328 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
369 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
4k 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
37 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 | |...
5
votes
3answers
1k 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
843 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
76 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
90 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
909 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
327 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
175 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
4k 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
6k 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;