Self-join of a subquery - sql

I was wondering, is it possible to join the result of a query with itself, using PostgreSQL?

You can do so with WITH:
WITH subquery AS(
SELECT * FROM TheTable
)
SELECT *
FROM subquery q1
JOIN subquery q2 on ...
Or by creating a VIEW that contains the query, and joining on that:
SELECT *
FROM TheView v1
JOIN TheView v2 on ...
Or the brute force approach: type the subquery twice:
SELECT *
FROM (
SELECT * FROM TheTable
) sub1
LEFT JOIN (
SELECT * FROM TheTable
) sub2 ON ...

Do you mean, the result of a query on a table, to that same table. If so, then Yes, it's possible... e.g.
--Bit of a contrived example but...
SELECT *
FROM Table
INNER JOIN
(
SELECT
UserID, Max(Login) as LastLogin
FROM
Table
WHERE
UserGroup = 'SomeGroup'
GROUP BY
UserID
) foo
ON Table.UserID = Foo.UserID AND Table.Login = Foo.LastLogin

Yes, just alias the queries:
SELECT *
FROM (
SELECT *
FROM table
) t1
JOIN (
SELECT *
FROM table
) t2
ON t1.column < t2.other_column

Related

How to create a select clause using a subquery

I have the following sql statement:
WITH
subquery AS (
select distinct id from a_table where some_field in (1,2,)
)
select id from another_table where id in subquery;
Edit
JOIN is not an option (this is just a reduced example of a bigger query)
But that obviously does not work. The id field exists in both tables (with a different name, but values are the same: numeric ids). Basically what I want to do is filter by the result of the subquery, like a kind of intersection.
Any idea how to write that query in a correct way?
You need a subquery for the second operand of IN that SELECTs from the CTE.
... IN (SELECT id FROM subquery) ...
But I would recommend to rewrite it as a JOIN.
Are you able to join on ID and then filter on the Where clause?
select a.id
from a.table
inner join b.table on a.id = b.id
where b.column in (1,2)
Since you only want the id from another_table you can use exists
with s as (
select id
from a_table
where some_field in (1,2)
)
select id
from another_table t
where exists ( select * from s where s.id=t.id )
But the CTE is really redundant since all you are doing is
select id
from another_table t
where exists (
select * from a_table a where a.id=t.id and a.some_field in (1,2)
)

Get Distinct From Multiple Similar Tables

