CASE syntax inside a WHERE clause with IN on SQL Server - sql

How can I make following query work? This is inside stored procedure #GetClosedMeetings bit parameter passed true to get the meetings which are finished, otherwise get planned and current meetings. (Status: 0 - Planned, 1 - Current, 2 - closed)
SELECT
MeetingId, Status
FROM
dbo.Meeting
WHERE
Status IN (CASE
WHEN #GetClosedMeetings = 1 THEN 1
ELSE #OtherStatuses)

If I understand your requirement correctly, if #GetClosedMeetings is TRUE, you want to return Meetings that are finished, which from your question seems to have a STATUS of 2. On the other hand, if #GetClosedMeetings is FALSE, then you want to get planned and current meetings. Here is one way to achieve this:
SELECT MeetingId, Status
FROM dbo.Meeting
WHERE
(#GetClosedMeetings = 1 AND Status = 2)
OR (#GetClosedMeetings = 0 AND Status IN(0, 1))

use this :
SELECT MeetingId, Status
FROM dbo.Meeting
WHERE (Status = 1 And #GetClosedMeetings = 1)
Or (Status <> 1 And #GetClosedMeetings <> 1 )

You missed end in case
SELECT MeetingId, Status
FROM dbo.Meeting
WHERE Status IN
(CASE
WHEN #GetClosedMeetings = 1 THEN 1
ELSE #OtherStatuses end)

Related

Get table rows by multiple/end column value status

I have a table with example data like below:
InquiryId status
-------------------
inquiry1 New
inquiry1 Started
inquiry1 Done
inquiry2 New
inquiry2 Pending
inquiry3 New
inquiry3 Started
inquiry4 New
inquiry4 Cancelled
..and so on
Each inquiry starts with status as New and can reach either of the end status'es (Done, Cancelled..) via the middle status'es (Started, Pending..)
The question is how do find the list of inquiries that started with New but never reached the end status, i.e. what SQL query needs to be written to get the list of uncompleted inquiries.
P.S: Using Sybase Database
You can user the following query:
select distinct InquiryId
from <table_name>
where status not in('Done','Cancelled');
It will return all all the InquiryId which doesn't have the status as Done or Cancelled and is still in intermediate state.
You can use not exists :
select t.*
from table t
where status = 'New' and
not exists (select 1
from table t1
where t1.InquiryId = t.InquiryId and
t1.status in ('Done', 'Cancelled')
);
I would use aggregation:
select inquiryId
from t
group by inquiryId
having sum(case when status = 'New' then 1 else 0 end) > 0 and
sum(case when status in ('Done', 'Cancelled') then 1 else 0 end) = 0;
The conditions in the having clause count the number of rows for each inquiryId that meet the given conditions. The > 0 says that the inquiryId has at least one row. The = 0 says that the inquiryId has more than one row.

Get "And" operation on a field in SQL Server

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

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';

Oracle SQL CASE expression in WHERE clause only when conditions are met

Cant quite figure this one out, i have a set of conditions that i want to be met only if a value is in a field.
So if the Status is complete i want to have three where clause's, if the status doesn't equal complete then i don't want any where clause.
Code
SELECT *
FROM mytable
WHERE CASE WHEN Status = 'Complete'
THEN (included = 0 OR excluded = 0 OR number IS NOT NULL)
ELSE *Do nothing*
It is usually simple to only use boolean expressions in the WHERE. So:
WHERE (Status <> 'Complete') OR
(included = 0 OR excluded = 0 OR number IS NOT NULL)
If Status could be NULL:
WHERE (Status <> 'Complete' OR Status IS NULL) OR
(included = 0 OR excluded = 0 OR number IS NOT NULL)
You can translate the natural language in SQL, then, if possible, reformulate.
SELECT *
FROM mytable
WHERE (Status = 'Complete' and (included = 0 OR excluded = 0 OR number IS NOT NULL))
or status <> 'Complete'
or status IS NULL;
It doesn't look like you really need a CASE statement, just use it like this:
SELECT *
FROM mytable
WHERE where (Status = 'Complete' and (included = 0 OR excluded = 0 OR number IS NOT NULL)) or (*Your do nothing*)

How can I write this select query in SQL Server?

I need to extract some data to analyse exceptions/logs, and I'm stuck at a point.
I have a table with a column called CallType, and a status which can be Success or Failure. This table also has a column called SessionId.
I need to do this:
Select all the SessionId's where all the CallType = 'A' are marked as Success, but there is at least one CallType = 'B' having a Failure for that session.
There will be a where clause to filter out some stuff.
I'm thinking something like:
select top 10 *
from Log nolock
where ProviderId=48 -- add more conditions here
group by SessionId
having --? what should go over here?
I would do this with conditional aggregation in the having clause:
select top 10 *
from Log nolock
where ProviderId=48 -- add more conditions here
group by SessionId
having sum(case when CallType = 'A' and Status = 'Failure' then 1 else 0 end) = 0 and
sum(case when CallType = 'B' and Status = 'Failure' then 1 else 0 end) > 0 and
sum(case when CallType = 'A' and Status = 'Success' then 1 else 0 end) > 0;
The having clause checks for three conditions by counting the number of rows that meet each one. If = 0, then no records are allowed. If > 0 then records are required.
That CallType A has no failures.
That CallType B has at least one failure.
That at least one CallType A success exists.
The third condition is ambiguous -- if is not clear if you actually need CallType As to be in the data, based on the question.
SELECT *
FROM Log L WITH(NOLOCK)
WHERE L.CallType='A'
AND L.[Status] = 'Success'
AND L.ProviderId = 48
AND EXISTS (SELECT 1
FROM Log
WHERE L.SessionID = SessionID
AND CallType='B'
AND [Status] = 'Failure')
Having clause can only operate on aggregates within the group so this isn't the correct way to go about it since you are filtering out other rows you want to check against. I'd use EXISTS for this e.g.
edit: corrected the query
SELECT *
FROM Log L WITH(NOLOCK)
WHERE ProviderId = 48
AND CallType = 'A'
AND Status = 'Success'
AND EXISTS(SELECT * FROM Log WHERE L.SessionId = SessionId AND CallType = 'B' AND Status = 'Failure')
You can essentially filter out rows in the EXISTS part of the query using the aliased Log table (aliased L), matching all rows with the same session ID and seeing if any match the filters you required (failed with call type B)