complex query join checking that value does not exist - tsql

I am struggling with a great challenge of a query. I have two tables, first has
Tb1
drID
schedDate
rteID
Second has:
Tb2
drID
FName
LName
Active
Tb1 drID must be checked for Null or blank and match on schedDate and drID can not have any values that match Tb2.drID for date selected, checking for Null and '' essentially do this.
SELECT drID, schedDate, rteID
FROM Tb1
WHERE (drID IS NULL OR drID = '') AND (schedDate = 11 / 1 / 2012)
From all of this I need to return from TB2 drID, Fname, LName Where Active = True and drID does not exist on any record in tb1 for the schedDate selected.
There are many tb1 rteID records for any possible date.
Thank you for any help on this and huge Holiday Thank You.

Can you make your select statement a subquery for example:
SELECT drID, Fname, LName
FROM TB2
WHERE Active = True
AND drID NOT IN (
SELECT drID
FROM Tb1
WHERE (drID IS NULL OR drID = '')
AND (schedDate = 11 / 1 / 2012)
)
Edit
To handle the case that the schedDate is null then
SELECT drID, Fname, LName
FROM TB2
WHERE Active = True
AND drID NOT IN (
SELECT drID
FROM Tb1
WHERE (drID IS NULL OR drID = '')
AND (schedDate = #yourDate OR schedDate IS NULL)
)
Edit 2
To handle the case that the drID is null then you can use the NOT EXISTS approach as highlighted in this SO post about NOT IN vs NOT EXISTS
SELECT drID, Fname, LName
FROM TB2
WHERE Active = True
AND NOT EXISTS (
SELECT drID
FROM Tb1
WHERE (schedDate = #yourDate)
AND Tb1.drID = TB2.drID
)

Related

Handle multiple resultset from THEN Statement in postgresql

SELECT
CASE WHEN a.text IS NOT NULL THEN a.text
WHEN a.number IS NOT NULL THEN a.number::text
WHEN a.id IS NOT NULL THEN (Select b.text from table_two b
Join table_one a ON b.id = a.id where b.place = 'city' AND a.roll = '20' )
ELSE NULL
END
FROM
table_one a
WHERE
a.roll = '20';
above query returns error as
ERROR: more than one row returned by a subquery used as an expression
Is there any way to handle the multiple rows returned from subquery. Expecting multiple resultset from THEN statement
SELECT
CASE WHEN a.text IS NOT NULL THEN a.text
WHEN a.number IS NOT NULL THEN a.number::text
WHEN a.id IS NOT NULL THEN a.id IN (Select b.text from table_two b
Join table_one a ON b.id = a.id where b.place = 'city' AND a.roll = '20' )
ELSE NULL
END
FROM
table_one a
WHERE
a.roll = '20';
If I try to handle multiple result using IN, then I get error
"ERROR: operator does not exist: integer = character varying
because a.id is integer and sub query returns string results

If the object we made by the group is null, I want to give [ ] instead postgresql

with
zakaz as (
select
o.*,
(select f_name from clients where id = o.client_id) as f_name,
(select l_name from clients where id = o.client_id) as l_name,
(select phone from clients where id = o.client_id) as phone,
(select name from item_types where id = o.item_type_id) as item_name,
(select name from trailer_types where id = o.trailer_type_id) as trailer_name,
(select name from cover_types where id = o.cover_type_id) as cover_name,
(select name from cities where id = o.from_city_id) as from_city,
(select name from cities where id = o.to_city_id) as to_city,
(select name from transport_types where id = o.transport_type_id) as transport_type,
(select first_name || ' ' || last_name || ' ' || middle_name as name from workers where id = o.logist_id) as logist_name
from orders as o
where o.transport_type_id = (select id from transport_types where name = $tt$${transport_type}$tt$)
${ where_key ? `and ${where_key} = $v$${where_val}$v$` : ``}
order by o.created_at
),
zakaz_j_agg as (
select
COALESCE(json_agg(zakaz.*), '[]') as array
from zakaz
group by zakaz.status
)
select
json_agg(ord.*) as result
from zakaz_j_agg as ord
Replace json_agg(ord.*) as result with coalesce(json_agg(ord.*), '[]') as result. The same pattern is used in the zakaz_j_agg CTE.
with
zakaz as (... your CTE query ...),
zakaz_j_agg as (... your CTE query ...)
select
coalesce(json_agg(ord.*), '[]') as result
from zakaz_j_agg as ord;

postgres how to insert values with 2 selects

I'm trying to do a query on Postgres but it's not working. I'd like to create an insert query with 2 select:
Example :
INSERT INTO table1 (id_1, id_2)
SELECT id from table_2 where code='01',
SELECT id from table_2 where code='02';
I don't find the good syntax for this.
I believe below query will works for your use case
INSERT INTO stats(totalProduct, totalCustomer, totalOrder)
VALUES(
(SELECT COUNT(*) FROM products),
(SELECT COUNT(*) FROM customers),
(SELECT COUNT(*) FROM orders)
);
you can changes query accordingly
You can add one more SELECT to achieve this
INSERT INTO table_1 (id_1, id_2)
SELECT
(SELECT id FROM table_2 WHERE code = '01') AS Id_1,
(SELECT id FROM table_2 WHERE code = '02') AS Id_2;
Or you may try with CASE expression:
INSERT INTO table1 (id_1, id_2)
SELECT MAX(CASE WHEN code = '01' THEN id ELSE 0 END) AS Id_1,
MAX(CASE WHEN code = '02' THEN id ELSE 0 END) AS Id_2
FROM table_2
Please refer to the working fiddle on db<>fiddle

Delete duplicate rows with Active flag false in Postgresql

I have a table with columns "ID", "Name" , "Email" , "Active". I added some duplicate values to the table.
I want to delete duplicate rows with flag false not all rows with active flag false. In the table I want to delete 2nd row only.
You may try below co-related sub-query (Before actual delete, You might want to to see the result using SELECT query)-
DELETE FROM YOUR_TABLE T1
WHERE EXISTS (SELECT NULL
FROM YOUR_TABLE T2
WHERE T1.ID = T2.ID
AND T1.NAME = T2.NAME
AND T1.EMAIL = T2.EMAIL
AND T1.ACTIVE <> T2.ACTIVE)
AND UPPER(T1.ACTIVE) = 'FALSE'
Try the below query:
DELETE t1 FROM tablename t1
INNER JOIN tablename t2
WHERE t1.id > t2.id AND t1.Name = t2.Name AND t1.Email =t2.Email AND t1.Active='FALSE'
DELETE FROM users T1
USING users T2
WHERE T1.ID <> T2.ID
AND T1.Name = T2.Name
AND T1.Email = T2.Email
AND T1.Active = FALSE;
DEMO
CREATE TABLE IF NOT EXISTS users (
ID serial PRIMARY KEY,
Name VARCHAR ( 50 ) NOT NULL,
Email VARCHAR ( 50 ) NOT NULL,
Active BOOLEAN NOT NULL
);
INSERT INTO users(Name, Email, Active) VALUES
('John', 'john.gmail.com', TRUE),
('John', 'john.gmail.com', FALSE),
('Bob', 'bob.gmail.com', FALSE);
SELECT * FROM users;
DELETE FROM users T1
USING users T2
WHERE T1.ID <> T2.ID
AND T1.Name = T2.Name
AND T1.Email = T2.Email
AND T1.Active = FALSE;
SELECT * FROM users;
DELETE FROM some_table
WHERE id IN (SELECT id FROM some_table GROUP BY id HAVING COUNT(*)>1)
AND NOT active;

How to pull out the employees who have 'first name,lastname and date of birth' as same from Employee table

I need to pull out the records whose First name,lastname and date of birth are of same.
Please find the below example.
Employeeid firstname lastname DOB
00010 ravi sagi 22/01/1990
00035 ravi sagi 22/01/1990
00060 vasanth guptha 20/01/1987
00115 vasanth guptha 20/01/1987
Can you please help in writing the query.
Try this:
select *
from
(
select *,
count(*) over(partition by firstname, lastname, DOB) as CC
from YourTable
) as T
where T.CC > 1
You can JOIN the table to itself comparing the firstname, lastname and DOB to make sure they are the same value and then that the employeeid is not the same:
select *
from yourtable t1
inner join yourtable t2
on t1.firstname = t2.firstname
and t1.lastname = t2.lastname
and t1.dob = t2.dob
and t1.empid != t2.empid
the above query could display duplicate records, so you could use the following (see SQL Fiddle with Demo):
select DISTINCT t1.empid,
t1.firstname,
t1.lastname,
t1.DOB
from yourtable t1
inner join yourtable t2
on t1.firstname = t2.firstname
and t1.lastname = t2.lastname
and t1.dob = t2.dob
and t1.empid != t2.empid
Or you can use EXISTS (See SQL Fiddle with Demo):
select t1.empid,
t1.firstname,
t1.lastname,
t1.DOB
from yourtable t1
where exists (SELECT *
FROM yourtable t2
WHERE t1.firstname = t2.firstname
and t1.lastname = t2.lastname
and t1.dob = t2.dob
and t1.empid != t2.empid)