Case statement for join condition - sql

Hi I want to use case statement in where condition or some similar logic.
I want to ignore where condition when there are no rows in tmp_collaboration table and use the condition when table has some rows in it.
Select clbid from tmp_collaboration;
select customer_id, product_id ,clbid
from customers c
where hdr_id = 10
and clbid in (select clbid from tmp_collaboration)
and status = 'y';

Is this what you want?
select customer_id, product_id ,clbid
from customers c
where hdr_id = 10 and status = 'y' and
(clbid in (select clbid from tmp_collaboration) or
not exists (select 1 from tmp_collaboration)
);

why not use a JOIN .. if there are rows in table the rows are involved otherwise not .
select customer_id, product_id ,clbid
from customers c
INNER JOIN tmp_collaboration t on t.clbid = c.clbid
AND hdr_id = 10
AND status = 'y';

Related

Return only one row based on search

Query
select
a.id,
a.ba,
b.status,
b.custid
from balist as a
inner join customer as b
on a.ba = b.ba
I have a table "balist" that has a list of (ba) and i inner join table "customer" on (ba) and right now by output is like the following
id
ba
status
custid
1
ba-1234455
A
123-321-123-321a
2
ba-1234455
I
123-321-123-321a
3
ba-1234457
A
123-321-123-321b
4
ba-1234458
A
123-321-123-321c
5
ba-1234459
I
123-321-123-321d
and I want to return all A and I status but remove the row that has status I that also have a A status. Like the following.
I have a table customer like the following
id
ba
status
custid
1
ba-1234455
A
123-321-123-321a
3
ba-1234457
A
123-321-123-321b
4
ba-1234458
A
123-321-123-321c
5
ba-1234459
I
123-321-123-321d
You could use a row_number() to filter your resulting rows eg
SELECT
id,ba,status,custid
FROM (
SELECT
a.id,
a.ba,
b.status,
b.custid,
ROW_NUMBER() OVER (
PARTITION BY a.ba
ORDER BY b.status ASC
) as rn
FROM
balist as a
INNER JOIN
customer as b ON a.ba = b.ba
)
WHERE rn=1
Let me know if this works for you.

Checking 2 tables for identical IDs and returning 1/0 in column if ID in table 1 exists in table 2

I have 2 tables, 1 containing all reservation ids, and 1 containing reservation ids for livestream reservations. I am trying to write a query that checks to see if a reservation id exists in the livestream table, and returns '1' if true & '0' if false. I figure the best way to do this is with a case statement that returns my result if the reservation id exists in the livestream table, but I am running into issues. Is there a better way to do this?
with table_name as(
select
reservation_id
from all_reservations
)
select t.*,
case when exists(l.reservation_id)
then '1'
else '0' end as is_livestream
from livestream_reservations l
left join table name t
on l.reservation_id = t.reservation_id
So long as reservation_id shows up with at most one record in livestream_reservations, this will work for you:
select r.*,
case
when l.reservation_id is null then 0
else 1
end as is_livestream
from reservations r
left join livestream_reservations l
on l.reservation_id = r.reservation_id;
The case relies on the fact that a failure to join to livestream_reservations returns null in all columns from that table.
In case there may be more than one row with the same reservation_id in the livestream_reservations table, then you could do this:
with ls_count as (
select reservation_id, count(*) as count_livestream
from livestream_reservations
group by reservation_id
)
select r.*, coalesce(lc.count_livestream, 0) as count_livestream
from reservations r
left join ls_count lc on lc.reservation_id = r.reservation_id;
I would recommend exists and using booleans:
select r.*,
(exists (select 1 from livestream_reservations lr where lr.reservation_id = r. reservation_id)
) as is_livestream
from reservations r;
There is a good chance that this is faster than other solutions. More importantly, it avoids problems with duplicates in livestream_reservations.

SQL Query – records within the SQL Select statement, but NOT in the table being queried

I have a large list of CustIDs that I need to query on to find if they are within the CUSTOMER table; I want the result to tell me which CustIDs ARE on the table and which CustIDs are NOT on the table.
I provided a short list below to give an idea of what I need to do.
Oracle database
Table: Customer
Primary Key: CustID
Scenario:
Customer table only has the following (2) CustID: ‘12345’, ‘56789’
Sql:
Select * from CUSTOMERS where CUSTID in (‘12345’, ‘56789’, ‘01234’);
I want the result to tell me that both ‘12345’ and ‘56789’ are in the table, AND that ‘01234’ is NOT.
select
v.CustID,
exists (select * from Customer where Customer.CustID = v.CustID)
from (values (12345), (56789), (01234)) v (CustID);
Results:
custid exists
12345 true
56789 true
1234 false
You need a left join or subquery for this. The precise syntax varies by database. Typical syntax is:
select i.custid,
(case when c.custid is not null then 1 else 0 end) as exists_flag
from (select '12345' as custid union all
select '56789' union all
select '01234'
) ci left join
customers c
on c.cust = i.custid;

SQL Query Exclude Records

I want to query a database of guests that bought certain items. I want to see what customers bought item 'A' but not item 'B'.
I tried:
SELECT customerName
FROM Customers
WHERE NOT item = 'A' AND item = 'B';
But I return customers that bought both items. I would like to exclude these customers from that query.
I am using SQLite
There are multiple ways to do this. I like to use group by and having, because it is very flexible for many conditions:
SELECT customerName
FROM Customers
GROUP BY customerName
HAVING SUM(CASE WHEN item = 'A' THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN item = 'B' THEN 1 ELSE 0 END) = 0;
You can also use the MINUS operator which returns all rows in the first SELECT statement that are not returned by the second SELECT statement. Such as:
(SELECT customerName FROM Customers WHERE item='A')
MINUS
(SELECT customerName FROM Customers WHERE item='B');
I would use EXISTS with NOT EXISTS:
select c.*
from Customers c
where exists (select 1
from Customers c1
where c1.customerName = c.customerName and c1.item = 'A'
) and not exists
(select 1
from Customers c2
where c2.customerName = c.customerName and c2.item = 'B'
);

selecting qualified records from a group

There are 2 tables. 1 is the primary table and the other has a 1 to many relationship with the primary table. The table with the many relationship has a field that is being used as a flag. So it usually have a value of Y or null.
So I'd like to select rows from the primary table that has records in the 2nd table only if all the rows on the 2nd table has a Y in its field. In other wards, if the Y field has a null or some other value, they don't qualify.
So the logic is if all rows have a Y than, OK, if some rows have yes and some do not have Y, than they don't qualify in the selection.
Hope that's clear.
Thank you.
Something like this should do the trick...
SELECT p.Field
FROM PrimaryTable p
WHERE NOT EXISTS(SELECT * FROM SecondaryTable s WHERE p.ID = s.ID AND (s.Flag <> 'Y' OR s.Flag IS NULL))
I've assumed that either there will always be a record in SecondaryTable for a given PrimaryTable record....OR, if not, then you still want the primary record returned.
SELECT
p.*
FROM
PrimaryTable p,
( select s.ID,
count(*) AllRecs,
sum( case when S.Flag = 'Y' then 1 else 0 end ) as YesRecCount
from
SecondaryTable s
group by
s.ID
having
AllRecs = YesRecCount ) squalified
WHERE
p.ID = squalified.ID
select p.*
from primary_table p
inner join secondary_table s
on p.join_key = s.join_key
and s.flag = 'Y'
where not exists(select null from secondary_table s2 where s2.join_key = p.join_key and (s2.flag is null or s2.flag <> 'Y'))