Why Group By is not working as expected in PostgreSQL? - postgresql

I m doing a query and it is showing me the output but not as expected.
Above image it is giving me two extra row (blue indicator) after doing group by which not should be exists in the output. Here is my query
SELECT som.customer_po,
pro.product_id,
pro.product_name,
som.mo_id,
ri.status
FROM schema_order_map som
JOIN product pro ON som.label_reference_id = pro.product_id
JOIN risk_information ri ON som.customer_po = ri.customer_po
WHERE ri.created_by = 18
AND ri.product_id = som.label_reference_id
GROUP BY som.customer_po,
pro.product_id,
product_name,
som.mo_id,
ri.status
I tried different way but it is giving me the same result.

It solves my problem somehow. but i don't know it is best practice or not
SELECT max(som.customer_po) AS customer_po,
max(pro.product_id) AS product_id,
max(pro.product_name) product_name,
max(som.mo_id) mo_id,
max(ri.status) AS risk_status
FROM schema_order_map som
JOIN product pro ON som.label_reference_id = pro.product_id
JOIN risk_information ri ON som.customer_po = ri.customer_po
WHERE ri.created_by = 18
AND ri.product_id = som.label_reference_id
GROUP BY som.customer_po,
pro.product_id

Related

The multi-part identifier could not be bound - SQL Server 2016

SELECT clm.CLCL_PAYEE_PR_ID, clm.SBSB_CK, clm.CLCL_ID, clm.clcl_id_adj_to,clm.clcl_id_adj_from, clm.CLCL_PAID_DT
FROM ODW.DW.fac_cmc_clcl_claim CLM
INNER JOIN ODW.DW.fac_cmc_meme_member MEME ON MEME.meme_ck = CLM.meme_ck
INNER JOIN ODW.DW.fac_cmc_mepe_prcs_elig MEPE ON MEPE.meme_ck = MEME.meme_ck
INNER JOIN ODW.DW.fac_cmc_mepr_prim_prov MEPR ON MEPE.meme_ck = MEPR.meme_ck AND CLM.clcl_prpr_id_pcp = MEPR.prpr_id
INNER JOIN ODW.DW.fac_cmc_sbsb_subsc SBSB ON MEME.sbsb_ck = SBSB.sbsb_ck
INNER JOIN ODW.DW.fac_cmc_prpr_prov PROV ON MEPR.prpr_id = PROV.prpr_id AND PROV.prpr_mctr_prty = 'RISK'
INNER JOIN ODW.DW.fac_cmc_prer_relation PRER ON PRER.prpr_id = MEPR.prpr_id
INNER JOIN ODW.DW.fac_cmc_plds_plan_desc PLDS ON MEPE.cspi_id = PLDS.cspi_id
INNER JOIN ODW.DW.fac_cmc_pdds_prod_desc PDDS ON MEPE.pdpd_id = PDDS.pdpd_id
WHERE CLM.clcl_paid_dt BETWEEN '2019-12-24 00:00:00.000' AND '2019-12-30 23:59:59.997'
AND CLM.clcl_cur_sts = '02'
AND CLM.clcl_cl_type = 'M'
AND CLM.clcl_cl_sub_type = 'H'
AND CLM.grgr_ck IN (46)
AND MEPR.grgr_ck IN (46)
AND MEPE.grgr_ck IN (46)
AND MEPE.mepe_elig_ind = 'Y'
AND CLM.clcl_low_svc_dt BETWEEN MEPE.mepe_eff_dt AND MEPE.mepe_term_dt
AND CLM.clcl_low_svc_dt BETWEEN MEPR.mepr_eff_dt AND MEPR.mepr_term_dt
AND SBSB.grgr_ck IN (46)
AND PRER.prer_prpr_entity = 'I'
AND PRER.prer_prpr_id IN ('64456546')
AND (PLDS.plds_desc LIKE '%risk%' OR PDDS.pdds_desc LIKE '%risk%');
This query runs in PROD with different variables which substitute the value of the hard coded values. It runs around 100 times per day in PROD and on some days some of the runs fail due to this error:
The multi-part identifier "PDDS.pdds_desc" could not be bound
Please note that all the joins are being done on views.
When I re-run the failed process, it succeeds the second time with no changes to the underlying query.
Can anyone suggest what could be the issue. Also, any performance optimization suggestions for this query query will be appreciated.
Thanks!

