multiple select in sql server - sql

I want to search between 2 tables but that field i want to search is foreign key in other table
my tables are like this:
table 1
ID TitleSR
1 888
2 999
table 2
ID TitleSR
1 11
2 22
3 33
4 44
table contain value
ID value
11 italy
22 swiss
888 lilium
999 mount
33 england

I think I understand you. Try this one:
Select *
From table3 as VCT Inner Join
(Select * From table1
Union
Select * From table2) as FGT
On VCT.ID = FGT.TitleSR
Where value = 'italy';

You can use either of these methods:
Returns only t1 fields
SELECT * FROM Table1 t1
WHERE t1.ID in (SELECT ID FROM Table2);
Returns ALL fields
SELECT * FROM Table1 t1
JOIN Table2 t2 on t1.ID = t2.ID;
If your 'values' exist in a separate table (tblValues), you can use any of these:
Returns tblValues fields
SELECT * FROM tblValues tval
WHERE tval.ID in (SELECT TitleSR FROM Table1);
returns ALL fields
SELECT *
FROM (tblValues tval
JOIN Table1 t1 on tval.ID = t1.TitleSR)
JOIN Table2 on tval.ID = Table2.TitleSR;

Related

prefer table 3 over table 2 and table 2 over table 1

I have 3 tables with names and number of few of my friends.
Table 3 has correct data, table 2 has minute errors in data and table 1 has more than table 2.
if a user exists in table 2 and 3 then show table 3 details in output else table 2 details, if user is not even there in table 2 then show user data from table1.
more like a preference order.
DUMMY DATA:
table 1 :
name phone
abc 2343
bcd 3434
ccd 3455
ffc 4545
table 2 :
name phone
abc 2313
bcd 3414
ccd 3415
table 3 :
name phone
abc 2344
bcd 3431
expected output :
name phone
abc 2344
bcd 3431
ccd 3415
ffc 4545
I tried this query but unable to find correct output.
select phone,
coalesce(table1.name, TABLE2.name,TABLE3.name) as namee
FROM TABLE1
left JOIN TABLE2
ON table1.name = table2.name
INNER JOIN table3
ON table3.name = table2.name
Would be a huge, huge heeelppppp.
Gordon was close; he had the preference backwards. Please make sure you understand why the script works.
-- grab all our preferred data first
select t3.*
from table3 t3
union all
-- grab anything that doesn't exist in our preferred table
select t2.*
from table2 t2
where not exists (select 1 from table3 t3 where t3.name = t2.name)
union all
-- grab anything that doesn't exist in our preferred tables
select t1.*
from table1 t1
where not exists (select 1 from table2 t2 where t2.name = t1.name) and
not exists (select 1 from table3 t3 where t3.name = t1.name);
Use union all:
select t1.*
from table1 t1
union all
select t2.*
from table2 t2
where not exists (select 1 from table1 t1 where t1.name = t2.name)
union all
select t3.*
from table3 t3
where not exists (select 1 from table1 t1 where t1.name = t3.name) and
not exists (select 1 from table2 t2 where t2.name = t3.name);

SQL Query -Without using nested subqueries

Table1
ID SystemID Description
---------------------------
1 25 Test1
1 25 Test2
2 40 Test1
2 40 Test3
3 26 Test9
3 36 Test5
4 70 Test2
4 70 Test9
Table2
ID Department
------------------
1 Sales
2 Marketing
3 Accounting
4 Purchasing
I have these 2 tables, Table1 and Table2.
I need to select all the distinct ids from Table1 that have the same description as ID = 1 and SystemID = 25, and then select all the rows from Table2 from the query result.
Is there a better way to query for this, without using nested subqueries?
select *
from Table2
where ID in (select distinct(ID)
from Table1
where SystemID = 25
and Description in (select Description
from Table1
where ID = 1 and SystemID = 25))
Final output is
1 Sales
2 Marketing
4 Purchasing
Any help is appreciated. Thank you.
I think you want:
select t1.id, t2.department
from table1 t1 join
table2 t2
on t1.id = t2.id
where t1.description in (select tt1.description from table1 tt1 where tt1.id = 1 and tt1.systemid = 25);
This is standard SQL and should work in both SQL Server and Oracle.
You can also use a modification of an outer join to detect presence of a value.
SELECT DISTINCT t2.ID, t2.DEPARTMENT
FROM
table2 AS t2
INNER JOIN table1 AS t1a ON table2.ID = table1.ID
LEFT OUTER JOIN table1 AS t1b ON t1b.id = 1 AND t1b.systemID = 25 AND t1b.description = t1a.description
WHERE t1b.ID IS NOT NULL
AND t1a.systemID = 25
This will filter out all entries who don't have a description matching an entry with id 1 and systemID 25
I believe this should give you the same result. Instead of using an IN I used an EXISTS and then instead of a futher subquery you can then use a JOIN:
SELECT *
FROM Table2 T2
WHERE EXISTS (SELECT 1
FROM Table1 T1
JOIN Table1 T1t ON T1.[Description] = T1t.[Description]
WHERE T1.ID = T2.ID
AND T1t.ID = 1 AND T1t.SystemID = 25);
SELECT DISTINCT T2.* --Use a distinct for simplicity but a group by is better
FROM Table2 AS T2
INNER JOIN Table1 AS T1_Source ON T1_Source.SystemID = 25 AND T1_Source.ID = 1
/*^ Find table1 with System and ID
Expected Result
ID SystemID Description
1 25 Test1
1 25 Test2
Note Rows are duplicated use distinct or group by
*/
INNER JOIN Table1 AS T1_Target ON T1_Target.Description = T1_Source.Description
/*^ Find table1 with all the Description matching the result we found
Expected Result
ID SystemID Description
1 25 Test1
1 25 Test2
2 40 Test1
4 70 Test2
Note Rows are duplicated use distinct or group by
*/

