Cannot get view to work on SQL Server - tsql

I am trying to create a view that includes columns froms several tables.
This is what it looks like:
And this is my query:
SELECT
Billing.WebPortalBilling.WebPortalBillingId,
Billing.WebPortalBilling.CorporationId,
Billing.WebPortalBilling.TokenId,
Billing.WebPortalBilling.GatewaySupportFee,
Billing.WebPortalBilling.GatewayPerTransactionFee,
Billing.WebPortalBilling.PortalPerCustomerFee,
Billing.WebPortalBilling.PortalSupportFee,
Customer.Account.AccountNumber,
Billing.WebPortalBilling.IsActive,
Customer.Customer.Name,
Customer.Customer.TaxCode,
Company.CorporationStructure.Branch
FROM
Company.CorporationStructure
RIGHT OUTER JOIN
Customer.Account ON Company.CorporationStructure.CorporationStructureId = Customer.Account.CorporationStructureId
RIGHT OUTER JOIN
Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch
RIGHT OUTER JOIN
Billing.WebPortalBilling ON Customer.Account.CorporationId = Billing.WebPortalBilling.CorporationId
WHERE
(Billing.WebPortalBilling.IsActive = 1)
It's only returning 1 record, which is not correct. I'm trying to tie the Customer's name back to the WebPortalBilling table along with the account number and branth in the other two tables.
I'm new to sql, so be kind.
Thanks!

As commented the where is killing the outer
Try
SELECT
Billing.WebPortalBilling.WebPortalBillingId,
Billing.WebPortalBilling.CorporationId,
Billing.WebPortalBilling.TokenId,
Billing.WebPortalBilling.GatewaySupportFee,
Billing.WebPortalBilling.GatewayPerTransactionFee,
Billing.WebPortalBilling.PortalPerCustomerFee,
Billing.WebPortalBilling.PortalSupportFee,
Customer.Account.AccountNumber,
Billing.WebPortalBilling.IsActive,
Customer.Customer.Name,
Customer.Customer.TaxCode,
Company.CorporationStructure.Branch
FROM
Company.CorporationStructure
RIGHT OUTER JOIN
Customer.Account ON Company.CorporationStructure.CorporationStructureId = Customer.Account.CorporationStructureId
RIGHT OUTER JOIN
Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch
RIGHT OUTER JOIN Billing.WebPortalBilling
ON Customer.Account.CorporationId = Billing.WebPortalBilling.CorporationId
AND Billing.WebPortalBilling.IsActive = 1

Try this, I think left joins are clearer.
SELECT
B.WebPortalBillingId,
B.CorporationId,
B.TokenId,
B.GatewaySupportFee,
B.GatewayPerTransactionFee,
B.PortalPerCustomerFee,
B.PortalSupportFee,
C.AccountNumber,
B.IsActive,
C.Name,
C.TaxCode,
CS.Branch
FROM Customer.Customer C
LEFT JOIN Company.CorporationStructure CS ON CS.Branch = C.Branch
LEFT JOIN Customer.Account A ON CS.CorporationStructureId = A.CorporationStructureId
LEFT JOIN Billing.WebPortalBilling B ON A.CorporationId = B.CorporationId
WHERE B.IsActive = 1

Related

Is there a way to modify this query and make it more simple?

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

Postgres Error: missing FROM-clause entry for table

I have a query and am using left joins. I have the left join clause as follows:
left outer join ( select pup.brokerage_code, pcz.zip, count (pup.aggregate_id) as VerifiedAgentCount
from partner_user_profiles pup
join partner_user_roles pure on pure.user_profile_id = pup.id
join profile_coverage_zips pcz on pcz.profile_id = pup.id
where lower(pure.role) = 'agent'
and pup.verification_status like 'Verified%'
group by pup.brokerage_code, pcz.zip) vac on vac.brokerage_code = b.brokerage_code and pcz.zip = bcz.zip
However I am getting the error message saying that I am missing the FROM entry clause for "pcz" however I aliased the table in the join clause so I am not sure what is wrong.
You have defined the table alias pcz within the sub-select however the alias no longer exists when the outside the sub-select. At the point you have used it the appropriate alias is the one for the entire sub-select, in this case vac. So: vac.zip = = bcz.zip
left outer join ( select pup.brokerage_code, pcz.zip, count (pup.aggregate_id) as VerifiedAgentCount
from partner_user_profiles pup
join partner_user_roles pure on pure.user_profile_id = pup.id
join profile_coverage_zips pcz on pcz.profile_id = pup.id
where lower(pure.role) = 'agent'
and pup.verification_status like 'Verified%'
group by pup.brokerage_code, pcz.zip
) vac on vac.brokerage_code = b.brokerage_code
and vac.zip = bcz.zip