Pass order by clause from Java in Jasper Report

I am using latest version of Jasper soft studio to design Jasper reports. I want to pass order by clause to jasper report based on certain criteria from Java. Can you please guide as how to append parameter(containing order by clause from Java) in the report query?
Query
with tmp as (
select d.gl_code, nvl(sum(d.debit),0)- nvl(sum(d.credit),0) as simple
from gl_forms g,gl_voucher_detail d,financial_year f
where g.voucher_no = d.voucher_no
and f.fid = $P{P_FID}
and g.voucher_date >= to_date('01-07-'+f.start_year,'dd-MM-yyyy') and g.voucher_date < $P{P_FROM}
and g.post=1
group by d.gl_code)
select g.voucher_no,
g.voucher_date,
g.voucher_type,
g.remarks remarks,
d.sr_no,
c.account_name,
d.debit,
d.credit,
d.narration narration,
c.account_code,
c.account_name || ' - ' || c.account_code Account,
(select name from company) name,
nvl((o.debit-o.credit),0) + nvl(tmp.simple,0) opening
from gl_voucher_detail d
join gl_forms g
on g.voucher_no = d.voucher_no
and g.post=1
and g.voucher_date between $P{P_FROM} and $P{P_TO}
left outer join opening_balances o
on d.gl_code = o.account_code
and o.fid = $P{P_FID}
right outer join chart_of_account c
on c.account_code = d.gl_code
left outer join tmp
on c.account_code = tmp.gl_code
where c.account_level=4
and c.active=1
and c.account_code = CASEWHEN($P{P_ACCOUNT}='%',c.account_code,$P{P_ACCOUNT})
Order by clause shall be one among these below two based on front fields selection:
order by c.account_name, g.voucher_date,g.voucher_no,d.sr_no
order by c.debit+c.credit desc

SELECT attribute with MIN value from set of attribute instances that are part of specific row?

I know my question is probably very vague and hard to understand at first glance, and i've sat 30 minutes thinking about a proper title. However my database knowledge is very limited so I have a hard time formulating myself properly yet.
It is part of a school assignment I'm currently doing, where the following is what I'm trying to achieve:
and an ER diagram I made of the system:
What I'm trying to accomplish is, selecting the quantity of the component within the computer_system that has the lowest quantity (current stock) so that I in the dataset I am printing out, am able to state exactly how many of each computer_system the store is able to sell, based on the lowest current quantity of any component the computer_system consists of.
This is the query that i am currently working with to accomplish it, but I've had multiple problems with quantity being ambiguous and other errors every time I try to make a fix. I have consulted a dozen of friends from class, but without luck.
SELECT
computer_system.NAME,
cpu.name as cpu,
gpu.name as gpu,
board.name as mainboard,
pccase.name as pc_case,
ram.name as ram,
component.quantity as qty,
(cpu.price *1.3+ board.price*1.3 + pccase.price*1.3 + ram.price*1.3 + gpu.price*1.3) as computer_system_price
FROM computer_system, component
join component cpu on cpu.id = computer_system.cpu
join component gpu on gpu.id = computer_system.gpu
join component board on board.id = computer_system.mainboard
join component pccase on pccase.id = computer_system.pc_case
join component ram on ram.id = computer_system.ram
JOIN component qty ON qty.quantity = (SELECT MIN(component.quantity) FROM component WHERE component.id IN
(computer_system.pc_case,
computer_system.mainboard,
computer_system.cpu,
computer_system.gpu,
computer_system.ram))
The following code fixed it for me:
SELECT computer_system.name AS "System name",
cpu.name AS "CPU",
gpu.name AS "GPU",
pc_case.name AS "Case",
mainboard.name AS "Mainboard",
ram.name AS "RAM",
FLOOR((cpu.price + mainboard.price + pc_case.price + ram.price + coalesce(gpu.price, 0))*1.3/100)*100+99 AS "System price",
SYSTEM.maxamount
FROM computer_system
join (SELECT name,
id,
price
FROM component) AS cpu
ON cpu.id = computer_system.cpu
left join (SELECT name,
id,
price
FROM component) AS gpu
ON coalesce(gpu.id,0) = computer_system.gpu
join (SELECT name,
id,
price
FROM component) AS pc_case
ON pc_case.id = computer_system.pc_case
join (SELECT name,
id,
price
FROM component) AS mainboard
ON mainboard.id = computer_system.mainboard
join (SELECT name,
id,
price
FROM component) AS ram
ON ram.id = computer_system.ram
join (SELECT computer_system.name,
Min(component.quantity) AS maxamount
FROM computer_system,
component
WHERE computer_system.cpu = component.id
OR computer_system.gpu = component.id
OR computer_system.mainboard = component.id
OR computer_system.pc_case = component.id
OR computer_system.ram = component.id
GROUP BY computer_system.name) AS SYSTEM
ON SYSTEM.name = computer_system.name;

