logo
down
shadow

CASE statement using 2 different columns


CASE statement using 2 different columns

By : cvriens
Date : November 17 2020, 11:52 AM
wish of those help Can someone please help me fix the below code? , There's another format for CASE that handles this:
code :
CASE WHEN Table1.Column1 = 'a' THEN '1'
     WHEN Table2.Column2 = 'b' THEN '2'
     WHEN Table2.Column2 = 'c' THEN '3'
     ELSE '4'
END AS [Test],


Share : facebook icon twitter icon
Case statement in sql using other selected columns in the same statement

Case statement in sql using other selected columns in the same statement


By : James
Date : March 29 2020, 07:55 AM
wish of those help You can define the A,B column aliases in a CTE then reference them in an outer select.
code :
;WITH CTE AS
(
SELECT CASE ... END AS A, 
       CASE ... END  AS B, 
       column1
FROM your_table
)
SELECT A, 
       B, 
       CASE WHEN column1='1' THEN A ELSE B END Col1
FROM  CTE 
SELECT A,
       B,
       CASE WHEN type='P' THEN A ELSE B END Col1 
FROM master..spt_values
CROSS APPLY (SELECT CASE WHEN number %2 = 1 THEN 1 END, 
                    CASE WHEN number %2 = 0 THEN 0 END) T(A,B)
;WITH x as
(
                SELECT 
                    ROW_NUMBER() OVER(PARTITION BY G.Value, C.Code, FR.Mod1 ORDER BY FR.Date DESC, FG.Date DESC, FC.Date DESC) ROW,
...<snip>                
                WHERE  FR.Date = @VersionDate 
),
x2 As
(
SELECT *,
       CASE WHEN 1=1 THEN Work ELSE 1 END WANNABE
FROM x
)
    SELECT 1 AS Region, 'Test', 
            CAST(Work AS NUMERIC(18,2)) Work,
            Work + 2 AS Work2,
            WANNABE
            FROM x2
            WHERE x2.Row = 1
            AND RTRIM(LTRIM(x2.Col1)) IN ('', '2')
MySQL CASE Statement with multiple columns in statement list

MySQL CASE Statement with multiple columns in statement list


By : dharmendra singh
Date : March 29 2020, 07:55 AM
this one helps. I have this query, which I know doesn't work, but I've left it as it is as pseudo-code to help explain what I'm doing. I'm trying to get "Booking" and "Sales" totals from a Booking table by day-of-the-week for the past week. Hence, Mon1B = Bookings for Monday and Mon1S = Sales for Monday. , Yes, but case can only return one value. You can do it like this:
code :
SELECT sum(CASE when WEEKDAY(b.created) = 0 then 1 else 0 end) as Mon1b,
       sum(case when weekday(b.created) = 0 then amount else 0 end) as Mon1S,
       ...
FROM Bookings b
WHERE b.created > '#week1Start#' and b.created <= '#week1End#'
select WEEKDAY(b.created), count(*) as cnt, sum(amount) as amt
from Bookings b
WHERE b.created > '#week1Start#' and b.created <= '#week1End#'
group by WEEKDAY(b.created)
order by 1
Is there possible to use in CASE statement in WHEN more columns?

Is there possible to use in CASE statement in WHEN more columns?


By : user3421476
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You have mixed the two different syntax for CASE expressions.
You could do it as:
code :
CASE
WHEN column_01 = 5 AND column_02 = 'NO' THEN
  value
ELSE
  value_other
   CASE [ expression ]

   WHEN condition_1 THEN result_1
   WHEN condition_2 THEN result_2
   ...
   WHEN condition_n THEN result_n

   ELSE result

   END
CASE 

   WHEN expression condition_1 THEN result_1
   WHEN expression condition_2 THEN result_2
   ...
   WHEN expression condition_n THEN result_n

   ELSE result

END 
Using a case statement to show certain columns in select statement

Using a case statement to show certain columns in select statement


By : Walter Otieno
Date : March 29 2020, 07:55 AM
help you fix your problem A SQL query returns a well-defined set of columns. You cannot change the number, types, or names of the columns, although you can change the contents.
CASE is an expression that returns a single value, which can be a column in the result set.
R Case statement using both and or in the same statement - between Dates, some with missing values over multiple columns

