Concatenate two fields from different tables in SQLite - database

How can I concatenate two fields from two different tables in SQLite?
Say I have:
Id Name
---------- ------
1 John
2 Doe
Id Name
---------- ------
1 SchoolA
2 SchoolB
Expected Output
Id Name
---------- ------
1 John-SchoolA
2 Doe-SchoolB
NB: Both tables have the same Ids.
Updated:
Table 1
Id Name
---- -------
1 John
2 Doe
Table 2
Table1_id Name
--------- ------
1 SchoolA
2 SchoolB
NB: Both tables have the same Ids (in terms of value).

You must join the tables on their ids and concatenate the names of the matching rows:
SELECT t1.Id,
t1.Name || '-' || t2.Name AS Name
FROM table1 t1 INNER JOIN table2 t2
ON t2.Id = t1.Id
See the demo.

Related

how to exclude not null and include only null values for two different tables with same columns names

Can someone please help me how to exclude not null and include only null values for two different tables with same columns names?
TableA TableB
Sub_id Track_no Active_date Sub_id Track_no Active_date
------- -------- ----------- ------- -------- -----------
001 123 null 001 124 01/02/2013
001 124 null
001 125 null
Here I need to get the records Track_no where active_date is null and sub_id = 001, I must join two table because Active_date is available in tableB only.
I hope i got you right
select * from tableA a
inner join tableB b
on a.sub_id = b.sub_id
and a.track_no = b.track_no
and a.active_date is null
The following query shows the tableA data, but active_date is taken from tableB in case it is null in tableA.
select sub_id,
track_no,
coalesce(a.active_date,b.active_date) active_date,
a.active_date active_date_from_a,
b.active_date active_date_from_b
from tableA a
left join tableB b
on a.sub_id = b.sub_id

SQL Server select join detect if common column between two tables are different

I am trying to write a function to check between two tables which have a common column with the same name and ID values.
Table 1: CompanyRecords
CompanyRecordsID CompanyId CompanyName CompanyProcessID
-----------------------------------------------------------
1 222 Sears 123
2 333 JCPenny 456
Table 2: JointCompanies
JointCompaniesID CompanyId CompanyName ComanyProcessID
-----------------------------------------------------------
3 222 KMart 123
4 444 Walmart 001
They both use the same foreign key CompanyProcessID with value 123.
How do I write a select statement when it is passed the CompanyProcessID to tell if the CompanyId has changed for the same CompanyProcessId.
I assume it is a join between the two tables with WHERE CompanyProcessID
Thanks for any help.
Is this what you want?
select max(case when cr.name = jc.name then 0 else 1 end) as name_not_same
from CompanyRecords cr join
JointCompanies jc
on cr.ComanyProcessID = jc.ComanyProcessID
where cr.ComanyProcessID = ?

Adding values from a different table based on values in multiple columns of another table

I have 2 Tables
Table 1
Name column2 column3 column 4
Suzy English null null
Rocky Polish Irish null
John English American Funny
George Funny English null
Table 2
Column Value
English 2
Polish 3
Irish 2
Funny 0
American 1
The values in Column in Table 2 are unique.
I want to add a column in Table 1 which finds all the matching values from columns 2, 3 and 4 in table 1, finds the corresponding values in the ‘column’ in table 2 and the adds the corresponding values, So that Table 1 now is updated to look like
Table 1
Name column2 column3 column 4 Total
Suzy english null null 2
Rocky Polish Irish null 5
John English American Funny 3
George Funny English null 2
Is this possible at all? Or do I need to have another query first?
Your table structures are less than ideal, since apparently all of column2, column3 and column4 in table 1 contain items of the same "type".
There are various ways of creating your totals - we can either perform multiple joins or use a correlated subquery. I'm using the subquery here:
select
*,
(select SUM(t2.Value) from Table2 t2
where t2.Column1 in (t1.Column2,t1.Column3,t1.Column4)) as TotalValue
from
Table1 t1
You can use left join and do addition as below
select t1.*, [Total] = isnull(C2.Value,0) + isnull(C3.Value,0) + isnull(C4.Value,0)
from [Table 1] t1
left join [Table 2] c2 on t1.Column1 = c2.[Column]
left join [Table 2] c3 on t1.Column1 = c3.[Column]
left join [Table 2] c4 on t1.Column1 = c4.[Column]

SQL Server query involving subqueries - performance issues