SQL query that joins results created by a query

I have two tables:
TWMWR and TWMWRemark. I have two competing criteria so I am trying to run the query separately and then join them. I am trying to find all the records in TWMWR that meet the following criteria:
PSWMS.TWMREMARK.TXT_REMARK LIKE '%Essential Equipment: N%'
AND PSWMS.TWMREMARK.TP_REMARK = '%CIS%'
then I want to take the list of WR (the records) and join that with another table to find the records that meet the following criteria:
PSWMS.TWMREMARK.ID_OPER = 'IXM0Q74'
AND PSWMS.TWMREMARK.TP_REMARK = 'CLM'
AND PSWMS.TWMREMARK.TXT_REMARK LIKE '%SOC%'
They have to be done separately. Can anyone help? I have pasted what I have tried below. I am using mySql developer.
SELECT PSWMS.TWMWR.CD_WR,
PSWMS.TWMWR.TP_JOB,
PSWMS.TWMWR.TS_OPER_RECORDED,
PSWMS.TWMWR.ID_CUSTOMER,
PSWMS.TWMREMARK.TS_REMARK,
PSWMS.TWMREMARK.ID_OPER,
PSWMS.TWMREMARK.TP_REMARK AS TP_REMARK1,
PSWMS.TWMREMARK.TXT_REMARK
FROM PSWMS.TWMWR
INNER JOIN PSWMS.TWMREMARK
ON PSWMS.TWMWR.CD_WR = PSWMS.TWMREMARK.CD_WR
WHERE PSWMS.TWMWR.TP_JOB = 'CLMDPD'
AND PSWMS.TWMWR.TS_OPER_RECORDED >= '21, Oct 2013'
AND PSWMS.TWMREMARK.ID_OPER = 'IXM0Q74'
AND PSWMS.TWMREMARK.TP_REMARK = 'CLM'
AND PSWMS.TWMREMARK.TXT_REMARK LIKE '%SOC%'
GROUP BY PSWMS.TWMWR.CD_WR,
PSWMS.TWMWR.TP_JOB,
PSWMS.TWMWR.TS_OPER_RECORDED,
PSWMS.TWMWR.ID_CUSTOMER,
PSWMS.TWMREMARK.TS_REMARK,
PSWMS.TWMREMARK.ID_OPER,
PSWMS.TWMREMARK.TP_REMARK,
PSWMS.TWMREMARK.TXT_REMARK
INNER JOIN
SELECT PSWMS.TWMWR.CD_WR
FROM PSWMS.TWMWR
INNER JOIN PSWMS.TWMREMARK
ON PSWMS.TWMWR.CD_WR = PSWMS.TWMREMARK.CD_WR
WHERE PSWMS.TWMWR.TP_JOB = 'CLMDPD'
AND PSWMS.TWMWR.TS_OPER_RECORDED >= '21, Oct 2013'
AND PSWMS.TWMREMARK.TXT_REMARK LIKE '%Essential Equipment: N%'
AND PSWMS.TWMREMARK.TP_REMARK = '%CIS%' AS WR
ON PSWMS.TWMWR.CD_WR = WR.CD_WR, PSWMS.TWMREMARK.CD_WR = WR.CD_WR

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.