I have this query to join a couple tables and get distinct values, it looks something like this:
SELECT DISTINCT [TrackingCode]
,[Opponent]
,CONCAT([TrackingCode], ' | ', [Opponent]) AS RowName
,[MultiYrEvent]
,[Identifier]
FROM [BUDGET_FY2014].[dbo].[TrackingCodes]
INNER JOIN
(
SELECT *
FROM [BUDGET_FY2014].[dbo].[Transactions]
WHERE Report='2377010003'
) AS T
ON T.EventCode LIKE CAST(TrackingCodes.TrackingCode AS nvarchar(20))+'%'
ORDER BY TrackingCode ASC
It works fine. However, I've got multiple Transactions tables with the same schema for the first and second previous years relative to the Transactions table, and I'd like to see distinct values from all three tables. So for example, if I copy/paste this query and change [Transactions] to [Transactions_Yr1] or [Transactions_Yr2], then I get the data I want from those tables. But, I want to combine the three. If I try to join them all, I get no results returned. I sort of understand why this doesn't work, but I don't know where to go from here:
SELECT DISTINCT [TrackingCode]
,[Opponent]
,CONCAT([TrackingCode], ' | ', [Opponent]) AS RowName
,[MultiYrEvent]
,[Identifier]
FROM [BUDGET_FY2014].[dbo].[TrackingCodes]
INNER JOIN
(
SELECT *
FROM [BUDGET_FY2014].[dbo].[Transactions]
WHERE Report='2377010003'
) AS T
ON T.EventCode LIKE CAST(TrackingCodes.TrackingCode AS nvarchar(20))+'%'
INNER JOIN
(
SELECT *
FROM [BUDGET_FY2014].[dbo].[Transactions_Yr1]
WHERE Report='2377010003'
) AS T1
ON T1.EventCode LIKE CAST(TrackingCodes.TrackingCode AS nvarchar(20))+'%'
INNER JOIN
(
SELECT *
FROM [BUDGET_FY2014].[dbo].[Transactions_Yr2]
WHERE Report='2377010003'
) AS T2
ON T2.EventCode LIKE CAST(TrackingCodes.TrackingCode AS nvarchar(20))+'%'
ORDER BY TrackingCode ASC
Any advice would be appreciated!
Try use UNION ALL clausele, e.g.:
SELECT DISTINCT [FILDS]
FROM (
SELECT *
FROM [BUDGET_FY2014].[dbo].[Transactions]
WHERE Report='2377010003'
UNION ALL
SELECT *
FROM [BUDGET_FY2014].[dbo].[Transactions_Yr1]
WHERE Report='2377010003'
UNION ALL
SELECT *
FROM [BUDGET_FY2014].[dbo].[Transactions_Yr2]
WHERE Report='2377010003'
)
ORDER BY TrackingCode ASC
have you tried unioning your transaction tables together?
Reference: https://msdn.microsoft.com/en-us/library/ms180026.aspx
SELECT DISTINCT [TrackingCode]
,[Opponent]
,CONCAT([TrackingCode], ' | ', [Opponent]) AS RowName
,[MultiYrEvent]
,[Identifier]
FROM [BUDGET_FY2014].[dbo].[TrackingCodes]
INNER JOIN
(
SELECT * FROM(
SELECT *
FROM [BUDGET_FY2014].[dbo].[Transactions]
WHERE Report='2377010003'
) AS T
ON T.EventCode LIKE CAST(TrackingCodes.TrackingCode AS nvarchar(20))+'%'
Union
(
SELECT *
FROM [BUDGET_FY2014].[dbo].[Transactions_Yr1]
WHERE Report='2377010003'
) AS T1
ON T1.EventCode LIKE CAST(TrackingCodes.TrackingCode AS nvarchar(20))+'%'
Union
(
SELECT *
FROM [BUDGET_FY2014].[dbo].[Transactions_Yr2]
WHERE Report='2377010003'
) AS T2
ON T2.EventCode LIKE CAST(TrackingCodes.TrackingCode AS nvarchar(20))+'%'
)
ORDER BY TrackingCode ASC

Simple SQL Query problem: how to only "select" items from table_A which are also in table_B

This is the query I have been using in sqlite / python:
Select wbCode from CountriesList_A
but I would like to have some sort of IF statement to only return me the items which are ALSO in CountriesListB.
Suggestions? Thank you very much.
You can just join to the other table using an inner join, which only returns rows that are present in both tables.
SELECT a.wbCode
FROM CountriesList_A a
INNER JOIN CountriesList_B b ON a.wbCode = b.wbCode
You can do this:
select wbCode
from CountriesList_A
where wbCode in (select wbCode from CountriesListB)
TRY:
Select * From tableA A
Where Exists
(Select * From Table B
Where wbCode = A.wbCode )
Select a.wbCode from CountriesList_A a
WHERE EXISTS (
select b.wbCode
FROM CountriesListB b
WHERE a.wbCode = b.wbCode)
Or
Select a.wbCode from CountriesList_A a
WHERE a.wbCode IN (
select b.wbCode
FROM CountriesListB b)
Or
Select a.wbCode
from CountriesList_A a inner join CountriesListB b
on a.wbCode = b.wbCode

SQL query to merge 2 tables with additional conditions?

