Can I JOIN table on the basis of the case statement in PostgreSQL - postgresql

Can I JOIN the table on the basis of the case statement in PostgreSQL. I have written the one SQL in stored procedure into that I'm passing the one flag on that basis I want to jon the table. Please see the case statement,
JOIN lease_intervals li_active
ON ( li_active.cid = l.cid AND l.id = li_active.lease_id AND
l.active_lease_interval_id = li_active.id )
LEFT JOIN applications a
ON ( a.cid = li.cid AND li.lease_id = a.lease_id AND
a.lease_interval_id = li.id )
**CASE
WHEN pIsFromUI = TRUE
THEN JOIN property_integration_databases pid ON ( pid.cid = l.cid AND pid.property_id == l.property_id )
JOIN integration_databases id ON ( id.id = pid.integration_database_id AND id.cid = pid.cid )
ELSE
1
END**
Please let me know is there any alternate solution for above.

join
lease_intervals li_active on
li_active.cid = l.cid and l.id = li_active.lease_id and
l.active_lease_interval_id = li_active.id
left join
applications a on
a.cid = li.cid and li.lease_id = a.lease_id and
a.lease_interval_id = li.id
left join
property_integration_databases pid on
pid.cid = l.cid and pid.property_id = l.property_id and pisfromui
left join
integration_databases id on
id.id = pid.integration_database_id and id.cid = pid.cid and pisfromui

Related

Syntax error raw sql on rails when query inner results

