Returning distinct columns from left outer join in db2 - db2

SELECT
nzy.NZPYYD
,nzy.NZZSYG
,nzy.NZJRYG
,acn.ANITCD
FROM
ACNTRA acn
LEFT OUTER JOIN NZYTFL nzy
ON (
nzy.NZCNO1 = acn.ANCNO1
AND nzy.NZCNO2 = acn.ANCNO2
AND nzy.NZCNO3 = acn.ANCNO3
AND nzy.NZCNO4 = acn.ANCNO4
AND nzy.NZCNO5 = acn.ANCNO5
AND nzy.NZSLKI = acn.ANSLKI
AND nzy.NZDLTM = ''
)
WHERE
acn.ANDLTM = ''
AND acn.ANTKCD = '1029'
AND nzy.NZTXKB = 1
The problem here is it gives 2 rows result.I want to get one unique row from the result of left outer join .Any help?

If both rows are identical, try
SELECT DISTINCT
nzy.NZPYYD
,nzy.NZZSYG
,nzy.NZJRYG
,acn.ANITCD
If not, you can try to SUM(), CONCAT(), MAX() or whatever the column with different values.
Difficult to be more precise without a sample output.

Related

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

How to select from subquery if column contains a specific value in postgre

I would like to ask if it is possible to select again from a result set if a column contains a specific value?
For example, from the below query I want to select it as subquery and check if that subquery's first column contains both 2 and 3 result. Otherwise, no values should be return.
select e.evaluator_id, ROUND(avg(cast(e.rating_score as int))::numeric,1)::varchar, c.q_category_name
from tms.t_evaluation e
inner join tms.m_q_category c
on e.nendo=c.nendo
and e.q_category_id = c.q_category_id
and c.delete_flg = '0'
inner join tms.m_q_subcategory qs
on e.q_category_id = qs.q_category_id
and e.q_subcategory_id = qs.q_subcategory_id
and c.nendo = qs.nendo
and qs.delete_flg = '0'
where e.nendo = '2018'
and e.empl_id = 'empl05'
and e.delete_flg = '0'
and e.evaluator_id in ('2' , '3')
group by e.empl_id, e.nendo, e.q_category_id,
c.q_category_name, e.evaluator_id, e.history_no
Result contains both 2 and 3 in first column. Is this possible?
select e.evaluator_id, ROUND(avg(cast(e.rating_score as int))::numeric,1)::varchar, c.q_category_name
from tms.t_evaluation e
inner join tms.m_q_category c
on e.nendo=c.nendo
and e.q_category_id = c.q_category_id
and c.delete_flg = '0'
inner join tms.m_q_subcategory qs
on e.q_category_id = qs.q_category_id
and e.q_subcategory_id = qs.q_subcategory_id
and c.nendo = qs.nendo
and qs.delete_flg = '0'
where e.nendo = '2018'
and e.empl_id = 'empl05'
and e.delete_flg = '0'
and e.evaluator_id in (select case when evaluator_id=2 or evaluator_id=3 then evaluator_id else null from t_evaluation order by evaluator_id asc)
group by e.empl_id, e.nendo, e.q_category_id,
c.q_category_name, e.evaluator_id, e.history_no

Cannot get view to work on SQL Server

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

Postgres Complex Select in a View

I have this select clause that is working perfect:
SELECT
"Aspectos"."ID" AS "Aspecto Normativo ID",
"Aspectos"."Aspecto" AS "Aspecto Normativo",
"Fatores"."ID", "Fatores"."Fator" AS "Fator Normativo",
"Diagnostico"."Vinculo_Final",
"Fatores_1"."ID",
"Fatores_1"."Fator" AS "Fator Determinativo",
"Aspectos_1"."ID" AS "Aspecto Determinativo ID",
"Aspectos_1"."Aspecto" AS "Aspecto Determinativo",
Count("Itens"."ID") AS "No Itens",
Count("Itens"."ID") AS "Pri"
FROM "Diagnostico" INNER JOIN ("Aspectos" AS "Aspectos_1"
INNER JOIN (("Fontes" INNER JOIN "Itens" ON "Fontes"."ID" = "Itens"."Fonte")
INNER JOIN ("Fatores" AS "Fatores_1"
INNER JOIN ("Aspectos"
INNER JOIN ("Vinculos"
INNER JOIN "Fatores"
ON "Vinculos"."Fator_Normativo" = "Fatores"."ID")
ON ("Aspectos"."ID" = "Fatores"."Aspecto")
AND ("Aspectos"."ID" = "Fatores"."Aspecto"))
ON "Fatores_1"."ID" = "Vinculos"."Fator_Determinativo")
ON "Itens"."ID" = "Vinculos"."Item")
ON "Aspectos_1"."ID" = "Fatores_1"."Aspecto")
ON "Diagnostico"."ID" = "Vinculos"."Diagnostico_ID"
GROUP BY "Aspectos"."ID", "Aspectos"."Aspecto",
"Fatores"."ID", "Fatores"."Fator",
"Diagnostico"."Vinculo_Final",
"Fatores_1"."ID",
"Fatores_1"."Fator",
"Aspectos_1"."ID",
"Aspectos_1"."Aspecto"
ORDER BY "Aspectos"."ID", "Aspectos_1"."ID",
"Fatores"."Fator", "Fatores_1"."Fator";
But when I try to CREATE A VIEW with this same select I'm getting thuis error:
ERROR: column "ID" specified more than one time
Can anybody help me on this.
Thanks
You have "Fatores"."ID" (line 4) and "Fatores_1"."ID" (line 6). Give them different aliases.
For such complex queries it is recommended to have only 1 (one) column in per line in the statement for better visibility. Also it is recommended to always give aliases to the columns.

