Joining table to other table using either one of two columns - c#

I'm currently writing a program in C# and I want to load friendships from the database based on the id passed as function parameter.
I got 2 tables (I only display the important columns).
Table 1: players
+----------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(15) | NO | | NULL | |
+----------------------+---------------------+------+-----+---------+----------------+
Table 2: messenger_friends
+-------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| user_one_id | int(11) | NO | PRI | NULL | |
| user_two_id | int(11) | NO | PRI | NULL | |
+-------------+---------+------+-----+---------+-------+
The thing is, my idea was the following: in messenger_friends, save one line for a friendship. I know I can save 2 for one friendship but it would mean more storage as 500 friendships would become 1000 records. Now, in my application I have to JOIN messenger_friends to players. I got this function:
public async Task<IReadOnlyList<MessengerFriend>> GetFriends(int playerId)
In here, I need to get all records from messenger_friends where EITHER user_one_id OR user_two_id is playerId. Then in the same query, I want to join it to players. I know I can get the records this way:
SELECT * FROM messenger_friends WHERE user_one_id = {playerId} OR user_two_id = {playerId}
But I'm not sure how to join it to the players table as I need to join EITHER user_one_id OR user_two_id with players.id

SELECT *
FROM players p
JOIN messenger_friends f ON p.id in (f.user_one_id, f.user_two_id)
WHERE p.id = {playerId}
or
SELECT *
FROM players p
JOIN messenger_friends f ON p.id = f.user_one_id
OR P.id = f.user_two_id
WHERE p.id = {playerId}

Related

Create a stored procedure to display rows with a connected column value from another column

I have this SQL Server database already stored
ID | TaxDecNo | OwnerName | PrevTaxDec
----------------------------------------------
1 | 5374 | John | 11135
2 | 9864 | Doe | 7394
3 | 11135 | John | 21784
4 | 7394 | Doe | 6872
5 | 21784 | John | NULL
6 | 6872 | Doe | NULL
When I'm going to display ID 3
ID | TaxDecNo | OwnerName | PrevTaxDec
----------------------------------------------
1 | 5374 | John | 11135
3 | 11135 | John | 21784
5 | 21784 | John | NULL
When I'm going to display RecordID 2
ID | TaxDecNo | OwnerName | PrevTaxDec
----------------------------------------------
2 | 9864 | Doe | 7394
4 | 7394 | Doe | 6872
6 | 6872 | Doe | NULL
The display only stop when no PrevTaxDec connects TaxDec No value.
I got this solution given to me a while but I don't know how to convert it to stored procedure which the data is already inserted the table
DECLARE #ID int = 3;
WITH YourTable AS(
SELECT V.ID,
V.TaxDecNo,
V.PrevTaxDec
FROM (VALUES(1,5374,11135), --This value must already inserted in the table
(2,9864,7394),
(3,11135,21784),
(4,7394,6872),
(5,21784,NULL), --I assume you aren't really mixing datatyoes. 'N/A' can't be inserted into an int column
(6,6872,NULL))V(ID,TaxDecNo,PrevTaxDec)),
--Solution
rCTEup AS(
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM YourTable YT
WHERE YT.ID = #ID
UNION ALL
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM rCTEup r
JOIN YourTable YT ON r.TaxDecNo = YT.PrevTaxDec),
rCTEdown AS(
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM YourTable YT
WHERE YT.ID = #ID
UNION ALL
SELECT YT.ID,
YT.TaxDecNo,
YT.PrevTaxDec
FROM rCTEdown r
JOIN YourTable YT ON r.PrevTaxDec = YT.TaxDecNo)
SELECT ID,
TaxDecNo,
PrevTaxDec
FROM rCTEup
UNION ALL
SELECT ID,
TaxDecNo,
PrevTaxDec
FROM rCTEdown
WHERE ID != #ID; --As it'll be in rCTEup

Better way to insert data in table from other table with ADO.NET

