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.
Related
I am working on a MySQl data base and have several tables that I need to join based on a temporary table that I created (layer_2).
Each table that I join has the join key for the next table;
E.g. layer_2 joins with "colegios" table, then the result will have the join key for the next table "ciudades". and finally the result will have the Join key for the next table "departamentos".
from what I got it works but I want to know if there is a way to simplify this?
Select
cuestionario_id,
tiempo,
nro_preguntas,
nro_preguntas_correctas,
tipo_usuario,
fecha_creacion_cuestionario,
con_tiempo,
det_cuestionarios_id,
respuesta_seleccionada,
pregunta_id,
enunciado,
respuesta_correcta,
usuarios_id,
dominio_correo,
tipo_institucion,
materia,
tematica,
area,
nombre_colegios,
direccion_colegios,
nombre_ciudades,
redsaber.departamentos.nombre as nombre_departamentos
From
(
Select
cuestionario_id,
tiempo,
nro_preguntas,
nro_preguntas_correctas,
cuestionable_id,
tipo_usuario,
fecha_creacion_cuestionario,
con_tiempo,
det_cuestionarios_id,
respuesta_seleccionada,
pregunta_id,
enunciado,
respuesta_correcta,
usuarios_id,
dominio_correo,
cole_ciud_id,
tipo_institucion,
materia,
tematica,
area,
combine_ciudad_id,
nombre_colegios,
direccion_colegios,
redsaber.ciudades.nombre as nombre_ciudades,
departamento_id
From
(Select
cuestionario_id,
tiempo,
nro_preguntas,
nro_preguntas_correctas,
cuestionable_id,
tipo_usuario,
fecha_creacion_cuestionario,
con_tiempo,
det_cuestionarios_id,
respuesta_seleccionada,
pregunta_id,
enunciado,
respuesta_correcta,
usuarios_id,
dominio_correo,
cole_ciud_id,
tipo_institucion,
materia,
tematica,
area,
ifnull(redsaber.colegios.ciudad_id,cole_ciud_id) as combine_ciudad_id,
redsaber.colegios.nombre as nombre_colegios,
redsaber.colegios.direccion as direccion_colegios
From layer_2
Left Join redsaber.colegios on layer_2.cole_ciud_id = redsaber.colegios.id
AND tipo_institucion = 'Colegio') as layer_3
Left Join redsaber.ciudades on combine_ciudad_id = redsaber.ciudades.id) as layer_4
Left Join redsaber.departamentos on departamento_id = redsaber.departamentos.id
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
I'm trying to find the greatest number of days (and the reviewer's badge number) it took one of several reviewers to approve a particular document in a workflow. For example, I have a table that holds several workflow approval steps (submitter, manager, controller, QA), along with their badge numbers, and date they approved. The table is called "Workflow" and has those four workflow steps mentioned above as records in the table, and the main table Design that has a one-to-many relationship with Workflow.
I'm trying to determine how many days for the longest review step (number of days), and the badge number of the reviewer for that step (who is holding up the approval workflow, basically). I've been trying to set independent variables to be used later, but not sure how to also set the badge number and I'm confused. I have tried CASE, IIF, and COALESCE but am not having any luck because I don't want the first true value returned and then stop, I want it to continue to evaluate all the steps. Here is an example of my SQL:
declare #managerTime int = 0
declare #controllerTime int = 0
declare #qaTime int = 0
SET #managerTime = (SELECT DATEDIFF(day, manager.BadgeDate, submitter.BadgeDate)
from Design d
left outer join Workflow submitter on (d.DCRId = submitter.DCRId and submitter.RoleName = 'Submitter')
left outer join Workflow manager on (d.DCRId = manager.DCRId and manager.RoleName = 'System Manager')
SET #controllerTime = (SELECT DATEDIFF(day, controller.BadgeDate, manager.BadgeDate)
from Design d
left outer join Workflow manager on (d.DCRId = manager.DCRId and manager.RoleName = 'System Manager')
left outer join Workflow controller on (d.DCRId = controller.DCRId and controller.RoleName = 'DCR Controller')
This is how I would do it:
Create table WorkflowDefinition with flow definiton:
Source Destination Description
Submitter System Manager Submitter -> System Manager
System Manager DCR Controller System Manager -> DCR Controller
DCR Controller QA DCR Controller -> QA
Now we use this table to join workflow elements and calculate greatest number of days:
SET #MaxTime = (SELECT MAX(DATEDIFF(day, source.BadgeDate, destination.BadgeDate))
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
)
When we have this value we can select all workflow elements which took this exact number of days to complete:
Select
destination.BadgeNumber
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
where DATEDIFF(day, source.BadgeDate, destination.BadgeDate) = #MaxTime
If you want to know max value of days for every type of step separately then we can do something like this: Calculate max value of days per step type and put this into temp table:
SELECT
MAX(DATEDIFF(day, source.BadgeDate, destination.BadgeDate)) as maxDays,
flow.Description as StepDescription
into #tmp
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
group by flow.Description
And now use this table to select steps matching max number of days and step description:
Select
destination.BadgeNumber
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
inner join #tmp on DATEDIFF(day, source.BadgeDate, destination.BadgeDate) = maxDays and StepDescription = flow.Description
I ended up tackling this from the "many" table (Workflow), instead of the Design table. I used the following SQL which got me the results I was after. Thank you all for trying to make sense of my ramblings.
select
w.DesignId,
w.RoleName,
w.BadgeNumber,
w.BadgeDate,
DATEDIFF(day,
(select x.BadgeDate from Workflow x
where x.BadgeDate is not null
and x.DesignId = w.DesignId
and x.StepOrder = w.StepOrder - 1),
(select b.BadgeDate from Workflow b
where b.BadgeDate is not null
and b.DesignId = w.DesignId
and b.StepOrder = w.StepOrder))
as StepDuration,
w.StepOrder,
TotalDuration = DATEDIFF(day,
(select y.BadgeDate from Workflow y
where y.RoleName = 'Submitter'
and y.DesignId = w.DesignId),
(select v.BadgeDate from Workflow v
where v.RoleName = 'Approver'
and v.DesignId = w.DesignId)),
d.VersionNumber,
d.Title
from Workflow w
inner join Design d on d.DesignId = w.DesignId
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
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