Linking Assignments to Rubrics in Moodle

Rubrics in Moodle are fine but it's hard to get an overview of all the rubrics used in a course.
I'm trying to write a query that lists all the assignments together with their rubric criteria.
This is my query:
SELECT cou.fullname, ass.name, def.name, cri.description
FROM mdl_course cou
inner join mdl_assign ass on ass.course = cou.id
inner join mdl_course_modules cmod on cmod.course = cou.id
inner join mdl_context ctx on ctx.instanceid = cmod.id
inner join mdl_grading_areas are on are.contextid = ctx.id
inner join mdl_grading_definitions def on def.areaid = are.id
inner join mdl_gradingform_rubric_criteria cri on cri.definitionid = def.id
where cou.fullname like '%rduin%1%'
order by cou.fullname, ass.name, cri.sortorder
It took me about two hours to find the link between Assignments and their grading_definitions, but the query still generates too many records. I guess I have to add another join or relationship, but can't figure out which.
Any help would be appreciated!
I've found the solution: there is a double relationship between (assignments and courses) and coursemoudules:
inner join mdl_course_modules cmod on cmod.course = cou.id and ass.id=cmod.instance

How to optimize a query with hard left join?

I have got a query
SELECT
"athlete"."id" AS "athlete_id"
FROM "athlete"
LEFT JOIN "athlete_in_game" ON athlete.id = athlete_in_game.id_athlete
LEFT JOIN "athlete_in_team" ON athlete.id = athlete_in_team.id_athlete
LEFT JOIN "game" ON
athlete_in_team.id_team = game.id_team_home OR
athlete_in_team.id_team = game.id_team_away OR
athlete_in_game.id_game = game.id
LEFT JOIN "sport_competition" ON sport_competition.id_game = game.id
GROUP BY "athlete"."id"
I need to choose all athletes which played in competitions. But it could be that no data in table "athlete_in_game" for several games and in this case I take all athletes from table "athlete_in_team" for these games. Because of it I use double "OR" in the third left join. I need all written conditions but because of double "OR" it could work too long. Is the chance to optimize it anyway?
i think first you have to improve schema in first place . team related info should be separate table. and game should be separate . you don't have to do left join with game table i think . only left join on athlete_in_game and athlete_in_team would be enough. like So
SELECT
"athlete"."id" AS "athlete_id"
FROM "athlete"
LEFT JOIN "athlete_in_game" ON athlete.id = athlete_in_game.id_athlete
LEFT JOIN "athlete_in_team" ON athlete.id = athlete_in_team.id_athlete
JOIN "game" ON
athlete_in_team.id_team = game.id_team_home OR
athlete_in_team.id_team = game.id_team_away OR
athlete_in_game.id_game = game.id
LEFT JOIN "sport_competition" ON sport_competition.id_game = game.id
GROUP BY "athlete"."id"

Left Join with Entity Framework

someone can tell me how to do this query in EF1:
select a.idAnimali, a.titolo, a.commenti, a.ordine, a.idcatanimali, table1.nomefoto FROM tabanimali as a LEFT JOIN
(SELECT idanimali, nomefoto tabfotoanimali FROM LIMIT 1) AS Table1
On a.idAnimali = table1.idanimali
WHERE a.idcatanimali = idcatanimale
Thanks
Let me know if this works for you, i think what u posed has a typo and i am assuming tabfotoanumali is your second table.
var query = (from a in tabanimali
join p in tabfotoanimali.FirstOrDefault() on a.idanimali equals p.idanimali
where a.idcatanimali = idcatanimale
select new {
a.idAnimali,
a.titolo,
a.commenti,
a.ordine,
a.idcatanimali,
p.nomefoto
}
);