R Case statement using both and or in the same statement - between Dates, some with missing values over multiple columns


By : Chris Martinus
Date : March 29 2020, 07:55 AM
To fix the issue you can do I'm trying to see whether or not a program participant was active on the beginning of each month. , Using dynamic variable creation of dplyr:
code :
library(dplyr) # version 0.6 and above
library(lubridate)

df <- df %>% 
  select(Admission, Discharge) %>% 
  mutate_all(mdy) # convert the columns to date format

# Start dates of the months and respective month names
my_months <- ymd("2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01")
month_names <- month(my_months, label=T) %>% as.character()


# Looping through the months and dynamic creation of month columns
for (i in seq(length(my_months))){
  df <- df %>%  
    mutate(!!month_names[i] := ifelse(my_months[i] > Admission &
                                        (my_months[i] < Discharge | is.na(Discharge)), 
                                        "Active", "NotActive"))
}
Related Posts Related Posts :
  • error even after following Microsoft format in SQL
  • Increase price by percentage, results rounded
  • Can we Use "Case" in a ColdFusion Query-of-Query
  • displaying 'Active' for a row which has 'A' as value
  • Using pattern search in SQL Server Query
  • SQL - how to group transactions for a certain date pattern?
  • How to Index SQL with multiple AND conditions nested inside OR
  • SQL rank with priority
  • Does BigQuery support SubQueries?
  • Any ideas for persisting H2 Database In-Memory mode transaction?
  • How to improve SQL query performance (correlated subqueries)?
  • Filter repeated columns in Oracle
  • SQL Server join on whichever column has value populated
  • use sql stored procedure to insert data which is returned from a query stored in a table
  • Query not working1
  • SQL - using an alias in a where clause in a subquery
  • How to search for a whole word with special char using oracle contains?
  • Production Perfomance of using SELECT * statements?
  • String concatenation not working as expected in SELECT statement
  • How to detect which stored procedures UPDATE or INSERT into a certain TABLE?
  • Select statement inside CASE - SQL
  • .Net Float error 4 - float(4) = 4.44089209850063E-16
  • How to show as column to recursive rows in a table With SQL Server
  • SQL Server error: Column name or number of supplied values does not match table definition
  • How to find a table name which's column name consist cl_
  • Cannot access existing table from WCF Code
  • SQL CREATE VIEW Error
  • Many Statement in the same transaction PLSQL
  • Tables are not joining properly in sql server 2008
  • DB2 how to sum two column from two different table
  • SQL msAccess: list unfinished courses of a student
  • SQL Server delete multiple rows
  • Finding max(date) from the resultant query output
  • updating multiple columns using case statement in sql server
  • Nested SQL query having select subquery in update statement
  • "putting" a certain object to the screen from inside an array
  • Retrieve segment from value
  • Error while creating a table in oracle 11g
  • Transaction with Sequelize doesn't work
  • Search time complexity of this sql query
  • How to return unique records between two tables without using distinct and union?
  • Is it possible to maximum occurences of a value for each key in my table all in one query?
  • Divide table into groups of 5000 based on rank
  • rolling back exec and sp_OA methods
  • How to convert multiple rows to one row in SQL Server?
  • How to use substring and charindex in google query language
  • Find table information where part of column matches char variable
  • Need to retrieve rows from table where the following condition was not satisfied column A = column B and column B = colu
  • Assigning an index to a constraint
  • Retrieve records from multiple Records returned by Sub-Query
  • SQL DELETE group of records based on opposite group being empty
  • SQL GetDate() returns wrong time
  • Total sales amount for customers in cities
  • SQL Server -- updating the `sys.*` tables and not just reading them
  • SQL Order By using concat
  • SQL server 2008 - subquery returned more than one value
  • Group Data Hourly and Insert Into a Summary Table in Postgres
  • Execute SQL Server Pass-Through Query From Access VBA
  • MS Access SQL Transform Aggregate Manipluation of Values for Pivot
  • SQL "if exists..." dynamic query
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org