Join two tables in postgresql if type of columns is different - psycopg2

I need to join table two tables table1 and table2 where table1.id = table2.id but table1.id is an integer and table2.id is a string.

Try
select * from a join b on a.id = nullif(b.id, '')::int

You need convert convert table2.id into integer before compare.
(https://www.postgresql.org/message-id/371F1510.F86C876B#sferacarta.com)
select * from table1,table2 where table1.id = cast(table2.id as int)

Related

Cognos 11 - filter between query subjects

Given Table A with columns: ColA1, ColA2, ColA3
And a Table B with columns: ColB1
I want to restrict the data that can be returned from Table A based on data in Table B, like:
ColA1 not in ColB1
Ideally, some way incorporate SQL queries in the filter with select statements
What you want is
SELECT a.ColA1
, a.ColA2
, a.ColA3
FROM TableA a
LEFT OUTER JOIN TableB b on b.ColB1 = a.ColA1
WHERE b.ColB1 IS NULL
So...
Query1 contains ColA1, ColA2, and ColA3 from TableA.
Query2 contains ColB1 from TableB.
Query3
joins Query1 and Query2 on ColA1 1..1 = 0..1 ColB1
Data Items: ColA1, ColA2, ColA3
Filter: ColB1 IS NOT NULL
not exists is probably what you are looking for
Try something like this
select * from TableA as T1
where not exists
(select * from TableB as T2
where t1.key1 = t2.key1 and T1.key2 = t2.key2)

{MS Access] How can I joint two tables without duplicate data?

I got two tables below:
And I want to create a query to combine them like below:
But unsuccessfully I got something like this:
Some data for "value1" duplicated
How can I solve this?
Is there any function that can have "value1" for the first "no." only?
Thank you.
You can do this by using a subquery to prepare the data.
It seems you want only the rows with the lowest Sub no to join, so we'll first select that:
SELECT [No], Value2
FROM Table2 m
WHERE
EXISTS(
SELECT 1
FROM Table2 s
WHERE s.[No] = m.[No]
HAVING MIN(s.sub_no) = m.sub_no
)
Then, integrate this into your main query:
SELECT *
FROM Table1
INNER JOIN (
SELECT [No], Value2
FROM Table2 m
WHERE
EXISTS(
SELECT 1
FROM Table2 s
WHERE s.[No] = m.[No]
HAVING MIN(s.sub_no) = m.sub_no
)
) AS T2 ON T1.[No] = T2.[No]

Typed query for INNER JOIN (SELECT DISTINCT)?

Is it possible to create a typed query that produces the following SQL?
SELECT A.*
FROM schema1.Table1 A
INNER JOIN (SELECT DISTINCT column1, column2 FROM schema1.Table2) B ON A.column1 = B.column1
You can't join a sub select with a typed API, the easiest way to implement this would be to use a CustomJoin, e.g:
var table1 = db.GetTableName<Table1>();
var q = db.From<Table1>()
.CustomJoin($#"INNER JOIN
(SELECT DISTINCT column1, column2 FROM schema1.Table2) B
ON {table1}.column1 = B.column1");

How can we construct a query containing Union with JoinSqlBuilder in SeviceStack OrmLite

I have a query like this where I need to union two tables and then join it with a third one
SELECT *
FROM (SELECT * FROM Table1 where col2_id = 1
UNION ALL
SELECT * FROM Table2 where col2_id = 1) AS UnionList
INNER JOIN Table3 t3 ON t3.id = UnionList.col3_id
I am trying to use JoinSqlBuilder in ServiceStack OrmLite to build this query. I am able to create the join part but I am confused about how to create the Union part. Did not find any option/function which union's two table.

SQL query for SQL Server Compact Edition 3.5 - GROUP BY issue

SELECT BabyInformation.* , t1.*
FROM BabyInformation
LEFT JOIN
(SELECT * FROM BabyData
GROUP BY BabyID
ORDER By Date DESC ) AS t1 ON BabyInformation.BabyID=t1.BabyID
This is my query. I want to get the one most recent BabyData tuple based on date.
The BabyInformation should left join with babyData but one row per baby...
I tried TOP(1) but this worked only for the first baby
Here is one way to do it, there are other ways which can be faster, but I believe this one to be the clearest for a beginner.
SELECT BabyInformation.*, BabyData.*
FROM BabyInformation
JOIN
(SELECT BabyID, Max(Date) as maxDate FROM BabyData
GROUP BY BabyID
) AS t1
ON BabyInformation.BabyID=t1.BabyID
Join BabyData ON BabyData.BabyID = t1.BabyID and BabyData.Date = t1.maxDate
This should do it:
SELECT bi.* , bd.*
FROM BabyInformation [bi]
LEFT JOIN BabyData [bd]
on bd.BabyDataId = (select top 1 sub.BabyDataId from BabyData [sub] where sub.BabyId = bi.BabyId order by sub.Date desc)
I've assumed that there is a column called 'BabyDataId' in the BabyData table.

Resources