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
-2
votes
2 answers

Conditionally evaulate a condition SQL

I have an employee table from which I retrieve records using a stored procedure now I need to add a condition in the stored procedure -- Only if employeeType='MD' then check if employeeReportableIndicator='N' , if 'N' then do not fetch this…
Manas Saxena
  • 2,171
  • 6
  • 39
  • 58
-2
votes
1 answer

Trying to add a duplicate row

I'm running a contest that lists all videos watched by school teachers, however if one of the videos they watch is a 'stem' video, I want their name to be listed twice so that they can be put into the drawing twice. Basically I have a case when…
-2
votes
2 answers

How to add averages w/ sum (case when...) in MySQL?

Im working on an SQL query that have almost where I want it. I just want to add two more columns for the average submissions for each of the "attributes_share_lemon" and "attribites_not_shared_lemon". Here is the SQL fiddle that I was working on…
lcm
  • 1,737
  • 6
  • 17
  • 40
-2
votes
2 answers

I need to group name in a row

I have 12 rows for year and name, but I want 1 row for year and name with 12 register in (1 or null). select id_agente, nombre, ene, feb, mar, abr, may, jun, jul, ago, sep, oct, nov, dic from ( (select a.id_agente id_agente, …
-2
votes
2 answers

How to create T - SQL statement that return different result set base on criteria (IF statement)

SQL, i created a SP that will return a result set base on pass criteria, i tried this statement CREATE PROCEDURE [dbo].[GetAllMerchandise] @param varchar(50) AS BEGIN SELECT CASE WHEN @param = 'Regular' THEN SELECT id,…
codefrog
  • 3
  • 2
-3
votes
2 answers

How to get row values as columns using Sql Query CASE statement

I have a table s_id student_name attendance_date status 1 student1 2020-01-01 P 2 student1 2020-01-02 P 3 student2 2020-01-01 P 4 student2 2020-01-02 A I want my table as s_id …
-3
votes
1 answer

If else in SQL Server with multiple insert statements

select din, driv_height, city, Driver_Addres_Posta_Code, DRIV_GENDER_ID from driv_testing if DRIV_GENDER_ID IN ('M', 'F') begin insert into driv_success end else DRIV_GENDER_ID not in ('M', 'F') begin insert into error_logs…
-3
votes
1 answer

Using a CASE WHEN statement in NHibernate HQL

My Query select oc.Id as Id, (CASE WHEN (select Count(Id) from CompanyAuthorizedInfo cai where cai.Company.Id = oc.Id and cai.IsDefault = 1 and cai.Status=1) = 1 THEN (select MAX(cai2.Name) from CompanyAuthorizedInfo cai2 where cai2.CompanyId =…
-3
votes
5 answers

WHEN condition in MySql genrating multiple column instead of one?

I am having two tables (student and marks) like below screen: STUDENT TABLE: MARKS TABLE: QUERY: select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total, CASE WHEN m.math < 30 THEN 'Fail in Math' else '' END, CASE WHEN…
Amrendra Kumar
  • 1,806
  • 1
  • 7
  • 17
-3
votes
2 answers

Capture WHERE Clause Conditional Result and append in SELECT Statement - SQL Server

I need to Capture WHERE Clause Conditional Result and append in SELECT Statement using SQL Server Sample SQL: SELECT c.* , Result_Of_Condition_#1 , Result_Of_Condition_#2 FROM Customer c WHERE (Condition #1) OR (Condition #2) Note:…
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
-3
votes
1 answer

SQL - SUMMING CASE/WHEN Continued

I just asked a question linked here about Summing from a CASE/WHEN query. Part of my question was answered, which leads to another question. See picture below: I have three fields, item_no, comp_item_no and Overhead. The comp_item_no is a list of…
Jabo13
  • 65
  • 1
  • 1
  • 7
-4
votes
2 answers

How to apply nested case when statements in the most efficient way possible in SQL?

I'm trying to select two columns from a join with three tables. Specifically, the first column is a rate which is computed as a subtraction between two columns if another column equals 1. The second column is a flag for how many times the txn_rate…
Derek Frank
  • 15
  • 1
  • 5
1 2 3
58
59