I have 2 identical tables: user_id, name, age, date_added.
USER_ID column may contain multiple duplicate IDs.
Need to merge those 2 tables into 1 with the following condition.
If there are multiple records with identical 'name' for the same user then need to keep only the LATEST (by date_added) record.
This script will be used with MSSQL 2005, but would also appreciate if somebody comes up with version that does not use ROW_NUMBER(). Need this script to reload a broken table once, performance is not critical.
example:
table1:
1,'john',21,01/01/2010
1,'john',15,01/01/2005
1,'john',71,01/01/2001
table2:
1,'john',81,01/01/2007
1,'john',15,01/01/2005
1,'john',11,01/01/2008
result:
1,'john',21,01/01/2010
UPDATE:
I think that I've found my own solution. It is based on an answer for my previous question given by Larry Lustig and Joe Stefanelli.
with tmp2 as
(
SELECT * FROM table1
UNION
SELECT * FROM table2
)
SELECT * FROM tmp2 c1
WHERE (SELECT COUNT(*) FROM tmp2 c2
WHERE c2.user_id = c1.user_id AND
c2.name = c1.name AND
c2.date_added >= c1.date_added) <= 1
Could you please help me to convert this query to the one without 'WITH' clause?
Here's a variant of #Andomar's answer:
; with all_users as
(
select *
from table1 u1
union all
select *
from table2 u2
)
, ranker as (
select *,
rank() over (partition by userid order by recordtime) as [r]
)
select * from ranker where [r] = 1
Just in the interests of giving a different approach...
WITH distinctlist
As (SELECT user_id,
name
FROM table1
UNION
SELECT user_id,
name
FROM table2)
SELECT C.*
FROM distinctlist d
CROSS APPLY (SELECT TOP 1 *
FROM (SELECT TOP 1 *
FROM table1
WHERE user_id = d.user_id
AND name = d.name
ORDER BY date_added DESC
UNION ALL
SELECT TOP 1 *
FROM table1
WHERE user_id = d.user_id
AND name = d.name
ORDER BY date_added DESC) T
ORDER BY date_added DESC) C
You could use not exists, like:
; with all_users as
(
select *
from table1 u1
union all
select *
from table2 u2
)
select *
from all_users u1
where not exists
(
select *
from all_users u2
where u1.name = u2.name
and u1.record_time < u2.record_time
)
If the database doesn't support CTE's, expand all_users in the two places it is used.
P.S. If there are only three columns, and no more, you could use an even simpler solution:
select name
, MAX(record_time)
from (
select *
from table1 u1
union all
select *
from table2 u2
) sub
group by
name

How to do a Select in a Select

I have a table containing a unique ID field. Another field (REF) contains a reference to another dataset's ID field.
Now I have to select all datasets where REF points to a dataset that doesn't exist.
SELECT * FROM table WHERE ("no dataset with ID=REF exists")
How can I do this?
3 ways
SELECT * FROM YourTable y WHERE NOT EXISTS
(SELECT * FROM OtherTable o WHERE y.Ref = o.Ref)
SELECT * FROM YourTable WHERE Ref NOT IN
(SELECT Ref FROM OtherTable WHERE Ref IS NOT NULL)
SELECT y.* FROM YourTable y
LEFT OUTER JOIN OtherTable o ON y.Ref = o.Ref
WHERE o.Ref IS NULL
See also Five ways to return all rows from one table which are not in another table
Try this:
SELECT * FROM TABLE WHERE NOT EXISTS
(SELECT * FROM OtherTable WHERE TABLE.Ref = OtherTable.ID)
I think this should work
SELECT * FROM table WHERE id NOT IN (SELECT ref_id FROM ref_table)
or with JOIN
SELECT table.*
FROM table LEFT JOIN ref_table ON table.id = ref_table.ref_id
WHERE ref_table.ref_id IS NULL
SELECT
table1.*
FROM
table1
LEFT JOIN table2 ON table1.id = table2.ref
WHERE
table2.ref IS NULL
You can do a subquery like:
select * from table where somefield not in (select otherfield from sometable where ID=REF)
SELECT *
FROM table
WHERE ((SELECT COUNT(*) FROM table2 WHERE table2.id = table.ref) = 0)
Something like that :
SELECT * FROM table WHERE ID NOT IN(SELECT REF FROM Table2 )
Yes you can use
select * from x where not exist ( select * from y )