how to create multiple join in ef? - entity-framework

i have 3 tables in my db and 2 of these tables have a key in another one .
how can i create this 2 join in entity frame work(version 5 or 6 , database first)?
tbl1
-----------
tbl1ID
tbl1name tbl3
-----------
tbl3ID
---inner join on tbl1ID and tbl2ID ---> tbl1ID
tbl2ID
tbl3name
tbl2
-----------
tbl2ID
tbl2name
i want this result:
result (columns)
--------------------
tbl1ID
tbl2ID
tbl3ID
tbl1name
tbl2name
tbl3name

following code snippets may help you:
(from c in db.tbl1
join d in db.tbl3 on c.tbl1ID equals d.tbl1ID
join e in db.tbl2 on d.tbl2ID equals e.tbl2ID
select new { c.tbl1ID, d.tbl2ID, e.tbl3ID, c.tbl1Name, d.tbl2Name, e.tbl3Name}).ToList();

If you are using entity framework then you doesn't need to use join you can directly access the results from tbl3 object. Use following:
tbl3.tlb1.tbl1ID
tbl3.tbl2.tbl2ID
tbl3ID,
tbl3.tlb1.tbl1name,
tbl3.tbl2.tbl2name,
tbl3name

Related

T-SQL Question for Getting One Customer Type When There Can be More Than One Value

