I'm trying to determine the following from the joining of two tables:
Sample TableA: Contains 1 column consisting of unique IDs
ID Name Department
1 John IT
2 Jason Sales
3 Dany IT
4 Mike HR
5 Alex HR
Sample TableB: Contains multiple columns, including ID from TableA. For example:
ID AccountNumber WebID
1 10725 ABC1
1 10726 ABC1
1 10727 ABC1
2 20100 ABC2
2 20101 ABC2
3 30100 ABC3
4 40100 NULL
I want to get the following results:
ID Name WebID
1 John ABC1
2 Jason ABC2
3 Dany ABC3
4 Mike NULL
5 Alex NULL
I tried the following query, which is returning the correct rows for these sample tables:
Select count(a.ID), a.ID, a.Name, b.WebID from TableA a
left join TableB b on a.ID = b.ID
group by a.ID, a.Name, b.WebID
But my Actual Database tables, this query does not return correct number of rows: (30992)
TableA contains 29066 rows and TableB contains 23033 rows
The query should return 29066 rows, as it is Left Join.
When I checked the IDs that are in TableA, but not in TableB, there were 6033 rows:
Select * from TableA where ID not in (Select ID from TableB)
Am I missing something in the query?
TABLE B has duplicates of the ID column... the code below should work (but might not be the results you expect since I just do a max on the webid column which is fine if it is always the same but I need a rule if not)
I just saw you had a count... I added that in.
SELECT A.ID, A.Name, B.WebID
FROM TABLEA A
LEFT JOIN (
SELECT ID, MAX(WebID) AS WebID, count(*) as CNT
FROM TABLEB
GROUP BY ID
) B ON A.ID = B.ID
I think your query is as simple as that:
select a.ID,a.Name,b.WebID
from TableA a
left join TableB b on a.ID = b.ID
Related
I want to select all records from table 1 and if there any match between table 1 and table 2 then I have to exclude that records.
Note: The requirement is I have to use left outer join for that..
For example,
table 1 table 2
1 1
2 2
3
Output should be 3
I have written query in SQL but I want this in SQL LINQ like from a in dbContext.Table1....
select t1.*
from table1 t1
left outer join table2 t2 on t1.ID = t2.ID and t1.Code = t2.Code
where s.ID is null
How to solve this?
var result = (from primer in one
join primer2 in two on primer.id equals primer2.id into gj
where gj.Count()==0
select new
{
primer.id
}).ToList();
Where ONE - it is first table, TWO - it is second table.
Instead of
...select new
{
primer.id
}...
You can use
...
select primer
...
I have 3 tables in sql
ProductMaster with column ProductName
PurchaseData with column ProductName and Quantity
SaleData with column ProductName and Quantity
Please guide me to write query for table join to get the result as under
PRODUCT1 10 5 5
PRODUCT2 10 0 10
PRODUCT3 10 5 5
You don't need anymore ProductMaster Table, because all information you need are in PURCHASEDATA and SALEDATA
SELECT P.[ProductName] as ProductName, P.PurchaseQty, S.SalteQTY, P.purchaseQTY-S.SalteQTY as ClosingQty
FROM [PURCHASEDATA] P LEFT OUTER JOIN [SALEDATA] S ON P.ProductName=S.ProductName
Try to start with something like this:
SELECT [ProductMaster].[ProductName]
,[PurchaseData].[Quantity] AS [PurchasedQuantity]
,[SaleData].[Quantity] AS [SoldQuantity]
,[PurchaseData].[Quantity]
- [SaleData].[Quantity] AS [ClosingStock]
FROM [ProductMaster]
LEFT OUTER JOIN [PurchaseData]
ON [ProductMaster].[ProductName] = [PurchaseData].[ProductName]
LEFT OUTER JOIN [SaleData]
ON [ProductMaster].[ProductName] = [SaleData].[ProductName]
The idea is to have the master table and join the two dependent tables to the master with an INNER JOIN on the ProductName field.
hi I'm using 4 tables..
Sometimes it does not have information in the this tables(education or address or certificate)
so the results are coming empty..
If have information how can I say?
example
**PersonTable**
PersonId PersonName
1453 george
**Certificate Table**
CertificateId PersonId CertificateName ( 0 records)
**Addres Table**
AddresId PersonId Adress
1 1453 ......
2 1453 .....
3 1453 ......
4 1453 ......
5 1453 ........
**EducationTable**
EducationId PersonId Education
1 1453 school name
**BankTable**
BankId PersonId Bank ( 0 records )
Person table = 1 records
Certificates = 0 records
Address = 5 records
Education = 1 records
Bank = 0 records
var query = (from p in DbEntities.t_Person
join q in DbEntities.t_Address on p.PersonId equals q.addressId
join n in DbEntities.t_Education on p.PersonId equals n.EduId
join j in DbEntities.t_Certificate on p.PersonId equals j.CertificateId
join f in DbEntities.t_BankAccount on p.PersonId equals f.BankId
where p.PersonId.Equals(id)
select new { p, q, n, j, f }).FirstOrDefault();
return query values null ,
I do not see why these values?
Person table = 1 records
Address = 5 records
Education = 1 records
I thought about it, but did not
var query = (from p in DbEntities.t_Person
where p.PersonId.equlas(id)
select p).singleOrDefault();
query = from p in dbEntities.Address
WHERE P.AddressId.equlas(query.PersonId)
select p;
It's because you're doing an INNER JOIN, which will only return records if all join conditions are met! It looks like you want to do a LEFT JOIN, which will return all rows from the left table, and from the right table it will return any record that is found, and null otherwise. Here is a good post explaining the different types of joins, with diagrams!
The Linq-to-Entities way to do a left join is DefaultIfEmpty(). See MSDN for a usage example.
Here is 2 Tables that are joined by the StaffID
Job Table
=========
JobID AssignedTo(StaffID) Created By(StaffID)
1 2 1
2 3 2
Staff Table
============
StaffID Name
1 May
2 Bob
3 Mary
I need An SQL Statement to get the job details with the corresponding staff name but have problems doing so as i'm unable to differentiate the columns as they are using the same table. The end result should look like this
JobID Assigned To Created By
1 Bob May
2 Mary Bob
You need to join Staff table twice
select J.JobId, S1.Name AS AssignedTo, S2.Name AS CreatedBy
from Job J
inner join Staff S1 on S1.StaffID = J.AssignedTo
inner join Staff S2 on S2.StaffID = J.CreatedBy
I am having a bit of trouble with my SQL query.
I have two tables:
Table1
id guid title D0 D1 D2
-----------------------------------------
1 guid1 Title1 0.123 -0.235 0.789
2 guid2 Title2 -0.343 0.435 0.459
3 guid3 Title3 0.243 -0.267 -0.934
...
100 guid4 Title100 -0.423 0.955 0.029
and Table 2 (note it has the same schema, just different data).
id guid title D0 D1 D2
----------------------------------------
1 guid1 Title1 0.233 -0.436 -0.389
2 guid2 Title2 -0.343 0.235 0.789
3 guid3 Title3 0.573 -0.067 -0.124
...
100 guid4 Title100 -0.343 0.155 0.005
I am trying to figure out how to write a SELECT statement which returns all the titles WHERE all the combinations of ABS(Table1_D0*Table2_D0)+ABS(Table1_D1*Table2_D1)+ABS(Table1_D2*Table2_D2) are less than a thresholded value (hard coded probably).
So far I am trying to use a CROSS JOIN, but I am not sure if this is the correct approach.
Does this make sense? Table1, row1 against all the rows of Table2, then Table1, row2 against all the rows of Table2.
If it matters, I am using MS SQL.
Many thanks!
Brett
SELECT t1.title
FROM Table1 t1
CROSS JOIN table2 t2
WHERE ABS(t1.D0*t2.D0)+ABS(t1.D1*t2.D1)+ABS(t1.D2*t2.D2)<10
SELECT *
FROM Table1 a inner join
Table2 b on a.Id=b.Id
where ABS(a.D0*b.D0)+ABS(a.D1*b.D1)+ABS(a.D2*b.D2)<=#Value
CROSS JOIN is the right choice and CROSS JOIN is just the same as no join at all (see Snowbear answer).
select t1o.title
from Table1 t1o
where not exists
(
-- none of the cross-joined rows for t1o must be above the threshold
select t1.title
from Table1 t1
cross join Table2 t2
where t1.id = t1o.id -- only the cross joined rows for the current t1o row
-- inverted b/c not exists
and abs(t1.D0*t2.D0)+abs(t1.D1*t2.D1)+abs(t1.D2*t2.D2) > 10
)