multiplication in group by , dyslexia - tsql

Now this is a simple insert query with one subquery and it's working perfectly:
INSERT INTO CSolutions..Report2
SELECT Nov.Chain, CG.Grouping, reg.code as regcode,reg.pname,reg.fname,reg.sname, SUM(NS.Currency) as sumeur , sum(ns2.kpl) as sumkpl,0,0,0,0
FROM CSolutions..NovSales NS,CSolutions..NSaleskpl NS2, CSolutions..NovCGroup CG, sales..reg_eng reg,
(SELECT DISTINCT P.Code, Nov.Chain
FROM Register..Pcy P
INNER JOIN Register..TCustomer TC ON (P.Code = TC.PCode)
INNER JOIN CSolutions..Novies2 Nov ON (TC.Code = Nov.TCode)
) AS Nov
WHERE NS.SpCode = CG.SpCode and ns.spcode = ns2.spcode AND Ns.PCode = Nov.Code
and ns.spcode = reg.code
and ns.pcode = ns2.pcode
**and NS.SalesMonth = '201205'
and NS2.SalesMonth = '201205'**
GROUP BY Nov.Chain, CG.Grouping , reg.code,reg.pname,reg.fname,reg.sname
This is derivate of the same query, but it's not working correctly:
INSERT INTO CSolutions..Report2
SELECT Nov.Chain, CG.Grouping, reg.code as regcode,reg.pname,reg.fname,reg.sname, SUM(NS.Currency) as sumeur , sum(ns2.kpl) as sumkpl,0,0,0,0
FROM CSolutions..NovSales NS,CSolutions..NSaleskpl NS2, CSolutions..NovCGroup CG, sales..reg_eng reg,
(SELECT DISTINCT P.Code, Nov.Chain
FROM Register..Pcy P
INNER JOIN Register..TCustomer TC ON (P.Code = TC.PCode)
INNER JOIN CSolutions..Novies2 Nov ON (TC.Code = Nov.TCode)
) AS Nov
WHERE NS.SpCode = CG.SpCode and ns.spcode = ns2.spcode AND Ns.PCode = Nov.Code
and ns.spcode = reg.code
and ns.pcode = ns2.pcode
**and NS.SalesMonth between '201201' and '201205'
and NS2.SalesMonth between '201201' and '201205'**
GROUP BY Nov.Chain, CG.Grouping , reg.code,reg.pname,reg.fname,reg.sname
This will give 5 times the 5 month sum amount,
I am a dyslexic and i have hard time to spot the error, I hope someone can help me ?? :)

There is no join on the salesmonth in CSolutions..NovSales and CSolutions..NSaleskpl NS2. In the frist query this does not matter because you are only looking at one salesmonth. In the second query I presume there are multiple salesmonth's as you are selecting a range of value from each table. You are getting a cross join because of this of salesmonth's because of this.

Related

Postgresql: Error missing FROM-clause entry for table

I have a query and I keep getting this a 'missing FROM-clase entry for table e, rx.rootentityid = rh.entityid1 AND rh.schoolid = e.schoolid'.
can anyone see the error in my code. My postgresql knowledge is very limited.
SELECT
eh.entityid1, eh.entityname1, eh.entityid2, eh.entityname2,
eh.entityid3, eh.entityname3, eh.entityid4, eh.entityname4,
eh.entityid5, eh.entityname5, eh.entityid6, eh.entityname6,
eh.entityid7, eh.entityname7, eh.schoolentityid, eh.schoolid,
eh.emiscode, eh.schoolname, rh.entityid1 AS reportentityid1,
rh.entityname1 AS reportentityname1, rh.entityid2 AS reportentityid2,
rh.entityname2 AS reportentityname2
FROM dbo.vwentityhierarchyname
JOIN dbo.entity AS eh
ON vwentityhierarchyname.entityid1 = eh.entityid AND eh.entitytypeid = 1
CROSS JOIN (SELECT
hd.rootentityid
FROM dbo.hierarchydefinition AS hd
JOIN dbo.entity AS e
ON hd.rootentityid = e.entityid
WHERE hd.relatedrootentityid = e.entityid AND hd.entitytypeid = 7 AND LOWER(e.name) SIMILAR TO LOWER('%Report Regions%')) AS rx
LEFT OUTER JOIN dbo.vwentityhierarchyname AS rh
ON rx.rootentityid = rh.entityid1 AND rh.schoolid = e.schoolid
WHERE eh.schoolid = par_SchoolID
ORDER BY eh.entityid1 DESC NULLS FIRST
LIMIT 1;
Thanks

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!

Why Group By is not working as expected in 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

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

Variable "Rs.Temp" not found

I'm getting error while executing a sql query in Alpha Anywhere Query (AlphaADO) section.
The code i'm executing is attached.
SELECT * FROM
(SELECT
tbl_wo_kitting.wo_kitting_id,
tbl_wo_kitting.level_no,
tbl_wo_kitting.line_no_parent,
tbl_wo_kitting.line_no AS kitting_line_no,
tbl_wo_kitting.production_type,
tbl_wo_kitting.quantity AS kitting_quantity,
tbl_wo_kitting.release_to_wr,
tbl_wo_kitting.mpn,
m_product.m_product_id,
m_product.value,
m_product.name,
m_product.storelocator,
tbl_wo_project.wo_project_id,
tbl_wo_project.line_no AS project_line_no,
tbl_wo_project.quantity AS project_quantity,
m_transaction_moveqty_v.qtyonhand AS qtyonhand,
tbl_work_order.work_order_id,
tbl_work_order.reference_key,
case
when tbl_wo_kitting.level_no, = 'P+'
then '0'
else tbl_wo_kitting.level_no,
end as linenumber,
case
when tbl_wo_kitting.line_no_parent = 'PT+'
then '0'
else tbl_wo_kitting.line_no_parent
end as linenoparent
FROM (tbl_wo_kitting tbl_wo_kitting
INNER JOIN (m_product m_product
INNER JOIN m_transaction_moveqty_v m_transaction_moveqty_v
ON m_product.m_product_id = m_transaction_moveqty_v.m_product_id )
ON tbl_wo_kitting.m_product_id = m_product.m_product_id
INNER JOIN (tbl_wo_project tbl_wo_project
INNER JOIN tbl_work_order tbl_work_order
ON tbl_wo_project.work_order_id = tbl_work_order.work_order_id )
ON tbl_wo_kitting.wo_project_id = tbl_wo_project.wo_project_id )) AS TABLE1
ORDER BY tbl_wo_kitting.level_no, (string_to_array(linenoparent, '.'))::int[],(string_to_array(linenumber, '.'))::int[]
Any idea? This is selecting from PostgreSQL.
Thank you very much in advance.