Inner Join on 2 tables having multiple join value

I have the following tables where a few of the columns are included here:
Table 1:
Id RefId PhoneNumber
1 11 919191
2 11 888888
3 11 919191
Table 2:
Id RefId City UniqueId
1 11 Mumbai 111
2 11 Pune 222
3 11 Nashik 333
I want a few columns from Table1 and Table2. Common in both table is RefId. Table2 has UniqueId which is primary key of Table2. If I do an inner join based on RefId I will get 9 records, but I want 3. How do I get that?
Here is my query:
SELECT T1.PhoneNumber,T2.City,T2.UniqueId,T2.RefId
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.RefId = T2.RefId
If the ID is used to join as well then
SELECT t1.Id, t1.RefId, t1.PhoneNumber, t2.City, t2.UniqueId
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.Id = t2.Id
AND t1.RefId = t2.RefId
If the Ids are randomly generated them you probably shouldn't join on them.
If the Ids were random but all of the PhoneNumbers were for the same place then you could just pick the Minimum value PhoneNumber (or Max)
SELECT t2.RefId, t1.PhoneNumber, t2.City, t2.UniqueId
FROM Table2 t2
INNER JOIN (
SELECT RefId, MIN(PhoneNumber) AS PhoneNumber
FROM Table1
GROUP BY RefId
) t1 ON t1.RefId = t2.RefId

SQL: KEEP Unique value of C1 with Highest value in C2

In my two columns of data I would like to keep only the unique values of ColumnOne that have the highest value in ColumnTwo.
For example
ColumnOne ColumnTwo
2 6
3 2
7 8
2 7
3 4
7 3
I would like the results:
ColumnOne ColumnTwo
2 7
3 4
7 8
You can do this with a group by statement:
select Column1, max(Column2)
from your_table
group by Column1
delete t1
from myTable t1
left join (select t2.Column1, max(t2.Column2) maxColumn2
from myTable t2
group by t2.Column1) tMax
on t1.Column1 = tMax.Column1
and t1.Column2 = tMax.maxColumn2
where tMax.Column1 is null
The below query will help you to accomplish your output for tables with huge no. of records:
create table table1_new as (select * from table1) with no data;
insert into table1_new
select columnone, max(columntwo) over(partition by columnone) from table1 group by columnone;
Validate data and then interchange table names:
drop table table1;
rename table1_new to table1;

Fetch records from two tables in sql server

I have two table in sql server like this .
table 1
userid value
a 1
b 1
c 1
d 1
table 2
userid value
e 0
f 0
g 0
a 0
b 0
I want to output like this from above two tables
usrid value
a 0
b 0
c 1
d 1
e 0
f 0
g 0
if any records exists in table 1, records must fetches the data from table 2, other wise table 1. if userid not exists in table 1 and fetches records from table 2 only.
Try this:
select ISNULL(t1.usrid,t2.usrid) as usrid
,ISNULL(t1.value,t2.value) as value
from table1 t1
outer join table2 t2 on t1.usrid = t2.usrid
Try this
SELECT * FROM dbo.Table_2 AS t WHERE t.uid IN (SELECT t2.uid FROM dbo.Table_1 AS t2)
UNION
SELECT * FROM dbo.Table_1 AS t WHERE t.uid NOT IN (SELECT t2.uid FROM dbo.Table_2 AS t2)
UNION
SELECT * FROM dbo.Table_2 AS t WHERE t.uid NOT IN (SELECT t2.uid FROM dbo.Table_1 AS t2)
Try this
SELECT *
FROM table1
WHERE userid NOT IN (select userid from table2)
UNION
SELECT *
FROM table2
Try this:
SELECT *
FROM table1
WHERE table1.userid NOT IN (select userid from table2)
UNION
SELECT *
FROM table2