I have two tables:
Table1 - MainTable
---------------------------------------
| MainTableID | CustomerName | BookID |
---------------------------------------
Table2 - BookTable
----------------------
| BookID | BookName |
----------------------
| 1 | physics |
----------------------
| 2 | Math |
----------------------
I want to get the result like this:
---------------------------------------
| MainTableID | CustomerName | BookID |
---------------------------------------
| 1 | Alex | Math |
---------------------------------------
I have list of BookNames in BookTable and I want to insert Data in MainTable. I am using ADO.NET entity data model in visual studio and I am doing this so:
BookTable correspondingBook=(from row in entities.BookTable
where rows.BookName == "Math"
select rows).First();
MainTable itemToAdd = new MainTable();
itemToAdd.CustomerName = "Alex";
itemToAdd.BookID = correspondingBook.BookID;
entities.MainTable.Add(itemToAdd);
entities.SaveChanges();
Is it good solution for this problem? if no, Which will be better?

Linq Account Hierarchy Drill Down

I have two tables, one an invoice table and the other a hierarchy table for the accounts on the invoice
Invoice Table:
| InvoiceNo| AccNo| InvoiceAmount |
--------------------------------------
| A1234| 345| 100.00 |
| A1235| 346| 95.00 |
| A1236| 347| 15.50 |
| A1237| 348| 20.10 |
Hierarchy Table
| AccNo| HierAccNo| Level|
--------------------------------------
| 123| | 1 |
| 789| 123| 2 |
| 890| 123| 2 |
| 345| 789| 3 |
| 346| 789| 3 |
| 347| 890| 3 |
| 348| 890| 3 |
What I'm trying to do is to roll up the amounts from the invoice table to the highest level AccNo which is Level1 and then on a seperate instance from the highest account number roll back down to the next levels.
So far I am able to roll up to the highest band number by the following :
var BandL2 = from invoice in db.Invoices//Roll up to level 2
join ban in db.HierarchyTable
on invoice.AccNo equals ban.Ban
where invoice.GlobalInvoiceID == globalInvoice.Id
group invoice by ban.HierAccNo into bandHierarchy
select new
{
Level2Band = bandHierarchy.Key,
Amount = bandHierarchy.Sum(m=> m.InvoiceAmount)
};
var bandHierarchyTable = db.HierarchyTable.AsQueryable();
var BandL1 = from band2 in BandL2 // Roll Up to level 1
join band1 in bandHierarchyTable
on band2.Level2Band equals band1.Ban
group band2 by band1.HierAccNo into bandL1
select new
{
Level1Band = bandL1.Key,
Amount = bandL1.Sum(m => m.Amount)
};
But now I'm having an issue reversing the process and drilling down from Level 1 as the only details from the form is the AccNo of Level 1(eg. 123).
I'm trying to do this on the fly using pop up modals as I'm drilling.
How do I drill down again so that I can get level by level amounts?
Example:
Ouput Table from Above Code
| AccNo| Amount|
--------------------------------------
| 123| 230.60 |
Then
| AccNo| Amount|
--------------------------------------
| 789| 195 |
| 890| 35.60|
And then clicking on one of the AccNo.
| AccNo| Amount|
--------------------------------------
| 345| 100|
| 346| 95 |
Thanks!
Looks like only leaf accounts can have invoices attached to it, so get the account number, check if it is leaf account, if it is leaf it should be something like that;
var hierAccNo = 123;
var details = from invoice in db.Invoices
join ban in db.HierarchyTable
on invoice.AccNo equals ban.Ban
where invoice.GlobalInvoiceID == globalInvoice.Id
and ban.HierAccNo = HierAccNo;
if it is not leaf you want your original query on level 1 I guess to get the subtotals.
var BandL2 = from invoice in db.Invoices//Roll up to level 2
join ban in db.HierarchyTable
on invoice.AccNo equals ban.Ban
where invoice.GlobalInvoiceID == globalInvoice.Id and ban.HierAccNo = hierAccNo // dont skip this
group invoice by ban.HierAccNo into bandHierarchy
select new
{
Level2Band = bandHierarchy.Key,
Amount = bandHierarchy.Sum(m=> m.InvoiceAmount)
};

Select values from one table based on specific value of another table Linq