I have three tables:
Table 1: | dbo.pc_a21a22 |
batchNbr Other columns...
-------- ----------------
12345
12346
12347
Table 2: | dbo.outcome |
passageId record
---------- ---------
00003 200
00003 9
00004 7
Table 3: | dbo.passage |
passageId passageTime batchNbr
---------- ------------- ---------
00001 2015.01.01 12345
00002 2016.01.01 12345
00003 2017.01.01 12345
00004 2018.01.01 12346
What I want to do: for each batchNbr in Table 1 get first its latest passageTime and the corresponding passageID from Table 3. With that passageID, get the relevant rows in Table 2 and establish whether any of these rows contains the record 200. Per passageId there are at most 2 records in Table 2
What is the most efficient way to do this?
I have already created a query that works, but it's awfully slow and thus unfit for tables with millions of rows. Any suggestion on how to either change the query or do it another way? Altering the table structure is not an option, I only have read rights to the database.
My current solution (slow):
SELECT TOP 50000
a.batchNbr,
CAST ( CASE WHEN 200 in (SELECT TOP 2 record FROM dbo.outcome where passageId in (
SELECT SubqueryResults.passageId From (SELECT Top 1 passageId FROM dbo.passage pass WHERE pass.batchNbr = a.batchNbr ORDER BY passageTime Desc) SubqueryResults
)
) then 1 else 0 end as bit) as KGT_IO_END
FROM dbo.pc_a21a22 a
The desired output is:
batchNbr 200present
--------- ----------
12345 1
12346 0
I suggest you use table joining rather than subqueries.
select
a.*, b.*
from
dbo.table1 a
join
dbo.table2 b on a.id = b.id
where
/*your where clause for filtering*/
EDIT:
You could use this as a reference Join vs. sub-query
Try this
SELECT TOP 50000 a.*, (CASE WHEN b.record = 200 THEN 1 ELSE 0 END) AS
KGT_IO_END
FROM dbo.Test1 AS a
LEFT OUTER JOIN
(SELECT record, p.batchNbr
FROM dbo.Test2 AS o
LEFT OUTER JOIN (SELECT MAX(passageId) AS passageId, batchNbr FROM
dbo.Test3 GROUP BY batchNbr) AS p ON o.passageId = p.passageId
) AS b ON a.batchNbr = b.batchNbr;
The MAX subquery is to get the latest passageId by batchNbr.
However, your example won't get the record 200, since the passageId of the record with 200 is 00001, while the latest passageId of the batchNbr 12345 is 00003.
I used LEFT OUTER JOIN since the passageId from Table2 no longer match any of the latest passageId from Table3. The resulting subquery would have no records to join to Table1. Therefore INNER JOIN would not show any records from your example data.
Output from your example data:
batchNbr KGT_IO_END
12345 0
12346 0
12347 0
Output if we change the passageId of record 200 to 00003 (the latest for 12345)
batchNbr KGT_IO_END
12345 1
12346 0
12347 0

SQL query to get all the data from different tables with same id

Sorry if this is too elemental but I cannot work it out. Don’t know how to search information on it either:
I have three tables:
Provider
id_provider name
---------- -----------
100 John
101 Sam
102 Peter
Contact
id_contact RowNo Email
---------- ----------- ----------------
100 1 john#work.com
100 2 john#gmail.com
101 1 sam#work.com
101 2 sam#yahoo.com
Product
Id_product RowNo Product
---------- ----------- ------------------------
100 1 John’s 1st product
100 2 John’s 2nd product
101 1 Sam’s 1st product
101 2 Sam’s 2nd product
101 3 Sam’s 3rd product
I need a query to show all the data from the three tables like this:
Id name id_contact RowNo Email Id_Product RowNo Product
100 John 100 1 john#work.com 100 1 John’s 1st product
100 John 100 2 john#gmail.com 100 2 John’s 2st product
101 Sam 101 1 sam#work.com 101 1 Sam's 1st product
101 Sam 101 2 sam#yahoo.com 101 2 Sam's 2nd product
101 Sam null null null 101 3 Sam's 3rd product
102 Peter null null null null null null
I am trying all the joins I know but I cannot make it work.
Thanks a lot
You can use the following query:
SELECT t1.id_provider AS Id, t1.name,
t2.id_contact, t2.cRowNo, t2.Email,
t2.Id_product, t2.Product
FROM Provider AS t1
LEFT JOIN (
SELECT COALESCE(id_contact, id_product) AS id,
c.id_contact, c.RowNo AS cRowNo, c.Email,
p.Id_product, p.Product, p.RowNo AS pRowNo
FROM Contact AS c
FULL JOIN Product AS p ON p.id_product = c.id_contact AND p.RowNo = c.RowNo
) AS t2 ON t1.id_provider = t2.id
The query does a FULL JOIN between Contact and Product tables and joins the table derived from the FULL JOIN to Provider table.
A FULL JOIN is required because we cannot know beforehand which of the two tables, Contact or Product, contains the most rows for each id.
select *
from Provider P1
left join Contact C2
on C2.id_contact = P1.id_provider
left join Product P2
on P2.id_product = P1.id_provider
SELECT prov.*,
c.*,
prod.*
FROM PROVIDER prov
LEFT JOIN Product prod ON prod.id_product = prov.id_provider
LEFT JOIN Contact c ON prov.id_provider = c.id_contact
AND prod.RowNo = c.RowNo
use left joins but join provider to product first then to contact
SQL Fiddle Demo

Resources