Get "And" operation on a field in SQL Server - sql

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

Related

How to check all records have some value in oracle query?

I have query to check some exists like:
SELECT CASE WHEN (exists (select * from "Customer" where length(CustomerID) >
0) ) then 1 else 0 end val from dual
How do I know all records of Customer table have length of the field(CustomerID) bigger than 3. If all records have value bigger than 3 then 1 or 0 .
Thanks in advance.
Joon
Something like this?
SELECT CASE WHEN MIN(LENGTH(CustomerID)) > 3 THEN 1 ELSE 0 END AS Val
FROM Customer;
Try this.
SELECT CASE
WHEN COUNT(CASE
WHEN LENGTH(CustomerID) > 3
THEN 1
END) = COUNT(*)
THEN 1
ELSE 0
END as res
FROM Customer;
Demo

How to check if all rows validate a predicate

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;

Case SQL If there is no record between dates

I'm new to SQL and am struggling with a case.
I would like to make the case where if an account (account_ID) doesn't have a record (ON billing_id) between current_date-302 and current_date-62 THEN MARK WITH A "1"
Query below:
Thanks in advance
SELECT
billing_date_local_time
,account_id
,contract_owner_name
,date_first_feature_partner
,deal_starts_at
,contract_id
,new_partner_type
,sum(voucher_sold) AS Vouchers
,sum(gross_bookings_local) AS GB
,sum(gross_revenue_local) AS GR
,is_G2
,Case when billing_date_local_time between current_date-302 and current_date-62 = 0 THEN 'YES' ELSE 'NO' End
FROM EMEA_ANALYTICS.eu_deal_flat
WHERE
country_id = 206
and billing_date_local_time between current_date-400
and current_date-2
GROUP BY 1,2,3,4,5,6,10,11
You'll need to do a correlated subquery; something like this:
select
a.billing_date_local_time
,a.account_id
,...
, CASE WHEN EXISTS (SELECT * FROM EMEA_ANALYTICS.eu_deal_flat b WHERE a.account_id = b.account_id AND b.billing_date_local_time between current_date-302 and current_date-62 ) THEN 'YES' ELSE 'NO' END
from
FROM EMEA_ANALYTICS.eu_deal_flat a
WHERE ...
You need to apply an aggregate function like this:
min(case when billing_date_local_time
between current_date-302 and current_date-62
then 0
else 1
end)

SQL to Return 0 instead of NULL

Trying to return 0 instead of NULL in the following select without success:
SELECT
IsNull(CASE WHEN TICKET IS NOT NULL THEN 1 ELSE 0 END,0) AS TICKET
FROM
TICKETS
WHERE
TICKET = '2'
Any suggestions? Thanks.
After reviewing the answers and your answer to them, I think what you want is: If no data returned, select 0 else select 1.. so:
SELECT case when exists(select 1 from tickets where ticket = '2')
then 1 else 0 end as IND
Use the CASE expression:
SELECT (CASE WHEN TICKET IS NOT NULL THEN 1 ELSE 0 END) AS TICKET
FROM TICKETS
WHERE TICKET = '2'
However, it makes no sense, because the value cannot be NULL given the WHERE clause. So, you can just do:
SELECT 1
FROM TICKETS
WHERE TICKET = '2';

If at least one record has value 1 then return 'Yes'

I have a table in SQL Server that contains a column yesno.
If at least one of the rows has the column yesno=1 then I need to return only one row yes.
I made a query that returns for every row if is yes or no.
(select (case when isnull(coalesce(dl.yesno,'2'),'2')='1' then 'Yes' else 'NO' END)
from table dl where dl.ID='A5454322-C239-4FF2-A458-8A9BD79C1839')
select 'yes'
where exists (select 1 from the_table where yesno = '1');
SQLfiddle example: http://sqlfiddle.com/#!3/069204/1
select 'yes' where exists (select * from MyTable where [yesno]='1')
select top 1 'yes' from table where yesno = 1