How to use OFSET and FETCH for table with JOIN - sql

I have a pretty simple case with two tables:
create table Table1
(
Id int,
Name varchar(255),
)
and
create table Table2
(
Id int,
Name varchar(255),
ParentId int
)
I want to get some count of rows (for example offset 10 and fetch 5) by Table1 with joining of Table2. Between Table1 and Table2 I have a relation one-to-many.
I try to write something like that:
SELECT *
FROM Table1
JOIN Table2 AS t2 ON Table1.Id = t2.ParentId
ORDER BY Table1.Id
OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY
As a result I get 5 rows from joined tables. But I want to get 5 rows from Table1 and then join to them Table2. Have someone any ideas how to do that?

SELECT *
FROM (SELECT *
FROM Table1
ORDER BY Id
OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY) AS t1
JOIN Table2 AS t2 ON t1.Id = t2.ParentId
Thank you all! That was easy)

Related

Find all ID's in 2 different tables that Don't have a Username

Ok, so I have two tables. Table1 and Table2.
Table1 - All ID's
ID
Table2 - All ID's with Usernames
ID
Username
I want to select all ID's that DON'T have usernames. Table2 only has ID's that have Usernames. Basically I want to do a select on table 1 join 2 table on ID that don't have usernames. Since table2 only has ID's with usernames, it's basically all of the ID's that are in Table 2 subtracted from the TOTAL ID's in table1 so that I only get ID's that aren't in table2 but are in table1
This should work for you:
select id from table1
left join table2 on table2.id = table1.id
where table2.usernames is null
or you can do a not in
select id from table1 where id not in(select id from table2)
SELECT t1.ID
FROM Table1 t1
JOIN Table2 t2 ON
t2.ID = t1.ID
WHERE t2.Usernames IS NULL
One of the ways to do this is with a compound query:
SELECT id FROM Table1
EXCEPT
SELECT id FROM Table2;

SQL many to many relation: create N random records in relations table

There are tree tables:
Table1
------------------
| Id | CreatedBy |
------------------
Table2
------------------
| Id | CreatedBy |
------------------
Table3 (table to store relations between Table1 and Table2)
-----------------------
| table1Id | table2Id |
-----------------------
I have many records with different creators in tables 1 and 2. What I need is to create relations between all records in table 1 and N random records in table2 (of course for same creator).
What I have tried already:
create select with inner join that will create relation records
INSERT INTO Table3
SELECT t1.Id, t2.Id FROM Table1 t1
INNER JOIN (SELECT TOP(10) Id, CreatedBy FROM Table2 ORDER BY NEWID()) AS t2
ON t1.CreateBy = t2.CreatedBy
the problem here that subquery can return top 10 records created by other creators and after ON t1.CreateBy = t2.CreatedBy we will get empty result.
write this query without join (few select statements but without success).
Why do you want to only create part of the data? This is quite a bizarre requirement. Something like this maybe?
with MyCTE as
(
SELECT t1.Id as ID1, t2.Id as ID2, ROW_NUMBER() over (partition by t1.CreateBy order by (select newid())) as RowNum
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.CreateBy = t2.CreatedBy
)
insert Table3
select ID1, ID2
from MyCTE
where RowNum <= 10
This is the solution:
Declare #Query Nvarchar(Max)
Declare #Num int=10
Set #Query='
Select a.Id,b.Id
From Table1 a
Inner join (
Select top '+ltrim(Rtrim(Str(#Num)))+' t.Id,t.CreatedBy
From Table2 t order by newid()) as b
On (a.CreatedBy=b.CreatedBy)'
Exec sp_executesql #Query

t-sql update multiple rows using inner join

i trying to update a column in my table for several rows at the same time.
i want get the value to update from an another table which has a foreignkey.
Table Table1:
Id Primary
UserNumber INT
Table Table2
Id Primary
Id_T1 ForeignKey
UserId INT
OrderNumber INT
can someone help with this pls?
Note: In the Table T2 i need the UserId Value Where the ordernumber has the maximum value.
for example:
Id_T1 UserId OrderNumber
15 24 1
15 55 2
15 72 3
the value i want to receive is the 72.
i have try this:
update T1 set T1.UserNumber = T2.UserId
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.Id = T2.IdMain
WHERE T1.Id = T2.IdMain
AND T2.OrderNumber = (SELECT MAX(T2.OrderNumber) FROM Table2)
but i get this error:
Msg 147, Level 15, State 1, Line 6 An aggregate may not appear in the
WHERE clause unless it is in a subquery contained in a HAVING clause
or a select list, and the column being aggregated is an outer
reference.
You don't need to use alias name in inner query.
Try this:
UPDATE T1
SET T1.UserNumber = T2.UserId
FROM Table1 AS T1 INNER JOIN
Table2 AS T2 ON T1.Id = T2.IdMain
WHERE T1.Id = T2.IdMain
AND T2.OrderNumber = (SELECT MAX(OrderNumber) FROM Table2)

How Can i select a field and check it with itself?

i want to say
select col1,col2,col3
from table1
inner join table2 on table1.col1=table2.col1
and ..... ( ? )
? : i want just 1 record or first record from table1 joined with first record from table2. but the command cause all record joined that can be join. for example if 2 records are in the table1 that col1=1432 and just 1 record in table2 exists that col1=1432 command joined all. but i want to join just first from table1 with first from table2
i want to display all record that are more than 1 record to be join.
i want just 1 record or first record from table1 joined with first
record from table2
Here you go:
select top 1 col1,col2,col3
from table1
inner join table2 on table1.col1=table2.col1
and .....
Try this query -
SELECT *
FROM dbo.table1 t1
JOIN (
SELECT TOP 1 *
FROM dbo.table2
) t2 ON t1.col1 = t2.col1
If you're on SQL Server 2005 or above, you can try a ranking function to grab only the first record of each group:
select *
from (
select a.col1
, a.col2
, a.col3
-- Use the order by to determine which rows will be ranked first for each group
, row_number() over (partition by a.col1 order by a.col2) as rownum
from table1 as a
join table2 as b on a.col1 = b.col1
) as q
where rownum = 1 -- Only get the first row of each group
i want to display all record that are more than 1 record to be join.
You can add this to your where clause:
and exists (
select col1
from table1
where col1 = q.col1
group by col1
having count(*) > 1
)
To offer another solution that doesn't use a ranking function, I think we would need to know more about your tables, especially unique keys and how you define the first record of each group.

Help required with SQLs

I am trying to join 2 tables and trying to fetch the records, which do not have values in the join column of the 2nd table.
For example:
Table 1 Id column values:
1,
2,
3,
4,
Table 2 Id column values:
1,
3,
Given the above example of the values in the join columns of the 2 tables, I want to fetch records from table1 with ids 2 and 4, because they are not present in table2.
Any help would be much appreciated.
My SQL has gotten rusty to the introduction of JPA frameworks, but today I cannot run away from not knowing it, it seems :(
Thanks!
select t1.id
from Table1 t1
left outer join Table2 t2 on t1.id = t2.id
where t2.id is null
SELECT * FROM table1 WHERE table1.id NOT IN (SELECT id from table2)
NOT EXISTS variant:
SELECT * FROM table1 WHERE NOT EXISTS
(SELECT NULL from table2 WHERE table2.id = table1.id)