how do you query raw SQL on rails?
So I have this raw sql that I need to run on rails but giving me a syntax error. I also escape the extra parenthesis but still got a syntax error near the first inner join.
here's my code:
Spree::CorporateAccount.joins(" (((((
( inner join spree_memberships on spree_corporate_accounts.id = spree_memberships.corporate_account_id)
inner join spree_users on spree_memberships.user_id = spree_users.id)
left join spree_variant_price_sets on spree_corporate_accounts.variant_price_set_id = spree_variant_price_sets.id)
left join spree_addresses on spree_corporate_accounts.bill_address_id = spree_addresses.id)
left join spree_states on spree_addresses.state_id = spree_states.id)
left join spree_countries on spree_addresses.country_id = spree_countries.id)
left join spree_partner_accounts on spree_corporate_accounts.id = spree_partner_accounts.partnerable_id
").where(" spree_memberships.deleted_at IS null
AND (spree_partner_accounts.partnerable_type = 'Spree::CorporateAccount' OR spree_partner_accounts.partnerable_type IS NULL)
AND admin = true
")
but on sql this is perfectly fine.
SELECT
spree_corporate_accounts.id,
spree_corporate_accounts.company_name,
spree_memberships.ADMIN,
spree_users.email,
spree_users.doctor AS name,
spree_partner_accounts.account_key,
CASE spree_corporate_accounts.billing_type when 1 THEN 'Postbill' WHEN 2 THEN 'Creditcard' ELSE 'Individual'END,
spree_variant_price_sets.name AS priceset,
spree_addresses.address1,
spree_addresses.address2,
spree_addresses.city,
spree_addresses.zipcode,
spree_states.name AS state,
spree_countries.name AS country,
spree_addresses.phone,
spree_users.created_at
from (((((( spree_corporate_accounts inner join spree_memberships on spree_corporate_accounts.id = spree_memberships.corporate_account_id)
inner join spree_users on spree_memberships.user_id = spree_users.id)
left join spree_variant_price_sets on spree_corporate_accounts.variant_price_set_id = spree_variant_price_sets.id)
left join spree_addresses on spree_corporate_accounts.bill_address_id = spree_addresses.id)
left join spree_states on spree_addresses.state_id = spree_states.id)
left join spree_countries on spree_addresses.country_id = spree_countries.id)
left join spree_partner_accounts on spree_corporate_accounts.id = spree_partner_accounts.partnerable_id
where spree_memberships.deleted_at IS null
and (spree_partner_accounts.partnerable_type = 'Spree::CorporateAccount' OR spree_partner_accounts.partnerable_type IS NULL )
AND admin = true
if I do this. It will yield a different result. so what im thinking is the parenthesis evaluate the result first and then go to the next.
Spree::CorporateAccount.joins("inner join spree_memberships on spree_corporate_accounts.id = spree_memberships.corporate_account_id")
.joins("inner join spree_users on spree_memberships.user_id = spree_users.id")
.joins("left join spree_variant_price_sets on spree_corporate_accounts.variant_price_set_id = spree_variant_price_sets.id")
.joins("left join spree_addresses on spree_corporate_accounts.bill_address_id = spree_addresses.id")
.joins("left join spree_states on spree_addresses.state_id = spree_states.id")
.joins("left join spree_countries on spree_addresses.country_id = spree_countries.id")
.joins("left join spree_partner_accounts on spree_corporate_accounts.id = spree_partner_accounts.partnerable_id ")
.where("spree_memberships.deleted_at IS null
AND spree_partner_accounts.partnerable_type = 'Spree::CorporateAccount' OR spree_partner_accounts.partnerable_type IS NULL
AND admin = true
")

Incorrect Syntax multi part identifier

Currently I'm getting an incorrect syntax but can't locate its origins. Any thoughts on what i'm missing. Multi-part identifier pvxme.mt couldn't be found.
SELECT
PVXME.MT
PVXME.MT_VERSION
PVXME.START_DATE_LOCAL
PVXMEDS.CREATION_DATE_LOCAL
PVXMIHS.USER_NAME
PVXMEDE.PAT_ID
PVXMEDE.STRING_VALUE
PVXMEDE.NUM_VALUE AS MEDE_NUM_VALUE
RVXMIIF2.II
RVXMIIF2.NUM_VALUE AS MIIF_NUM_VALUE;
FROM
PVXMEDE
RIGHT OUTER JOIN
PVXMEDG ON PVXMEDG.ME = PVXMEDE.ME;
RIGHT OUTER JOIN
PVXMEDG ON PVXMEDG.SEQUENCE = PVXMEDE.SEQUENCE
AND PVXMEDG.SOURCE_TP = PVXMEDE.SOURCE_TP
AND PVXMEDG.SOURCE_ID = PVXMEDE.SOURCE_ID
AND PVXMEDG.SOURCE_VERSION = PVXMEDE.SOURCE_VERSION;
RIGHT OUTER JOIN
PVXMEDS ON PVXMEDS.ME = PVXMEDG.ME
AND PVXMEDS.SEQUENCE=PVXMEDG.SEQUENCE;
RIGHT OUTER JOIN
PVXME ON PVXME.ME = PVXMEDS.ME AND PVXME.MT=;
RIGHT OUTER JOIN
PVXMI ON PVXMI.MI = PVXME.MI;
LEFT OUTER JOIN
PVXMIHS ON PVXMIHS.MI = PVXME.MI;
LEFT OUTER JOIN
PVXMIID ON PVXME.ME = PVXMIID2.ME;
LEFT OUTER JOIN
RVXMIII ON PVXMIID2.MI = RVXMIIF2.MI
AND PVXMIID2.ID = RVXMIIF2.ID
AND PVXMIID2.ID_SEQUENCE = RVXMIIF2.ID_SEQUENCE
AND PVXMIID2.ME = RVXMIIF2.ME;
WHERE
(PVXMIHS.USER_NAME <> 'sipat'
AND PVXMIHS.WHAT = 'MethodPrepare'
AND PVXME.CX_STRING_4 = '20190117-7h40m'
AND PVXME.MT LIKE 'MK-0431%Tab CA%'
AND PVXMEDE.PAT_ID NOT LIKE '%Spectrum')
AND (RVXMIIF2.II = 'LeverageLimit'OR RVXMIIF2.II = 'XresidualLimit')
Try this:
do put a colon after each column in the SELECT list of columns
do NOT put a semicolon at the end of the list of columns and of each JOIN condition
do complete the RIGHT OUTER JOIN that references PVXME.MT = .......
Code:
SELECT
PVXME.MT,
PVXME.MT_VERSION,
PVXME.START_DATE_LOCAL,
PVXMEDS.CREATION_DATE_LOCAL,
PVXMIHS.USER_NAME,
PVXMEDE.PAT_ID,
PVXMEDE.STRING_VALUE,
PVXMEDE.NUM_VALUE AS MEDE_NUM_VALUE,
RVXMIIF2.II,
RVXMIIF2.NUM_VALUE AS MIIF_NUM_VALUE
FROM
PVXMEDE
RIGHT OUTER JOIN
PVXMEDG P1 ON P1.ME = PVXMEDE.ME -- use table alias P1 here
RIGHT OUTER JOIN
PVXMEDG P2 ON P2.SEQUENCE = PVXMEDE.SEQUENCE -- use table alias P2 here
AND P2.SOURCE_TP = PVXMEDE.SOURCE_TP
AND P2.SOURCE_ID = PVXMEDE.SOURCE_ID
AND P2.SOURCE_VERSION = PVXMEDE.SOURCE_VERSION
RIGHT OUTER JOIN
PVXMEDS ON PVXMEDS.ME = P2.ME
AND PVXMEDS.SEQUENCE = P2.SEQUENCE
RIGHT OUTER JOIN
PVXME ON PVXME.ME = PVXMEDS.ME AND PVXME.MT = ?????? -- ** COMPLETE THIS! **
RIGHT OUTER JOIN
PVXMI ON PVXMI.MI = PVXME.MI
LEFT OUTER JOIN
PVXMIHS ON PVXMIHS.MI = PVXME.MI
LEFT OUTER JOIN
PVXMIID ON PVXME.ME = PVXMIID2.ME
LEFT OUTER JOIN
RVXMIII ON PVXMIID2.MI = RVXMIIF2.MI
AND PVXMIID2.ID = RVXMIIF2.ID
AND PVXMIID2.ID_SEQUENCE = RVXMIIF2.ID_SEQUENCE
AND PVXMIID2.ME = RVXMIIF2.ME
WHERE
(PVXMIHS.USER_NAME <> 'sipat'
AND PVXMIHS.WHAT = 'MethodPrepare'
AND PVXME.CX_STRING_4 = '20190117-7h40m'
AND PVXME.MT LIKE 'MK-0431%Tab CA%'
AND PVXMEDE.PAT_ID NOT LIKE '%Spectrum')
AND (RVXMIIF2.II = 'LeverageLimit'OR RVXMIIF2.II = 'XresidualLimit')

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

change query from postgresql into oracle

how change this query from postgresql into oracle?
SELECT tbmm.tbmm_id,
tbmm.tbmm_email AS seller_mail,
'FB' AS seller_type,
concat(tbmm.tbmm_firstname, COALESCE(tbmm.tbmm_middlename),
COALESCE(tbmm.tbmm_lastname)) AS seller_owner_name,
tbmss.tbmss_name AS seller_store_name,
tbmss.tbmss_description AS seller_description,
tbmss.tbmss_phone AS seller_phone,
btrim(concat(COALESCE(tbmma.tbmma_addr1), COALESCE(tbmma.tbmma_addr2,
tbmma.tbmma_addr1), COALESCE(tbmma.tbmma_addr3, tbmma.tbmma_addr1),
COALESCE(tbmkc.tbmkct_nama), COALESCE(tbmkl.tbmkl_nama),
COALESCE(tbmma.tbmma_postcode, '0') AS seller_store_address,
tbmb.tbmb_name AS bank_name,
concat(tbmb.tbmb_name, COALESCE(tbmsd.tbmsd_bank_account_branch) AS
bank_desc,
tbmsd.tbmsd_bank_account_no AS bank_account_no,
tbmsd.tbmsd_bank_account_name AS bank_account_name,
tbmsd.tbmsd_no_id AS seller_id_card,
'F' AS flag_alfamind,
0 AS commision,
tbmm.tbmm_mage_entity_id,
tbmm.tbmm_update_date AS member_update_date,
tbmss.tbmss_update_date AS store_update_date,
tbmsd.tbmsd_update_date AS document_update_date,
tbmma.tbmma_update_date AS address_update_date
FROM coreapp.tb_master_member tbmm
JOIN coreapp.tb_master_seller_store tbmss ON tbmm.tbmm_id =
tbmss.tbmm_id
JOIN coreapp.tb_master_seller_document tbmsd ON tbmsd.tbmm_id =
tbmm.tbmm_id
JOIN coreapp.tb_master_bank tbmb ON tbmb.tbmb_id = tbmsd.tbmb_id
JOIN coreapp.tb_master_member_group tbmg ON tbmg.tbmm_id = tbmm.tbmm_id
LEFT JOIN coreapp.tb_master_member_address tbmma ON tbmma.tbmm_id =
tbmm.tbmm_id AND tbmma.tbmma_address_type = 140 AND tbmma.tbmma_status = 23
LEFT JOIN coreapp.tb_master_kecamatan tbmkc ON
tbmkc.tbmkct_kecamatan_id = tbmma.tbmkct_kecamatan_id
LEFT JOIN coreapp.tb_master_kelurahan tbmkl ON tbmkl.tbmkl_kelurahan_id
= tbmma.tbmkl_kelurahan_id
WHERE tbmg.tbmg_id = 4;
i have try to query on my sql developer but something error with query
now I'm just try until one day and still can't run

Does Not Exist using Multiple Columns

I am trying to find all records in my #TempTable that are not in the staging table.
Its important to note that the comparison needs to take place over 16 fields.
I have tried several combinations and nothing seems to work.
SELECT CustomerAccountNo FROM #TempTable
WHERE NOT EXISTS
(SELECT e.[CustomerAccountNo] ,
e.[MeterNo] ,
e.[CustomerName1] ,
e.[ServiceAddress1] ,
e.[ServiceAddress2] ,
e.[ServiceCity] ,
e.[ServiceState] ,
e.[ServiceZip] ,
e.[BillingAddress1] ,
e.[BillingAddress2] ,
e.[BillingAddress3] ,
e.[BillingCity] ,
e.[BillingState] ,
e.[BillingZip] ,
e.[BillingZip4] ,
e.[PrimaryPhoneNumber] FROM #TempTable e
JOIN dbo.Staging s
ON e.CustomerAccountNo = s.CustomerAccountNo AND
e.MeterNo = s.MeterNo AND
e.CustomerName1 = s.CustomerName1 AND
e.ServiceAddress1 = s.ServiceAddress1 AND
e.ServiceAddress2 = s.ServiceAddress2 AND
e.ServiceCity = s.ServiceCity AND
e.ServiceState = s.ServiceState AND
e.ServiceZip = s.ServiceZip AND
e.BillingAddress1 = s.BillingAddress1 AND
e.BillingAddress2 = s.BillingAddress2 AND
e.BillingAddress3 = s.BillingAddress3 AND
e.BillingCity = s.BillingCity AND
e.BillingState = s.BillingState AND
e.BillingZip = s.BillingZip AND
e.BillingZip4 = s.BillingZip4 AND
e.PrimaryPhoneNumber= s.PrimaryPhoneNumber
)
Instead of a JOIN, try using Except.
SELECT CustomerAccountNo, MeterNo -- and so on
FROM #TempTable
EXCEPT
SELECT CustomerAccountNo, MeterNo -- and so on
FROM Staging
Just do a join and look for null. Like this
SELECT e.*
FROM #TempTable e
LEFT JOIN dbo.Staging s
ON e.CustomerAccountNo = s.CustomerAccountNo AND
e.MeterNo = s.MeterNo AND
e.CustomerName1 = s.CustomerName1 AND
e.ServiceAddress1 = s.ServiceAddress1 AND
e.ServiceAddress2 = s.ServiceAddress2 AND
e.ServiceCity = s.ServiceCity AND
e.ServiceState = s.ServiceState AND
e.ServiceZip = s.ServiceZip AND
e.BillingAddress1 = s.BillingAddress1 AND
e.BillingAddress2 = s.BillingAddress2 AND
e.BillingAddress3 = s.BillingAddress3 AND
e.BillingCity = s.BillingCity AND
e.BillingState = s.BillingState AND
e.BillingZip = s.BillingZip AND
e.BillingZip4 = s.BillingZip4 AND
e.PrimaryPhoneNumber= s.PrimaryPhoneNumb
WHERE s.CustomerAccountNo is null
You should provide more detailed circumstance to get correct answer.
Clearly, you don't have any connection between your FROM clause and WHERE clause so the query will return all.
Try this one:
SELECT CustomerAccountNo FROM #TempTable t
WHERE NOT EXISTS
(SELECT 1 FROM dbo.Staging s WHERE
t.CustomerAccountNo = s.CustomerAccountNo AND
t.MeterNo = s.MeterNo AND
t.CustomerName1 = s.CustomerName1 AND
t.ServiceAddress1 = s.ServiceAddress1 AND
t.ServiceAddress2 = s.ServiceAddress2 AND
t.ServiceCity = s.ServiceCity AND
t.ServiceState = s.ServiceState AND
t.ServiceZip = s.ServiceZip AND
t.BillingAddress1 = s.BillingAddress1 AND
t.BillingAddress2 = s.BillingAddress2 AND
t.BillingAddress3 = s.BillingAddress3 AND
t.BillingCity = s.BillingCity AND
t.BillingState = s.BillingState AND
t.BillingZip = s.BillingZip AND
t.BillingZip4 = s.BillingZip4 AND
t.PrimaryPhoneNumber= s.PrimaryPhoneNumber
)