Questions tagged [case]
The case tag has no usage guidance.
176
questions
0
votes
2
answers
17
views
comparing a date value in one table against multiple values in another to categorise by age
I have created a form which allows users to enter multiple, unlimited month number values and label them to use against inventory to categorise the age of each item.
Table 1 - AgeConfig - Manually ...
-2
votes
2
answers
30
views
I have a table where where each project_id generates multiple rows
My table below shows the rows and columns where the column data is filtered using a CASE syntax to differentiate capital from expense. The problem is I am getting multiple rows per project_ID.
I need ...
4
votes
1
answer
304
views
Can CASE be used to select which table to JOIN?
I'm attempting to use a CASE expression to select which table each row should be joined with.
Say I've got a table of item_instances in my game, of all the items in the game world, and some of the ...
1
vote
1
answer
52
views
search a string to create dummy variables
I have a string variable I have created that is a concatenation of 12 columns, each of the 12 columns contains 4 or 5 digit character. I want to create a series of dummy variables, based on the ...
-1
votes
1
answer
18
views
Error while declaring procedure
When I run this query
it returns me a count of null values of the column country
(select
sum(case
when (country IS NULL) then 1
else 0 END)
as 'null_flag'
from city)
whereas ...
0
votes
3
answers
100
views
CASE statement issue [duplicate]
Can anyone help with this error?
SQL:
SELECT
InvoiceDate,
BillingAddress,
BillingCity,
Total,
CASE
WHEN Total < 2.00 THEN 'Baseline Purchase'
WHEN Total BETWEEN ...
0
votes
0
answers
28
views
PostgreSQL COALESCE arguments change resulting timezone for DateTime
I have strange behaviour of COALESCE function of PostgreSQL. Depending on arguments of this function the result is different:
SELECT
"CreatedOn", -- 7/24/2020 8:15:03 AM
COALESCE(&...
1
vote
2
answers
232
views
MySQL - Update column based on multiple conditions
I have a table which has Rows like these:
+----------+--------------+---------------------+------+
| CASE_ID | CAT | BIRTH | TYPE |
+----------+--------------+------------------...
0
votes
0
answers
27
views
Best approach to convert a non trivial function (CASE WHEN) into a lookup (decision) table?
Assume A, B, C etc are attribute values in the current rowset
Current function logic is similar to:
Case A = 'Y' then
Case B in (1,2,3,4,5) OR C in (3,4,5,6) then 'Sales'
Case B in (99,98,102) OR D ...
9
votes
1
answer
1k
views
Strange conditional Syntax in TSQL Query: "<=+" What does it do?
I am working on diagnosing a problem with a legacy query in TSQL. One of the returned columns in the query is:
CASE WHEN PaidDate >= '2021-01-01' AND PaidDate <=+'2021-10-31' THEN WO ELSE 0 END ...
0
votes
0
answers
34
views
HQL aggregate based on other column
Currently working with Spark data frames using HQL syntax (which slightly differs from SQL). I'm trying to aggregate a table by id and want to select the feature row by the most recent date (they may ...
0
votes
1
answer
90
views
Avoiding a duplicate CASE expression
SELECT x, y, z,
CASE
WHEN COUNT (PH.header_id) OVER (PARTITION BY PL.header_id)
NOT IN (null,0) THEN L_Count
ELSE COUNT (PH.header_id) OVER (PARTITION BY PL.header_id)
END as quantity
Is there a ...
0
votes
2
answers
140
views
Query with CASE in subquery join clause is very slow on PostgreSQL but fast on Oracle and MariaDB
We have an attribute-oriented data management system with bitemporality and flexible tenant priorization logic. Deletion of records is done by inserting the same row in DELETED status. Tenants may ...
0
votes
3
answers
95
views
Query for getting the sum of all unique entries in a column
Currently I'm using mysql to return the sum of all data in a column that have the same word. To do this I'm using the following query:
SELECT SUM(CASE WHEN status_to = 'Draft' THEN 1 END) AS draft,
...
2
votes
2
answers
233
views
Zero/NULL Case Trick
In the book Getting Started with SQL, Thomas Nield talks about a technique he calls the zero/null case trick:
There is a simple but powerful tool that can apply different filtering
conditions to ...
-4
votes
1
answer
199
views
Case statement syntax error [closed]
I am trying the code below but the SQL throws an error
case
when (b.ModuleType in (20) and rtrim(substring(a.PrdAcctId,1,8))='RD')
then (a.Balance1+(a.Balance2-(a.Balance3+a.Balance4))
when (b....
0
votes
2
answers
137
views
Is it possible to nest CASE-statement into WHERE clause?
I am developing an application with FastAPI. It contains a function that brings two variables into a SQL query using the sqlalchemy.sql.text as shown here.
SELECT ATTR3
FROM TABLE
WHERE ATTR1 = ...
4
votes
3
answers
252
views
Why do subsequent unmet conditions in my CASE statement affect my return data type?
I have the following variables and CASE statement:
DECLARE @DATE_VALUE VARCHAR(20) = '4/29/2021'
DECLARE @CONVERT_CODE VARCHAR(1) = 'I'
SELECT CASE @CONVERT_CODE WHEN 'I' THEN DATEDIFF(day, '04/21/...
0
votes
2
answers
212
views
Limit rows in Correlated Subquery
I have one big and two small issues with my query I need help with.
First, I want a SQL (postgres if it matters) query that gets SUM of last two ratings of fruits and makes a new column for each fruit ...
2
votes
2
answers
595
views
How to create a new column in SELECT CASE when string contains Arabic words?
I have a problem with a select case when statement, I would like to add a new column in the select case when statement, I got the result but with ????? because I have set the column to Arabic words, I ...
1
vote
1
answer
62
views
SQLite: How do I use the sum function
The code I am using is here
CREATE TABLE timecard (
id INTEGER PRIMARY KEY autoincrement,
clock_in REAL not null,
clock_out REAL,
lunch integer,
pay_type TEXT
);
INSERT INTO timecard ...
-2
votes
1
answer
63
views
Select Case Statement redundancy -- way to use a recurring expression WITH inequality?
I'm using SQL server.
I want to use a case statement to quickly 'bucket' a bunch of datediff results.
So---
case
when datediff(d,start_time,end_time) > 100 then '100+'
when datediff(d,start_time,...
1
vote
1
answer
302
views
CASE statement not returning expected results [closed]
First time using Postgres and building a query with a CASE statement. I am trying to end up with a collection of data whereby one of the columns in the data (status) qualifies the row based on ...
-1
votes
3
answers
254
views
Function: if either x or y are null, return null
I'm looking for an Oracle 18c function that evaluates to null if either of two variables is null.
The Constraints: (details here)
The function/expression can't contain spaces (so CASE won't work).
I ...
-1
votes
4
answers
50
views
CASE expression with OR AND [closed]
I have 5 columns - Order, shipping, tracking, Delivery and Overallstauts.
I have have 4 status, Green means "good", Amber means "ok", Yellow means "average" and Red means ...
0
votes
1
answer
35
views
Unusual behaviour of a query when using case expression in group by, some records are being missed
I have 4 table with below structure:
Table "Customer_Info"
create table Customer_Info
(
Customer_Number int,
Customer_Issued_Branch int,
<Other Columns related to customer&...
1
vote
1
answer
58
views
A Query with a Dynamic Case clause or how to avoid Case When
Let's take an everyday query with a CASE clause so I can show you the problem, see how we'd have to change the query when a new Certification comes out:
select count(*) as "certCount",
CASE
...
0
votes
1
answer
27
views
Preserve CASE WHEN output as new column in MySQL
I am new to MySQL and this question may look silly, but:
I have a dogs table and I categorize their size by their weight:
SELECT *,
CASE
WHEN weight < 15 THEN 'Small'
WHEN ...
1
vote
0
answers
91
views
Oracle error 43914 in CASE WHEN expression: arguments have undefined collation
I want to know which is the difference between this query:
SELECT *
FROM TEST_TABLE
WHERE CHAR_FIELD = CASE WHEN KEY_FIELD = 'Q' THEN 'M'
ELSE CASE WHEN KEY_FIELD = 'T' THEN 'K' ...
0
votes
2
answers
15
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
0
answers
39
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
votes
1
answer
549
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
1
answer
36
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
1
answer
177
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
1
answer
20
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
1
answer
43
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
1
answer
2k
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
1
answer
55
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
1
answer
130
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
1
answer
39
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
1
answer
374
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
2
answers
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
1
answer
1k
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
4
answers
223
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
1
answer
118
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
2
answers
68
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
2
answers
58
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
1
answer
43
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
1
answer
595
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
3
answers
161
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 (...