I have table named boolean which contain 'true' and/or 'false' values as strings in one column.
I have problem to create case statement to show me whether there are only 'true' or 'false' or 'both' values
Example 1:
'true'
'true'
result:'true'
Example 2:
'false'
'false'
'false'
Result: 'false'
Example 3:
'true'
'false'
'true'
Result: 'both'
Edit:
case statement should look like:
case
when "column content are only true values" then 'true'
when "column content are only false values" then 'false'
else 'both'
end
You could aggregate the max and min of the column, and then evaluate the results - if they are the same, there's only one value in the column. If not, there must be both. Note that since these are string representations the values are sorted lexicographically:
SELECT CASE WHEN MAX(col) = MIN(col) THEN MAX(col) ELSE 'both' END
FROM my_table
SELECT CASE WHEN MIN(Col) <> MAX(Col) THEN
'Both'
ELSE
MIN(Col)
END
FROM YourTable
select case when count(distinct bool_column) = 2 then 'both'
when sum(case when bool_column = 'false' then 1 end) > 0 then 'false'
else 'true'
end as result
from your_table
Related
I do a CASE WHEN in my SELECT to convert any null into 'None'. I also want to take a specific non-null value (e.g. 'X') and treat this the same as the null values (e.g. make them all 'None').
I do something like this:
SELECT
CASE WHEN Val = 'X' THEN 'None' ELSE NVL(Val, 'None') END ValCol
FROM tbl
GROUP BY Val
What I end up with is two rows of 'None'; in essence the two 'None' values are being treated separately.
How do I combine them?
The problem is the GROUP BY. You are grouping by Val, so all rows with Val as X are grouped together and all rows where Val IS NULL are grouped together. This actually results in 2 rows, but are displayed with the same value because of your CASE expression.
You can solve this by grouping with your CASE instead of Val:
SELECT
CASE WHEN Val = 'X' THEN 'None' ELSE NVL(Val, 'None') END ValCol
FROM
tbl
GROUP BY
CASE WHEN Val = 'X' THEN 'None' ELSE NVL(Val, 'None') END
As side note, I find the following CASE a little more expressive:
CASE WHEN Val = 'X' OR Val IS NULL THEN 'None' ELSE Val END
Try this:
SELECT
CASE WHEN Val = 'X' THEN 'None' ELSE NVL(Val, 'None') END ValCol
FROM tbl
GROUP BY (CASE WHEN Val = 'X' THEN 'None' ELSE NVL(Val, 'None') END)
You can do this by using the expression instead of the column in the GROUP BY clause like this:
SELECT CASE WHEN Val = 'X' THEN 'None' ELSE NVL(Val, 'None') END ValCol
FROM tbl
GROUP
BY CASE WHEN Val = 'X' THEN 'None' ELSE NVL(Val, 'None') END
A second way is to use a sub select for the CASE and to group in an outer select
SELECT ValCol
FROM(SELECT CASE WHEN Val = 'X' THEN 'None' ELSE NVL(Val, 'None') END ValCol
FROM tbl
)
GROUP
BY ValCOL
I would prefer the second way because it's more readable.
I am trying to apply two conditions in one SQL Query.
(select DISTINCT (
CASE WHEN (
ABC.GemUserID = '99' )
OR ABC.GemUserID != '99'
THEN 'Yes'
ELSE 'No'
END)) AS AllWell
This gives me output as "Yes" where as the CASE is true only for 1 file like below :
Current Result:
99 , Yes
99 , Yes
99 , Yes
Expected Result:
99 , No
99 , No
99 , Yes
I am using the below query but the SQL Query Intellisence is identifying it as wrong.
Wrong Query:
(select DISTINCT (
CASE WHEN ( ABC.GEMUserID = '99' THEN 'Yes' else 'No'
CASE WHEN ( ABC.GEMUserID != '99' THEN 'No' else 'Yes'
END)) AS AllWell
After fixing the above Wrong Query:
(select DISTINCT
(CASE WHEN ABC.GemUserID = '99' THEN 'Yes' else 'No' END),
(CASE WHEN ABC.GemUserID != '99' THEN 'No' else 'Yes' END))
AS AllWell
But I am getting error:
Msg 116, Level 16, State 1, Line 17 Only one expression can be
specified in the select list when the subquery is not introduced with
EXISTS.
How to fix this?
select distinct is -- itself -- part of SQL syntax. The distinct is not a function. It should not be followed by parentheses. So, if I understand your question:
select DISTINCT
( CASE WHEN ABC.GEMUserID = '99' THEN 'Yes' else 'No' END),
( CASE WHEN ABC.GEMUserID <> '99' THEN 'No' else 'Yes' END) as AllWell
Do you plan on giving the first column a name?
select DISTINCT
CASE WHEN ABC.GEMUserID = '99' THEN 'Yes'
ELSE 'No' -- This is automatically When ABC.GEMUserID <> '99'
END AS AllWell
According to the error, your query is a subquery (probably behind IN?) in a larger SQL command. Therefore, it is not possible for such subquery to return more than one column.
So your first query, you've said:
CASE WHEN userID = 99 OR userID != 99
In other words:
CASE WHEN 1=1
This is why it returns yes for everything (not sure what the difference between your current and expected result should be considering that the userID is 99 for all rows).
For your erroneous query, seems you're returning that select in the middle of another select (since you alias it at the end). Due to that, you cannot return more than one column in your nested select. You do not need the second CASE statement, simply change your query to:
(select DISTINCT
CASE WHEN ABC.GemUserID = '99' THEN 'Yes' Else 'No' End) AS AllWell
Assuming that you hold the missing pieces to the query such as the FROM.
i have table1 that have a "status" field of type bit
i wanna get true if all rows have status=1(true)
how to do this?
If I get your question correctly, this can help you. You want true if all rows have status equal to 1.
IF EXISTS (SELECT 1 FROM Table1 WHERE status <> 1)
SELECT 'false'
ELSE
SELECT 'true'
select case when SUM(case when status=1 then 1 ELSE 0 end) = COUNT(*) then 'true'
else 'false' end
from tab
with the help of #Vicky_Thinking's answer i wrote this:
select isnull((SELECT top 1 status FROM Table1 WHERE status <> 1),1)
result is 1 if all rows have status=1 and otherwise result is 0
I've a table in my database for which I need to check if all rows have one field not null.
If there are no row or if there is at least 1 row with the field null => true
If there are rows and they are all with the field not null => False
Is there a way to do this in on simple query? Or I need to check if my table is empty first then if it's not check if I've a row with the field value empty ?
This will count how many NULL values you have in a field;
SELECT
SUM(CASE WHEN FieldName IS NULL THEN 1 ELSE 0 END) NullValues
FROM TableName
Will return 0 if there are no NULL values, and will return the number of NULLS if there are any present.
If you actually want to return a value as 'True' or 'False' then do this;
SELECT CASE
WHEN a.NullValues > 0
THEN 'True'
ELSE 'False'
END CheckField
FROM (
SELECT
SUM(CASE WHEN FieldName IS NULL
THEN 1
ELSE 0
END) NullValues
FROM TableName
) a
Use count(*) and count(field) and compare the two:
select
case when count(*) > 0 and count(*) = count(field) then 1 -- not empty and no nulls
else 0 end as isgood
from mytable;
Oracle SQL has no boolean data type , so I use 1 for true and 0 for false. You can replace this with whatever you like (e.g. 'true' instead of 1 and 'false' instead of 0).
As to turning this into a predicate (correlated to a main query), you'd use something along the lines of:
select ...
from main
where exists
(
select 1
from mytable
where mytable.colx = main.coly
having count(*) > 0 and count(*) = count(field)
);
You can do this with aggregation. However, it is difficult to understand what you are asking for. If you want to check that a field has no NULL values, you can do:
select (case when count(*) > 0 then 1 else 0 end) as HasNullValues
from t
where field is null;
Alternate way I found using max with putting null first:
select case when
max(field) keep (dense_rank first order by datfin desc nulls first) is null then 1
else 0 end as flag
from MYTABLE;
I have the following problem:
I have a select statement that includes a case part. Up til there it is easy the problem is that the case includes a check against another field in the same table.
select h.id,
case h.value
when 'P' then 'test'
when '' then 'failed'
when 'D' then 'passed'
else null end
as info,
b.text,
case h.diag
when h.value = '' [or 'failed' not sure tried both and didn't work]
else h.diag end
as diag1, h.date from valuetab h, texttab b where h.id=b.id
I want to have h.diag only to show values when h.value is not failed.
I always get the mistake that the = should be concat.. but that doesn't make sense in my eyes.
Any ideas??
Thats for all your help.
You can also write a case statement with your expression in a different place i.e.
SELECT CASE WHEN X = 1 THEN 'Y' WHEN X = 2 THEN 'Z'
I think what you want to do is something more like this:
SELECT CASE WHEN h.value = '' THEN h.diag end
Use the other form of case statement, which doesn't specify a column you want to look at:
select case
when column1 = 2 then 'Foo'
when other_column = 'blah' then 'Bar'
end
from table
The problem with using case column1 when... is that it implicitly compares column1 to each when clause. You can't then include a comparison to some other column in it.
You are missing a THEN portion of the WHEN clause, and specifying a condition where you could specify a value:
case h.value
when '' THEN NULL
else h.diag end
Ok got it....
after the 2nd case the "h.diag" must be removed....
so it is
case
when h.value = '' then null
else h.diag end
as diag1,