Fetch records from two tables in sql server - sql

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

Related

retrieving ids with 'only' one type of code

Sample of table:
ID Code
2324 1
2324 2
2325 1
2326 1
2326 2
I want to get the id’s that only have code ‘1’ and not also code ‘2’ so the result would be
2325 1
Since the others have code’s 1 and 2
I've tried
SELECT * FROM TABLE
WHERE CODE != 1 AND CODE = 2
but that just returns any id's with code 2 regardless if the id also has code 1 or not
select ID, min(code) from t1
group by ID
having min(code) = 1 and max(code) = 1
With NOT EXISTS:
SELECT * FROM TABLE T
WHERE T.Code = 1 AND NOT EXISTS (
SELECT 1 FROM TABLE WHERE ID = T.ID AND Code <> T.Code
)
Try this:
select * from myTable t1
where not exists(select 1 from myTable
where t1.id = id and Code <> 1)
use not exists
select * from table t1 where Not exists
( select 1 from table t2 where t1.id=t2.id and
t2.code=2)
with cte as
(
select 2324 as id ,1 as code union all
select 2324 ,2
union all
select 2325,1
union all
select 2326,1 union all
select 2326 ,2
)
select * from cte t1 where Not exists
( select 1 from cte t2 where t1.id=t2.id and
t2.code=2)
demo link
id code
2325 1
You can try this.
SELECT * FROM TABLE
WHERE T.Code = 1 AND NOT EXISTS (
SELECT 1 FROM TABLE WHERE ID = T.ID AND Code <> T.Code
)

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
*/

multiple select in sql server

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;

Query to Group by multiple table data in SQL Server

I have 3 tables containing data as below and I want to group data into single output.
table1
pid val
1 1
2 2
table2
id pid val
1 1 1
2 1 2
3 2 1
table3
id pid val
1 1 1
2 1 2
3 2 1
4 2 1
Required Output:
pid output
1 1 (table1 val + table2 Sum(val) - table3 Sum(val))
2 1 (table1 val + table2 Sum(val) - table3 Sum(val))
Please help with optimal query which can achieve this without any temp table.
Assume that pid is primary key of table1, you could use this
SELECT
t1.pid,
t1.val + t2.sumval - t3.sumval
FROM
table1 t1
INNER JOIN
(SELECT
pid, SUM(val) as sumval
FROM
table2
GROUP BY
pid) t2
ON
t2.pid = t1.pid
INNER JOIN
(SELECT
pid, SUM(val) as sumval
FROM
table3
GROUP BY
pid) t3
ON
t3.pid = t1.pid;

Get groups that are exactly equal to a table

I have a query that groups easily. I need to get the groups that have exactly the same records to another table (relationship).
I'm using ANSI-SQL under SQL Server, but I accept an answer of any implementation.
For example:
Table1:
Id | Value
---+------
1 | 1
1 | 2
1 | 3
2 | 4
3 | 2
4 | 3
Table2:
Value | ...
------+------
1 | ...
2 | ...
3 | ...
In my example, the result is:
Id |
---+
1 |
How imagined that it could be the code:
SELECT Table1.Id
FROM Table1
GROUP BY Table1.Id
HAVING ...? -- The group that has exactly the same elements of Table2
Thanks in advance!
You can try the following:
select t1.Id
from Table2 t2
join Table1 t1 on t1.value = t2.value
group by t1.Id
having count(distinct t1.value) = (select count(*) from Table2)
SQLFiddle
To get the same sets use an inner join:
SELECT Table1.Id
FROM Table1
INNER JOIN table2 ON table1.id=table2.id
GROUP BY Table1.Id
HAVING ...? --
CREATE TABLE #T1 (ID INT , [Values] INT) INSERT INTO #T1 VALUES (1,1),(1,2),(1,3),(2,4),(2,5),(3,6)
CREATE TABLE #T2 ([Values] INT) INSERT INTO #T2 VALUES (1),(2),(3),(4)
SELECT * FROM #T1
SELECT * FROM #T2
SELECT A.ID
FROM
( SELECT ID , COUNT(DISTINCT [Values]) AS Count FROM #T1
GROUP BY ID
) A
JOIN
(
SELECT T1.ID, COUNT(DISTINCT T2.[Values]) Count
FROM #T1 T1
JOIN #t2 T2
ON T1.[Values] = T2.[Values]
GROUP BY T1.ID
) B
ON A.ID = B.ID AND A.Count = B.Count