CloudKit - How to perform Not Exists query? - cloudkit

I want to perform a database query in CloudKit like in SQL:
SELECT *
FROM table1 t1
WHERE NOT EXISTS
(SELECT *
FROM table2 t2
WHERE t1.userid = t2.userid)
Do you know how to do that?

Try
SELECT *
FROM table1 t1
WHERE t1.userid NOT IN(
SELECT t2.userid
FROM table2 t2);
or
SELECT t1.userid
FROM table1 t1
MINUS
SELECT t2.userid
FROM table2 t2;

Related

postgresql How to share cte among different tables in plain sql?

Say select id from some_expensive_query is the cte I want to share. Currently I write two sql in a transaction:
with t as (select id from some_expensive_query) select * from t1 join t on t.id =t1.id;
with t as (select id from some_expensive_query) select * from t2 join t on t.id =t2.id;
As you can see, the cte is executed twice but I want something like:
t = select id from some_expensive_query;
select * from t1 join t on t.id =t1.id;
select * from t2 join t on t.id =t2.id;
for portability, I don't want to use pgsql or functions, anyway to solve this?
Why don't you use union all ?
with t as (select id from some_expensive_query)
select * from t1 join t on t.id =t1.id
union all
select * from t2 join t on t.id =t2.id;

pl-sql equivalent for t-sql "UPDATE FROM" clause on temp tables

In SQL Server stored procedure I have this code:
UPDATE #tbinforesult
SET prifix = ''
FROM #tbmax t
INNER JOIN #tbinforesult i
ON i.prifix = t.prifix
AND i.PropertyID = t.PropertyID
AND i.GroupID = t.GroupID
AND i.NID <> t.id
What is the equivalent in pl-sql with pl-sql collections not global temporary table?
TSQL :
UPDATE
Table_A
SET
Table_A.col1 = Table_B.col1,
Table_A.col2 = Table_B.col2
FROM
Some_Table Table_A
INNER JOIN
Other_Table Table_B
ON
Table_A.id = Table_B.id
WHERE
Table_A.col3 = 'cool'
PLSQL:
UPDATE table1 t1
SET (name, desc) = (SELECT t2.name, t2.desc
FROM table2 t2
WHERE t1.id = t2.id)
WHERE EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.id = t2.id )
OR:
UPDATE (SELECT t1.id,
t1.name name1,
t1.desc desc1,
t2.name name2,
t2.desc desc2
FROM table1 t1,
table2 t2
WHERE t1.id = t2.id)
SET name1 = name2,
desc1 = desc2
SOURCE :enter link description here

select record from joined table if it exists

I'm working on a sql query that should 'coalesce' the records from 2 tables, i.e. if the record exists in table2, it should take that one, otherwise it should fall back to the values in table1.
In the example, table1 and table2 have just 2 fields (id an description), but obviously in reality there could be more.
Here's a small test case:
create table table1 (id int, description nvarchar(50))
create table table2 (id int, description nvarchar(50))
insert into table1 values (1, 'record 1')
insert into table1 values (2, 'record 2')
insert into table1 values (3, 'record 3')
insert into table2 values (1, 'record 1 modified')
insert into table2 values (2, null)
The result of the query should look like this:
1, "record 1 modified"
2, null
3, "record 3"
Here's what I came up with.
select
case when table2.id is not null then
table2.id else table1.id
end as Id,
case when table2.id is not null then
table2.description
else
table1.description
end as Description
-- etc for other fields
from table1
left join table2 on table1.id = table2.id
Is there a better way to achieve what I want? I don't think I can use coalesce since that would not select a null value from table2 if the corresponding value in table1 is not null.
How about:
SELECT t2.ID, t2.Description
FROM table2 t2
UNION ALL
SELECT t1.ID, t1.Description
FROM table1 t1
WHERE NOT EXISTS (SELECT *
FROM table2 t2
WHERE t2.ID = t1.ID)
The above query gets all the records from table 2 (including the case where description is NULL but the ID is populated), and only the records from table 1 where they don't exist in table 2.
Here's an alternative:
SELECT table2.*
FROM table1
RIGHT JOIN table2
ON table1.id = table2.id
UNION
SELECT table1.*
FROM table1
FULL OUTER join table2
ON table1.id = table2.id
WHERE table1.id NOT IN (SELECT id FROM table2)
--and table2.id not in (select id from table1)
You can add in that last line if you don't want ids that are only in table2. Otherwise I guess Stuart Ainsworth's solution is better (i.e. drop all the joins)
http://sqlfiddle.com/#!3/03bab/12/0

tsql join 2 tables with duplicate records in the second table

I have 2 tables.
Table 1:
Id Name
1 John
2 Mike
3 Sam
Table 2:
Name Data
John Data1
John Data1
John Data1
Mike Data2
Mike Data2
Sam Data3
If I write
select Table2.Name, Table2.Data
from Table1
inner join Table2 on Table1.Name= Table2.Name
I get all the duplicate data.
I would like to be able to retrieve something like:
John Data1
Mike Data2
Sam Data3
SELECT DISTINCT NAME
, DATA
FROM Table2
WHERE NAME IN (SELECT NAME
FROM Table1)
There are a few different options here...
There are two ways to do that.
You can use distinct clause:
select distinct t2.Name, t2.Data
from Table1 t1
inner join Table2 t2 on t1.Name= t2.Name
Here is link to MSDN.
You can use group by :
select t2.Name, t2.Data
from Table1 t1
inner join Table2 t2 on t1.Name= t2.Name
group by t2.Name, t2.Data
Here is link to MSDN.
I prefer second solution, because I always can add grouping functions.
Note:
In both queries I used aliases (t1,t2). It's more readable.
You can use CTE for this and apply a row_number()
;with cte as
(
select t1.name, t2.data,
row_number() over(partition by t1.id order by t1.id) rn
from table1 t1
inner join table2 t2
on t1.name = t2.name
)
select *
from cte
where rn = 1
Or a non-CTE version:
select *
from
(
select t1.name, t2.data,
row_number() over(partition by t1.id order by t1.id) rn
from table1 t1
inner join table2 t2
on t1.name = t2.name
) x
where rn = 1
see SQL Fiddle with Demo
Are you looking for something like:
select Table2.Name, Table2.Data, count(*) from Table1
inner join Table2 on Table1.Name= Table2.Name
group by Table2.Name, Table2.Data;

ordering by rows

OK so I have a query I am trying to build.. I have 2 tables, table1 has a bunch of regular records as normal with a unique ID (auto increment) and table2 has records that include some of those ids from table1. I am trying to order by the highest records with that same ID in table1.. Heres what I've got:
SELECT * FROM table1
WHERE table1.status = 1
AND (SELECT COUNT(*) FROM table2 WHERE table2.tbl1_id = table1.id)
ORDER BY table1.id DESC
Thanks :)
SELECT table1.id
FROM table1
LEFT JOIN table2 ON table2.tbl1_id = table1.id
WHERE table1.status = 1
GROUP BY table1.id
ORDER BY COUNT(table2.tbl1_id) DESC
Try this:
SELECT a.*, b.cnt
FROM table1 a LEFT JOIN
(
SELECT tbl1_id, COUNT(*) cnt
FROM table2
GROUP BY tbl1_id
) b
ON a.id = b.tbl1_id
WHERE table1.status = 1
ORDER BY cnt DESC