I have 2 tables:
Location
id | user_id | latitude | longitude|
1 | 2 | 11.32323 | 11.32323 |
2 | 3 | 12.32323 | 12.32323 |
3 | 4 | 21.32323 | 12.32323 |
Task
id | user_id | status |
1 | 2 | 0 |
2 | 2 | 1 |
3 | 2 | 0 |
4 | 2 | 2 |
5 | 2 | 1 |
6 | 2 | 0 |
7 | 3 | 1 |
8 | 3 | 1 |
9 | 3 | 1 |
I want to select all rows from location table in which users have
either no record in Tasks table (for e.g. user_id = 4)
or if records
exists then all of them must have status equals to 1 (for e.g. user_id
= 3).
In above example, user_id = 2 should not be selected because it has rows in Tasks table with status other than 1.
I am not very much familiar with SQL and LINQ so any help would be appreciated.
This is the expected result:
Result
id | user_id | latitude | longitude|
2 | 3 | 12.32323 | 12.32323 |
3 | 4 | 21.32323 | 12.32323 |
Location with user_id = 2 was ignored because it has some rows in Tasks table with status other than 1.
Location with user_id = 3 was selected because all rows in Tasks table has status = 1.
Location with user_id = 4 was selected because there were no rows in Tasks table with user_id = 4.
Looking at you requirements could be this
select * from location
where user_id not in (select distinct user_id from task )
or user_id not in (select distinct user_id from task where status != 1);
Your conditions are equivalent to saying that no non-"1" value exists in task. I would write this as:
select l.*
from location l
where not exists (select 1 from tasks where t.user_id = l.user_id and t.status = 1);
I prefer not exists to not in because not in will filter out all rows if user_id is ever NULL in tasks.
Using a LEFT JOIN without a sub-SELECT:
SELECT
l.id,
l.user_id,
l.latitude,
l.longitude
FROM
Location l
LEFT JOIN Task t
ON l.user_id = t.user_id
WHERE
t.id IS NULL /* No record in tasks table */
OR (t.id IS NOT NULL AND l.status = 1) /* if records exists then all of them must have status equals to 1 */

LINQ join tables with value if no match

I know that there are some examples but I could not apply them on my code. I am pretty new to Linq and SQL. I have two tables I want to join.
First Table:
--------------
| Id | Count |
--------------
| 1 | 10 |
--------------
| 2 | 4 |
--------------
Second Table:
--------------
| Id | Name |
--------------
| 1 | Tom |
--------------
| 2 | John |
--------------
| 3 | Nick |
--------------
| 4 | Max |
--------------
As you can see, the second table has more records than the first. My goal is to join them based on the Id. The problem is that after I have joined the tables it only displays the matching records, which is Id 1 and 2. Though I want to display every Id (from 1 to 4) and if there is no match in both tables, there should be a default value 0.
It should look like this:
----------------------
| Id | Name | Count |
----------------------
| 1 | Tom | 10 |
----------------------
| 2 | John | 4 |
----------------------
| 3 | Nick | 0 |
----------------------
| 4 | Max | 0 |
----------------------
So far I have got this code:
// first table
var listCount = entity.tblKundes.Where(x => x.Studio == 2)
.Select(x => new { x.Id, x.Name})
.GroupBy(x => x.Name).ToList();
// second table
var listBerater = entity.tblUsers.Where(x => x.Studio == 2)
.Select(x => new { x.Id, x.Name})
.ToList();
// This join should be edited so that it displays non matching records as well
var test = listCount.Join(
listBerater,
count => count.Key,
berater => berater.Id,
(count, berater) => new { listCount = count, listBerater = berater }
).ToList();
Edit:
var test2 = (from list in listCount
join berater in listBerater on list.Berater equals berater.Id into gj
from sublist in gj.DefaultIfEmpty()
select new { sublist.Id, sublist.Nachname, sublist.Vorname }).ToList();
There is a typical concept in every Structured Query Languages which is called "Left join". Left-Join means that you will have all rows of data from first table even there is no equivalent in the second one. "Inner-Join" is a little different and only looks for matched rows of data.
Here you can find enough and complete information about your issue.
Left Join

Categories