postgres sql query counting two sets of objects

I have this join query below. I am having one issue with it.
The shippedUnits column comes out wrong. It just counts the total items. The issue is I am counting two sets of objects that have everything in common except the shipped field.
SELECT count(*) as "units", (select count("EsnsVmas".*) from "EsnsVmas" where
"EsnsVmas"."shipped" = false AND "EsnsVmas"."VmaId" = 2 AND "EsnsVmas"."approved" =
true) as "shippedUnits", "Vmas".*, "Vendors"."name" as "name", "EsnsVmas".id as
"OrderItemId", "PurchaseOrderItems"."price", "Grades"."name" as "vendorGrade",
"Items"."name" as "model", "Items".id as "ItemId", "Grades".id as "GradeId"
FROM "Vmas"
JOIN "EsnsVmas" on ("EsnsVmas"."VmaId" = "Vmas".id)
JOIN "Esns" on ("EsnsVmas"."EsnId" = "Esns".id)
JOIN "PurchaseOrderItems" on ("Esns"."PurchaseOrderItemId" =
"PurchaseOrderItems".id)
JOIN "Items" on ("PurchaseOrderItems"."ItemId" = "Items".id)
JOIN "Grades" on ("PurchaseOrderItems"."GradeId" = "Grades".id)
JOIN "Vendors" on ("Vmas"."VendorId" = "Vendors".id)
WHERE "Vmas".id =2 AND "EsnsVmas"."approved" = true
GROUP BY "Vmas".id, "PurchaseOrderItems".id, "Grades".id, "Items".id, "Vendors".id, "EsnsVmas".id ;
It still seems that there is no relation between the inner and outer queries other than the hardcoded VmaId = "2". Since I don't have the tables in a database I can't test to make sure it works - but here's my first shot at a possible fix:
SELECT count(*) as "units", (select count("EsnsVmas".*) from "EsnsVmas" e2 where
e2."shipped" = false AND e2."VmaId" = "Vmas".id AND e2."approved" =
true AND "EsnsVmas".id = e2.id) as "shippedUnits", "Vmas".*, "Vendors"."name" as "name", "EsnsVmas".id as
"OrderItemId", "PurchaseOrderItems"."price", "Grades"."name" as "vendorGrade",
"Items"."name" as "model", "Items".id as "ItemId", "Grades".id as "GradeId"
FROM "Vmas"
JOIN "EsnsVmas" on ("EsnsVmas"."VmaId" = "Vmas".id)
JOIN "Esns" on ("EsnsVmas"."EsnId" = "Esns".id)
JOIN "PurchaseOrderItems" on ("Esns"."PurchaseOrderItemId" =
"PurchaseOrderItems".id)
JOIN "Items" on ("PurchaseOrderItems"."ItemId" = "Items".id)
JOIN "Grades" on ("PurchaseOrderItems"."GradeId" = "Grades".id)
JOIN "Vendors" on ("Vmas"."VendorId" = "Vendors".id)
WHERE "Vmas".id =2 AND "EsnsVmas"."approved" = true
GROUP BY "Vmas".id, "PurchaseOrderItems".id, "Grades".id, "Items".id, "Vendors".id, "EsnsVmas".id ;
This changed two things. First was relating back with "EsnsVmas".id = e2.id at the end of the subselect (I aliased the EsnsVmas table in the subselect to e2). The other thing was changing e2."VmaId" = "Vmas".id (instead of hardcoding it to 2) which shouldn't affect the output but allows you to change the id in only one place.