I have two tables.
TableA
Id
Column 1
Column 2
TableB (n-1 mapping with TableA)
Column 1
Column 2
fkToTableAonIdentity
and my query is
DECLARE #Offset = 0,
DECLARE #pageSize = 10
SELECT
A.column1, B.Column1
FROM
TableA AS A
LEFT JOIN
TABLEB AS TABLE B
ORDER BY
B.Column2 DESC
OFFSET #Offset ROWS
FETCH NEXT #PageSize ROWS ONLY
I was trying to fetch 10 rows from tableA joining data for tableB
but the query will only return exact 10 rows from the set created by left join, but I needed 10 rows of data from table A, so in set of left join number of rows may vary for each record in TableA.
How can I get the desired result?
Update:
I am using the above query in my stored procedure where #pageSize will be a parameter to the stored procedure.
Use following syntax:
SELECT * FROM
(SELECT TOP 10 * FROM Table1) ST1
JOIN Table2 ON ST1.Id=Table2.FkToT1
I expect your query will look as following one:
SELECT ST1.Col1, T2.Col1 FROM
(
SELECT * FROM Table1
ORDER BY Col1
OFFSET #offset ROWS
FETCH NEXT #page ROWS ONLY
) ST1
JOIN Table2 T2 ON ST1.Id=T2.FkToT1
Related
So, I have 2 tables…. I need to get the combined row count from both, excluding any duplicates…
For example….
Table 1 has 20000 rows and Table 2 has 500 rows
There is 1 duplicate id that is in both table 1 and table 2, so the total row count should be 20,499….
This is what I have tried so far….
with cterc as
(SELECT COUNT(*) as rn
FROM Table_1 as t1
join Table_2 as t2 on t1.id <> t2.id)
SELECT SUM(rn) as totalrowNo
from cterc
Does the following provide your expected count?
select count(*)
from (
Select Id from Table_1
union /* distinct values, union all doesn't distinct values */
Select Id from Table_2
)t;
I have two tables, Table 1 with multiple columns, name, ID number, address, etc. And Table 2 with columns, ID number 1 and ID number 2 and a few other columns.
I am trying to get a T-SQL query returning all rows in Table 1 with an indicator showing whether the ID number in Table 1 matches either ID_1 or ID_2 in Table 2. The result set would be all columns from Table 1 , plus the indicator “Matched” if the ID number in Table 1 matches either ID_1 or ID_2 in Table 2.
Table 1: ID | Name | Address |
Table 2: ID_1 | ID_2
Result
T1.ID, Name, Address, ("Matched"/"Unmatched") ...
Also, would it be the same to do the opposite, meaning instead of the result including all rows from Table 1 that have a matching ID in ID_1 or ID_2 in Table 2, the result set would include only records from Table 1 where t1.ID = (T2.ID_1 or T2.ID_2)?
SELECT DISTINCT
CASE
WHEN (table1.ID = table2.ID_1 )
THEN 'Matched'
ELSE 'Unmatched'
END AS Status ,
table1.*
FROM
table1
LEFT JOIN
table2 ON table1.ID = table2.ID_1
UNION
SELECT DISTINCT
CASE
WHEN (table1.ID = table2.ID_2)
THEN 'Matched'
ELSE 'Unmatched'
END AS Status,
table1.*
FROM
table1
LEFT JOIN
table2 ON table1.ID = table2.ID_2
I think that a correlated subquery with an exists condition would be a reasonable solution:
select
t1.*,
case when exists (select 1 from table2 t2 where t1.id in (t2.id_1, t2.id_2))
then 'Matched'
else 'Unmatched'
end matched
from table1 t1
And the other way around:
select
t2.*,
case when exists (select 1 from table1 t1 where t1.id in (t2.id_1, t2.id_2))
then 'Matched'
else 'Unmatched'
end matched
from table2 t2
If you want to "align" the rows based on the match for the whole dataset at once, then you might want to try a full join:
select t1.*, t2.*
from table1 t1
full join table2 t2 on t1.id in (t2.id_1, t2.id_2)
I have two tables i.e. table A and table B. Table B has primary key of table A as Id column.
I want to get the name and address from two tables.
How can I achieved this?
I tried:
select name,address from tableA join tableB on tableA.id=tableB.id
Your own answer is correct.
Here's the tables you described with the relationships:
I filled the tables with some guid's and copy pasted your own SQL
(50 rows affected)
Try this
Check tableA has values
Select COUNT(*) from [tableA]
And tableB
Select COUNT(*) from [tableB]
if they both have a count > 0
How many items in tableA have at least one value in tableB
--Show all the raw data
Select tableA.*,(select count(*) from tableB where tableA.id = tableB.id)VolumeOfAddresses
FROM tableA
--or Group and count it
Select VolumeOfAddresses,count(*) NameCount
FROM (Select tableA.*,(select count(*) from tableB where tableA.id = tableB.id)VolumeOfAddresses
FROM tableA) a
group by a.VolumeOfAddresses
--or express that as a string of text for simplicity
Select cast(count(*) as varchar(10)) + ' Names exists which each have an address COUNT of ' + cast(VolumeOfAddresses as varchar(10))A_TextString
FROM (Select tableA.*,(select count(*) from tableB where tableA.id = tableB.id)VolumeOfAddresses
FROM tableA) a
group by a.VolumeOfAddresses
The example result from the last query in my testing shows:
50 Names exists which each have an address COUNT of 0
50 Names exists which each have an address COUNT of 1
There is a table called: IDs and another table called Entries.
Not all ids from Ids have entries. I do want to count how many entries have ALL the ids. if an Id has no entry I want to print 0.
Ids have PK: ID and Entries have a column ID.
If I joined them I get only the IDS having entries, but I want to get all of the IDS.
You are using INNER JOIN you can achieve this by using LEFT JOIN instead
EXAMPLE
/* Declare Temperory table for data storage */
DECLARE #MasterTable AS TABLE
(
ID INT
)
DECLARE #EntryTable AS TABLE
(
EntryID INT IDENTITY(1,1)
,MasterId INT
)
--Insert entries to Master Table
INSERT INTO #MasterTable
SELECT 1
UNION
SELECT 2
UNION
SELECT 3
UNION
SELECT 4
--Insert details into details table for only 1 and 2
INSERT INTO #EntryTable
(
MasterId
)
SELECT 1
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 3
SELECT
ID
,COUNT(EntryTable.MasterId) AS EntryCount
FROM
#MasterTable MainTable
LEFT JOIN
#EntryTable EntryTable
ON
MainTable.ID = EntryTable.MasterId
GROUP BY
ID
Use a left join
select ids.id, count(entries.id)
from ids
left join entries on entries.id = ids.id
group by ids.id
Also see this great explanation of joins
SELECT DISTINCT id, EntriesCount.entriesCount
FROM IDs
OUTER APPLY (
SELECT COUNT(id) entriesCount
FROM Entries
WHERE Entries.ids = IDs.id
) AS EntriesCount
outer apply let's you use the id from IDs in the 'where' condition from Entries.
Situation:
I have two tables
Table 1 always has records
Table 2 is the result of a select statement and may or may not have records
Desired Results:
If Table 2 has ANY records, I want only matching records from Table 1. Otherwise, I want all records from Table 1.
I realize I can do this:
DECLARE #count int
SELECT #count=COUNT(*) FROM Table2
IF #count>0
SELECT t1.* FROM Table1 t1 INNER JOIN Table2 t2 ON t1.id=t2.id
ELSE
SELECT * FROM Table1
However, I am trying to avoid IF statements if possible.
Is that even possible?
select t1.*
from Table1 t1
left join
Table2 t2
on t1.id = t2.id
where t2.id is not null -- Match found
or not exists -- Or Table2 is empty
(
select *
from Table2
)