We have an organization that can have more than one customer type basically. However, what a user wants to see is either the partner or direct type (customer type is either Direct, Partner1, Partner2, or Partner3 but can be direct plus a partner value but only can be one of the partner values). So if a customer is both (ex: Direct and Partner1) they just want the type that is a partner (ex: Partner1). So I tried splitting out partners only into one temp table from a few tables joining together different org data. I have the same query without any limit pulling into a different temp table. Then I calculate count and put that into a temp table. Then I tried gathering data from all the temp tables. That is where I run into trouble and lose some of the customers where the type is direct (I have a image link below for a directcustomer and a customer who is both). I have been out of SQL for a bit so this one is throwing me...I figure the issue is the fact that I have a case statement referencing a table that a direct customer will not exist in (#WLPO). However I am not sure how to achieve pulling in these customers while also only selecting which partner type it is for a customer that has a partner and is also direct. FYI using MSSMS for querying.
If OBJECT_ID('tempdb..#WLPO') IS NOT NULL
DROP TABLE #WLPO
IF OBJECT_ID('tempdb..#org') IS NOT NULL
DROP TABLE #org
IF OBJECT_ID('tempdb..#OrgCount') IS NOT NULL
DROP TABLE #OrgCount
IF OBJECT_ID('tempdb..#cc') IS NOT NULL
DROP TABLE #cc
Select
o.OrganizationID,
o.OrganizationName,
os.WhiteLabelPartnerID,
s.StateName
INTO #WLPO
from [Org].[Organizations] o
join [Org].[OrganizationStates] os on o.OrganizationID=os.OrganizationID --and os.WhiteLabelPartnerID = 1
join [Lookup].[States] s on os.StateID = s.StateID
join [Org].[PaymentOnFile] pof on pof.OrganizationID=o.OrganizationID
where os.WhiteLabelPartnerID in (2,3,4)
and os.StateID in (1, 2, 3)
and o.OrganizationID = 7613
select * from #WLPO
Select
o.OrganizationID,
o.OrganizationName,
os.WhiteLabelPartnerID,
s.StateName
INTO #org
from [Org].[Organizations] o
join [Org].[OrganizationStates] os on o.OrganizationID=os.OrganizationID --and os.WhiteLabelPartnerID = 1
join [Lookup].[States] s on os.StateID = s.StateID
join [Org].[PaymentOnFile] pof on pof.OrganizationID=o.OrganizationID
where 1=1--os.WhiteLabelPartnerID = 1
and os.StateID in (1, 2, 3)
and o.OrganizationID = 7613
select * from #org
Select
OrganizationID,
count(OrganizationID) AS CountOrgTypes
INTO #OrgCount
from #org
where OrganizationID = 7613
group by OrganizationID
select * from #OrgCount
Select distinct
ct.OrganizationID,
ok.OrganizationName,
ct.CountOrgTypes,
case when ct.CountOrgTypes = 2 then wlp.WhiteLabelPartnerID
when ct.CountOrgTypes = 1 then ok.WhiteLabelPartnerID
END AS CustomerTypeCode,
case when ct.CountOrgTypes = 2 then wlp.StateName
when ct.CountOrgTypes = 1 then ok.StateName END As OrgState
INTO #cc
from #org ok
left join #WLPO wlp on wlp.OrganizationID=ok.OrganizationID
join #OrgCount ct on wlp.OrganizationID=ct.OrganizationID
select * from #cc
Select
OrganizationID,
OrganizationName,
CountOrgTypes,
case when CustomerTypeCode = 1 then 'Direct'
when CustomerTypeCode = 2 then 'Partner1'
when CustomerTypeCode = 3 then 'Partner2'
when CustomerTypeCode = 4 then 'Partner3' ELSE Null END AS CustomerType,
OrgState
from #cc
order by OrganizationName asc
DirectCustomer
CustomerwithBoth

Orientdb sql , select from multiple tables

dont run "select from multiple tables" commands in orientdb 3.0 (centos)
i tested like this following commands
SELECT *
FROM Employee A, City B
WHERE A.city = B.id
Error Codes ; "Error parsing query: ^ Encountered " "SELECT "" at line 1, column 1. Was expecting one of: ... ... ";" ... DB name="
The most important difference between OrientDB and a Relational Database is that relationships are represented by LINKS instead of JOINs.
For this reason, the classic JOIN syntax is not supported. OrientDB uses the "dot (.) notation" to navigate LINKS. Example 1 : In SQL you might create a join such as:
SELECT *
FROM Employee A, City B
WHERE A.city = B.id
AND B.name = 'Rome'
In OrientDB an equivalent operation would be:
SELECT * FROM Employee WHERE city.name = 'Rome'
For more information: https://orientdb.com/docs/2.2.x/SQL.html#joins
Hope it helps
Regards

Copy join-query from database to another datable

Previously, I am using dblink to achieve the mission but it involved copy one query only. What if I have doing the join query (4 tables) in one database, then i want to copy the data output into another database.Anyone know about it ?
select
a.sysname, a.ip, b.host_id, b.resource_name, b.resource_id
, c.metric_id, d.metric_name, c.value, c.resource_id
, to_timestamp(c.date_id)as datetime
from inv.el a
inner join inv.if b on a.host_id = b.host_id
inner join me.me_cr c on b.resource_id = c.resource_id
inner join inv.me d on c.metric_id = d.metric_id
where date_id = (
select max(date_id) from me.me_cr
)
you can try using postgres_fdw on Release > 9.6 as it
... now supports remote joins...
https://www.postgresql.org/docs/9.6/static/release-9-6.html

How to integrate WITH Recursive in a Querydsl query

I'm new to Querydsl and I'm struggling to figure out how to implement the following query:
WITH RECURSIVE results AS
(
SELECT id,
parent_id
FROM project
WHERE id = '8a3d6714-27fa-4d1f-962f-9047d616ab42'
UNION ALL
SELECT t.id,
t.parent_id
FROM project t
INNER JOIN results r ON r.parent_id = t.id
)
SELECT *
FROM results;
Its objective is to, starting from the bottom of a hierarchy, collect the anchor's parent (8a3d6714-27fa-4d1f-962f-9047d616ab42 in the above example; there is only one parent per row) all the way up to a point where there is a element without a parent. If successful, using my database, it should yield the following:
id parent_id
------------------------------------ ------------------------------------
8a3d6714-27fa-4d1f-962f-9047d616ab42 babc74e8-1b6f-49e8-a1e3-a176fce2975d
babc74e8-1b6f-49e8-a1e3-a176fce2975d 3f83c9a2-bf43-46d8-bf87-070f5b55ae5a
3f83c9a2-bf43-46d8-bf87-070f5b55ae5a 69c074c6-a329-42c3-8e2e-5da9ab0ef81e
69c074c6-a329-42c3-8e2e-5da9ab0ef81e bccab264-027c-4c4f-9ae8-3409efc6aeb3
bccab264-027c-4c4f-9ae8-3409efc6aeb3 227db39a-2219-4abb-a2a7-3d28bf47ac01
227db39a-2219-4abb-a2a7-3d28bf47ac01
Any thoughts would be much appreciated.
Regards
Rodrigo
QProjects results = new QProjects("results");
QProjects p = new QProjects("p");
QProjects t = new QProjects("t");
String id = "8a3d6714-27fa-4d1f-962f-9047d616ab42";
query.withRecursive(results, subQuery()
.unionAll(
subQuery().from(p).where(p.id.eq(id)).list(p.id, p.parentId),
subQuery().from(t).innerJoin(results).on(results.parentId.eq(t.id)).list(t.id, t.parentId)))
.from(results)
.list(results.id, results.parentId);

Inner join 4 columns with different server .., different database ., and different table

I have 3 server:
ECPAYSERV2
POSSQLSERVER
ECPAYSERV1
3 databases
ECPNWEB
ECPNPOS
ECPNDB
and 3 tables
account
branch
terminal
I have also finish to link the server :
ECPAYSERV2.ECPNWEB.dbo.account
POSSQLSERVER.ECPNPOS.dbo.branch
ECPAYSERV1.ECPNDB.dbo.terminal
The fields that should be seen would be the following
AccountID = ECPAYSERV2.ECPNWEB.dbo.account.(accntid)
BranchID = ECPAYSERV2.ECPNWEB.dbo.branch.(branchid)
TID = ECPAYSERV1.ECPNDB.dbo.terminal.(TID)
Store # = POSSQLSERVER.ECPNPOS.dbo.branch.(Storeno)
Store name = ECPAYSERV2.ECPNWEB.dbo.account.(accountholder)
Branch name = ECPAYSERV2.ECPNWEB.dbo.brachn.(branchname) , POSSQLSERVER.ECPNPOS.dbo.branch.(branchname) , ECPAYSERV1.ECPNDB.dbo.terminal.(retail_store_code)
The data enclosed with parethesis are the columns.,
To be more clear of all this are the columns in the table..
Table : Account
|Accntid|managedby|Accountholder|Description|AccountType|ContactPerson|ContactNumber|EmailAddress|
Table : Branch
|BranchID|BranchName|AccountID|StoreNo|Description|Status|
Table : Terminal
|TerminalID|TID|retail_store_code|t_distributor_code|
All I want is to Get this output with RIGHT data ..
|accntid|accountholder|tid|storeno|branchname|branchid|
This is what I tried for but not getting the RIGHT output
SELECT account.accntid,account.accountholder,terminal.tid,branch.storeno,branch.branchname,branch1.branchid
FROM ECPAYSERV2.ECPNWEB.dbo.account as account
INNER JOIN POSSQLSERVER.ECPNPOS.dbo.branch as branch
ON account.accntid=branch.branchid
INNER JOIN ECPAYSERV2.ECPNWEB.dbo.branch as branch1
ON account.accntid=branch1.branchid
INNER JOIN ECPAYSERV1.ECPNDB.dbo.terminal as terminal
ON account.accntid=terminal.tid
Please help me about This matter Thanks :(
SELECT
account.accntid, account.accountholder, terminal.tid, branch.storeno, branch.branchname, branch1.branchid
FROM
ECPAYSERV2.ECPNWEB.dbo.account as account
INNER JOIN POSSQLSERVER.ECPNPOS.dbo.branch as branch
ON account.accntid=branch.AccountID
INNER JOIN ECPAYSERV2.ECPNWEB.dbo.branch as branch1
ON account.accntid=branch1.AccountID
INNER JOIN ECPAYSERV1.ECPNDB.dbo.terminal as terminal
ON account.accntid=terminal.???
the ??? is not correct, but it is not clear which column of terminal is a FK for account.